LEARN COMPLETE PYTHON IN 24 HOURS

2. Basic Building Blocks

These are the foundational elements of Python programming. Once you understand them, you can start building real programs!

2.1 Variables and Data Types

Variables are like containers that store data (values). In Python, you don't need to declare the type — it's dynamically typed (Python figures it out automatically).

Syntax to create a variable:

Python

variable_name = value

Rules for variable names:

  • Start with letter (a-z, A-Z) or underscore (_)

  • Can contain letters, numbers, underscore

  • Case-sensitive (age ≠ Age)

  • Cannot be Python keywords (like if, for, print)

Examples:

Python

# Valid variables name = "Anshuman" # string age = 25 # integer height = 5.9 # float is_student = True # boolean _marks = 98 # starts with underscore (ok) # Invalid examples (will give error) # 1age = 20 # starts with number # my-name = "Test" # has hyphen

Main Data Types in Python (built-in):

  • int → Whole numbers (e.g., 5, -100, 0)

  • float → Decimal numbers (e.g., 3.14, -0.5)

  • str → Text/strings (e.g., "Hello", 'Python')

  • bool → True or False

  • complex → Complex numbers (e.g., 3+4j) — used in math/science

Python also has more advanced types like list, tuple, dict, set (covered later).

Quick example program:

Python

name = "Anshuman" age = 25 height = 5.7 is_learning = True print("My name is", name) print("I am", age, "years old") print("Height:", height, "feet") print("Learning Python? Yes!", is_learning)

Output:

text

My name is Anshuman I am 25 years old Height: 5.7 feet Learning Python? Yes! True

Tip: Use meaningful names like total_price instead of x — makes code easier to read!

2.2 Numbers (int, float, complex)

Python handles numbers very easily.

int (Integer): Unlimited size (no overflow like in some languages).

Python

a = 100 b = -45 big_number = 12345678901234567890 # very large – no problem! print(a + b) # 55 print(a * 10) # 1000

float (Floating point): Decimals with approximate precision.

Python

pi = 3.14159 temperature = -2.5 print(pi * 2) # 6.28318

complex (Complex numbers): Used in engineering/math (real + imaginary part).

Python

c = 3 + 4j print(c) # (3+4j) print(c.real) # 3.0 print(c.imag) # 4.0 print(c.conjugate()) # (3-4j)

Common Operations:

Python

x = 10 y = 3 print(x + y) # 13 addition print(x - y) # 7 subtraction print(x y) # 30 multiplication print(x / y) # 3.333... division (always float) print(x // y) # 3 floor division (integer result) print(x % y) # 1 remainder (modulo) print(x * y) # 1000 power (x to the power y)

Tip: // is useful for integer results, % for checking even/odd (num % 2 == 0 → even).

2.3 Strings – Everything You Need to Know

Strings are sequences of characters (text). Defined using single quotes ' ' or double quotes " ".

Basic examples:

Python

greeting = "Hello, World!" name = 'Anshuman' multi_line = """This is a multi-line string in Python!""" print(greeting) # Hello, World! print(multi_line)

String Concatenation (joining):

Python

first = "Hello" second = "Python" print(first + " " + second) # Hello Python print(first * 3) # HelloHelloHello

f-strings (Best way – Python 3.6+): Super easy formatting!

Python

name = "Anshuman" age = 25 print(f"My name is {name} and I am {age} years old.") # My name is Anshuman and I am 25 years old.

Escape Characters: Use \ for special characters.

Python

print("He said, \"Python is fun!\"") # quotes inside print("Line1\nLine2") # new line print("Tab\tHere") # tab space print("Backslash \\") # print backslash

String Length & Access:

Python

text = "Python" print(len(text)) # 6 print(text[0]) # P (first character) print(text[-1]) # n (last character) print(text[1:4]) # yth (slicing: from index 1 to 3)

Tip: Strings are immutable — you can't change a character directly (e.g., text[0] = 'J' → error).

2.4 Boolean (True / False)

Booleans represent truth values — only two possibilities: True or False.

Used heavily in conditions/loops (later sections).

Python

is_adult = True has_python = False print(is_adult) # True print(type(is_adult)) # <class 'bool'> # Comparisons give booleans print(10 > 5) # True print(5 == 10) # False print("a" != "b") # True

Boolean from other types:

  • Empty string/list → False

  • Non-empty → True

  • 0 → False, non-zero → True

Python

print(bool(0)) # False print(bool(100)) # True print(bool("")) # False print(bool("hi")) # True

2.5 type() function and Type Conversion

type() tells you the data type of any value/variable.

Python

x = 42 print(type(x)) # <class 'int'> y = 3.14 print(type(y)) # <class 'float'> z = "Hello" print(type(z)) # <class 'str'>

Type Conversion (Casting): Convert one type to another.

Python

# str → int num_str = "100" num = int(num_str) print(num + 50) # 150 # int/float → str age = 25 print("Age: " + str(age)) # Age: 25 # float → int (removes decimal) price = 99.99 print(int(price)) # 99 # str → float height = "5.7" print(float(height)) # 5.7

Common conversions:

  • int() → to integer

  • float() → to float

  • str() → to string

  • bool() → to boolean

Warning: Invalid conversion gives error, e.g., int("hello") → ValueError.

2.6 Comments in Python (# and multiline)

Comments explain your code — ignored by Python.

Single-line comment:

Python

# This is a single-line comment print("Hello") # This prints Hello (inline comment)

Multi-line comments: Use triple quotes (technically a string, but unused → acts as comment)

Python

""" This is a multi-line comment. You can write many lines here. Useful for explaining functions or sections. """ # OR use # on each line # Line 1 # Line 2

Best Practice:

  • Use comments to explain why (not what the code does)

  • Keep code self-explanatory with good variable names

  • Use docstrings (triple quotes) inside functions/classes later

Example with comments:

Python

# Program to calculate area of rectangle length = 10 # length in cm width = 5 # width in cm area = length * width # calculate area print(f"Area of rectangle: {area} sq cm") # output result

Output:

text

Area of rectangle: 50 sq cm