Variables and Data Types#

A variable is a container for storing data. Python uses dynamic typing — no need to declare types, just assign a value directly:

1
2
3
x = 10
name = "Alice"
is_active = True

Variable Naming Rules#

  • Can only contain letters, digits, and underscores _
  • Cannot start with a digit
  • Case-sensitive (age and Age are different variables)
  • Cannot use Python reserved keywords (e.g., if, for, class)
1
2
3
4
5
6
7
8
# Valid variable names
user_name = "Bob"
score1 = 95
_private = True

# Invalid (will cause errors)
# 1score = 95      # Cannot start with a digit
# my-name = "Bob"  # Cannot use hyphens

Multiple Assignment#

1
2
3
4
5
6
7
# Assign values to multiple variables at once
a, b, c = 1, 2, 3
print(a, b, c)  # 1 2 3

# Multiple variables pointing to the same value
x = y = z = 0
print(x, y, z)  # 0 0 0

Data Types#

Python has the following basic data types:

TypeDescriptionExample
intInteger10, -5, 0
floatFloat (decimal)3.14, -0.5
strString"hello", 'world'
boolBooleanTrue, False
NoneTypeNull valueNone

Check the Type: type()#

1
2
3
4
5
print(type(10))       # <class 'int'>
print(type(3.14))     # <class 'float'>
print(type("hello"))  # <class 'str'>
print(type(True))     # <class 'bool'>
print(type(None))     # <class 'NoneType'>

Integer (int)#

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
a = 100
b = -50
c = 0

# Large numbers can use underscores as separators for readability
population = 23_000_000
print(population)  # 23000000

# Different number bases
binary = 0b1010    # Binary, value is 10
octal = 0o12       # Octal, value is 10
hexadecimal = 0xA  # Hexadecimal, value is 10

Float (float)#

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
pi = 3.14159
temperature = -10.5

# Scientific notation
speed_of_light = 3e8   # 3 × 10^8
tiny = 1.5e-4          # 0.00015

# Note: floating-point precision issue
print(0.1 + 0.2)       # 0.30000000000000004 (not exact)
print(round(0.1 + 0.2, 1))  # 0.3 (rounded to 1 decimal place)

Boolean (bool)#

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
is_raining = True
has_ticket = False

# Booleans are actually a subclass of integers
print(True + True)   # 2
print(True * 5)      # 5
print(False + 1)     # 1

# Which values are equivalent to False?
print(bool(0))       # False
print(bool(""))      # False (empty string)
print(bool([]))      # False (empty list)
print(bool(None))    # False

# Which values are equivalent to True?
print(bool(1))       # True
print(bool("hi"))    # True
print(bool([1, 2]))  # True

None#

None represents “no value”, similar to null in other languages:

1
2
3
4
5
result = None

# Conventionally use is to check for None
if result is None:
    print("No result yet")

Type Conversion#

Explicit Conversion#

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
# String to integer
age = int("25")
print(age, type(age))  # 25 <class 'int'>

# Integer to float
price = float(100)
print(price)  # 100.0

# Number to string
score = str(95)
print("Score: " + score)  # Score: 95

# Convert to boolean
print(bool(0))    # False
print(bool(100))  # True

Conversion Failures Raise Errors#

1
2
3
4
5
6
7
# The following will raise a ValueError
# int("hello")   # Cannot convert a non-numeric string to integer
# int("3.14")    # Decimal point not allowed — use float() first

# Correct approach
num = int(float("3.14"))  # Convert to float first, then to int
print(num)  # 3

Operators#

Arithmetic Operators#

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
a = 10
b = 3

print(a + b)   # 13, addition
print(a - b)   # 7, subtraction
print(a * b)   # 30, multiplication
print(a / b)   # 3.3333..., division (result is float)
print(a // b)  # 3, integer division (truncates decimal)
print(a % b)   # 1, modulo (remainder)
print(a ** b)  # 1000, exponentiation (10 to the power of 3)

Assignment Operators#

1
2
3
4
5
6
x = 10
x += 5   # equivalent to x = x + 5, x becomes 15
x -= 3   # x = x - 3, x becomes 12
x *= 2   # x = x * 2, x becomes 24
x //= 5  # x = x // 5, x becomes 4
x **= 2  # x = x ** 2, x becomes 16

Comparison Operators (Return Boolean)#

1
2
3
4
5
6
7
8
a, b = 10, 5

print(a == b)  # False, equal to
print(a != b)  # True, not equal to
print(a > b)   # True, greater than
print(a < b)   # False, less than
print(a >= 10) # True, greater than or equal to
print(a <= 9)  # False, less than or equal to

Logical Operators#

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
age = 20
has_id = True

# and: both conditions must be True
print(age >= 18 and has_id)  # True

# or: at least one condition must be True
print(age < 18 or has_id)    # True

# not: reverses the boolean value
print(not has_id)            # False

input() — Get User Input#

input() reads input from the user. The return value is always a string :

1
2
3
4
5
6
name = input("Enter your name: ")
print("Hello, " + name + "!")

# If you need a number, you must convert it manually
age = int(input("Enter your age: "))
print("Next year you will be", age + 1, "years old")