Loading notes...
Loading notes...
Class 11 • Chapter 4
Chapter 4 covers the fundamental building blocks of Python scripts: variables, data types, dynamic typing, and handling basic input/output operations.
Every Python program is built using a few core structural components. Understanding how to create variables, identify data types, capture user input, and display output is the foundation of actually writing functional Python scripts. This chapter covers the absolute necessities to get your code talking to the user.
4.1 Variables and Dynamic Typing
A variable is a named memory location used to store data. In Python, you do not need to explicitly declare a variable's type before using it (like you do in C++ or Java). Python is Dynamically Typed, meaning it automatically figures out the data type based on the value you assign to it during runtime.
# Creating variables using the assignment operator (=)
player_name = "Basant" # Python knows this is a String
score = 1500 # Python knows this is an Integer
# Dynamic typing in action: We can change the type of a variable at any time
score = "High Score!" # 'score' is now a String. This is totally valid in Python.4.2 Core Data Types
Although Python infers data types automatically, you must know the fundamental types because different types behave differently (e.g., you can multiply integers, but multiplying strings does something else).
4.3 The type() Function
Because Python hides the data type, you might sometimes forget what type of data a variable is holding. Python provides the built-in `type()` function to reveal the exact data type of any variable or literal.
gravity = 9.8
is_active = True
print(type(gravity)) # Output: <class 'float'>
print(type(is_active)) # Output: <class 'bool'>
print(type("123")) # Output: <class 'str'> (Because of the quotes!)4.4 Output: The print() Function
The `print()` function is used to display output to the screen. It can take multiple arguments separated by commas, automatically inserting a space between them.
The `print()` function also has optional parameters like `sep` (separator) and `end` (what to print at the very end, which defaults to a newline).
name = "Alice"
age = 16
# Standard printing
print("Student:", name, "Age:", age)
# Using 'sep' to change the default space separator to a dash
print(10, 20, 30, sep="-") # Output: 10-20-30
# Using 'end' to prevent the print function from moving to a new line
print("Hello", end="***")
print("World") # Output: Hello***World4.5 Input: The input() Function
To make a program interactive, we use the `input()` function to pause execution and wait for the user to type something on the keyboard. CRITICAL RULE: The `input()` function ALWAYS returns the user's data as a String (str), even if they type a number.
user_name = input("Enter your name: ")
# Even if the user types 18, it is stored as the string "18"
user_age = input("Enter your age: ")
print("Welcome", user_name)
print(type(user_age)) # Output: <class 'str'>4.6 Type Conversion (Type Casting)
Because `input()` always returns a string, we cannot do math with it immediately. We must convert it using Type Casting functions: `int()`, `float()`, or `str()`.
# We wrap the input() function inside int() to convert the string immediately to an integer
birth_year = int(input("Enter birth year: "))
# Now we can safely perform math
age = 2024 - birth_year
print("You are", age, "years old.")