Day 1 - Core Python: Syntax, Variables, Data Types
1. Python Syntax and Indentation
Python uses indentation (whitespace) to define blocks of code. Correct indentation is important to avoid
errors. Real-life example: If you follow traffic rules properly (indentation), you avoid accidents (errors).
if 5 > 2:
print('Five is greater than two')
2. Comments
Comments are used to explain code and make it more readable. They are ignored by the Python interpreter.
# This is a single-line comment
'''This is a
multi-line comment'''
3. Variables and Constants
Variables store data values. Constants are variables whose values should not change (use uppercase by
convention).
name = 'Saniya'
age = 23
PI = 3.14 # Constant
4. Data Types
Common data types include:
- int: Integer numbers
- float: Decimal numbers
- str: Text data
- bool: True/False values
x = 10 # int
y = 3.14 # float
z = 'Hi' # str
b = True # bool
Day 1 - Core Python: Syntax, Variables, Data Types
5. Type Checking and Type Casting
Use type() to check data type. Use int(), float(), str() to convert between types.
x = '123'
print(int(x)) # 123
print(type(int(x))) # <class 'int'>