⬅ Previous Next ➡

Variables, Data Types & I/O

Variables in Python
- Variable = a name that stores a value in memory. - Python creates variable when you assign a value. - No need to declare type. Example: name = "Sourav" age = 25 marks = 89.5
Assignment Operators
- Assignment stores value into variable. Common operators: = (assign) += (add and assign) -= (subtract and assign) *= (multiply and assign) /= (divide and assign) //= (floor divide and assign) %= (modulus and assign) **= (power and assign) Example: x = 10 x += 5 x *= 2 print(x) # 30
Naming Rules (Identifiers)
Rules: - Must start with letter or underscore (_) - Cannot start with digit - Only letters, digits, underscore allowed - Cannot be a keyword - Case-sensitive Good names: total_marks student_name _temp Bad names: 1value class my-name
Built-in Data Types (Basics)
Main built-in types: int -> 10, -5 float -> 10.5, 3.14 bool -> True, False str -> "hello" complex -> 2+3j Collection types: list -> [1, 2, 3] tuple -> (1, 2, 3) set -> {1, 2, 3} dict -> {"a": 1, "b": 2} Example: a = 10 b = 12.5 c = "Python" d = True print(type(a), type(b), type(c), type(d))
Type Casting (Type Conversion)
- Converting one data type into another. Common functions: int(), float(), str(), bool(), list(), tuple(), set() Example: x = "123" y = int(x) z = float(y) print(y, z) Note: int("12.5") gives error, use float first.
Constants in Python
- Python has no true constant keyword. - We use ALL_CAPS naming to indicate constant (convention). Example: PI = 3.14159 MAX_USERS = 100 Note: Constants can still be changed, but we avoid changing them.
User Input Methods
- input() takes data as string by default. - Convert using int()/float() when needed. Example (string input): name = input("Enter name: ") print("Hello", name) Example (integer input): age = int(input("Enter age: ")) print("Age:", age) Example (two numbers): a = int(input("Enter a: ")) b = int(input("Enter b: ")) print("Sum:", a + b)
Output Formatting in Python
1) Using comma (simple): name = "Sourav" age = 25 print("Name:", name, "Age:", age) 2) f-string (recommended): print(f"Name: {name}, Age: {age}") 3) format() method: print("Name: {}, Age: {}".format(name, age)) 4) Controlling decimals: pi = 3.14159 print(f"Pi: {pi:.2f}") # 3.14
Mini Program: Simple Calculator (Input + Casting + Output)
Program: a = float(input("Enter first number: ")) b = float(input("Enter second number: ")) add = a + b sub = a - b mul = a * b div = a / b print(f"Add: {add}") print(f"Sub: {sub}") print(f"Mul: {mul}") print(f"Div: {div:.2f}")
Variables in Python
  • Variable stores a value in memory.
  • Python creates a variable when you assign a value.
  • No need to declare data type (dynamic typing).
name = "Sourav"
age = 25
marks = 89.5
is_pass = True
Assignments in Python
  • Assignment operator = stores value into a variable.
  • Multiple assignment is possible.
  • Swapping can be done without a temp variable.
x = 10
y = 20

a, b = 5, 7      # multiple assignment
a, b = b, a      # swapping
print(a, b)
Assignment Operators
  • Used to update a variable value in short form.
x = 10
x += 5     # x = x + 5
x -= 2     # x = x - 2
x *= 3     # x = x * 3
x /= 2     # x = x / 2
x //= 2    # floor division
x %= 3     # remainder
x **= 2    # power
print(x)
Naming Rules (Identifiers)
  • Must start with a letter (A-Z/a-z) or underscore (_).
  • Cannot start with a digit.
  • Only letters, digits, and underscore allowed.
  • Cannot be a keyword (if, class, for, etc.).
  • Case-sensitive (name and Name are different).
Valid:
total_marks
_student
marks1

Invalid:
1total
class
my-name
Built-in Data Types (Basics)
  • int (10, -5), float (3.14), bool (True/False), str ("hi")
  • list [1,2], tuple (1,2), set {1,2}, dict {"a":1}
a = 10
b = 12.5
c = "Python"
d = True

nums = [1, 2, 3]
point = (10, 20)
unique = {1, 2, 2, 3}
student = {"name": "Sourav", "age": 25}

print(type(a), type(b), type(c), type(d))
print(type(nums), type(point), type(unique), type(student))
Type Casting (Type Conversion)
  • Converting one data type to another.
  • Common functions: int(), float(), str(), bool(), list(), tuple(), set().
x = "123"
y = int(x)        # string to int
z = float(y)      # int to float
s = str(z)        # float to string

print(y)
print(z)
print(s)
  • Note: int("12.5") gives error. Use float first.
Constants in Python (Convention)
  • Python has no fixed constant keyword.
  • We use ALL_CAPS naming to represent constants (convention).
  • Constants should not be changed in code.
PI = 3.14159
MAX_USERS = 100

print(PI)
print(MAX_USERS)
User Input Methods
  • input() always returns a string.
  • Convert using int() / float() for numbers.
name = input("Enter name: ")
print("Hello", name)

age = int(input("Enter age: "))
print("Age:", age)

a = float(input("Enter a: "))
b = float(input("Enter b: "))
print("Sum:", a + b)
Output Formatting in Python
  • Print with comma (simple).
  • Use f-string (recommended).
  • Use format() method.
  • Control decimals using :.2f etc.
name = "Sourav"
age = 25
pi = 3.14159

# 1) comma
print("Name:", name, "Age:", age)

# 2) f-string
print(f"Name: {name}, Age: {age}")

# 3) format()
print("Name: {}, Age: {}".format(name, age))

# 4) decimal control
print(f"Pi: {pi:.2f}")
Mini Program: Input + Casting + Formatting
  • This program takes marks and prints percentage with 2 decimals.
total = float(input("Enter total marks: "))
obtained = float(input("Enter obtained marks: "))

percent = (obtained / total) * 100

print(f"Percentage: {percent:.2f}%")
⬅ Previous Next ➡