🐍Python Programming
Python Basics & Syntax
Variables, data types, operators, input/output, indentation, and Python code structure.
BeginnerIntermediateAdvanced
What You Will Learn in Python Basics & Syntax
Python is a high-level, interpreted, dynamically typed language known for its clean, readable syntax that uses indentation to define code blocks.
- Dynamic typing: no type declarations; variables are assigned types at runtime.
- Indentation (4 spaces standard) defines code blocks — no braces needed.
- Everything in Python is an object, including functions and classes.
- Built-in types: int, float, str, bool, list, tuple, dict, set, None.
- `print()` for output; `input()` for user input (always returns string).
- Multiple assignment: `a, b = 5, 10` or `a = b = 0`.
Syntax
# Variables and data types
variable = value # dynamic typing
x, y = 10, 20 # multiple assignment
# Input/Output
name = input("Enter name: ")
print(f"Hello, {name}!") # f-string (Python 3.6+)
# Type conversion
n = int(input()) # convert string input to intComplete Code Example
# Basic Python program
name = input("Enter your name: ")
age = int(input("Enter your age: "))
gpa = float(input("Enter your GPA: "))
print(f"Name: {name}")
print(f"Age in 5 years: {age + 5}")
print(f"GPA: {gpa:.2f}") # 2 decimal places
# Multiple assignment
a, b = 10, 20
a, b = b, a # swap without temp variable
print(f"a={a}, b={b}") # a=20, b=10Example
`x = 5; y = 3.14; name = 'Python'` — Python infers int, float, str automatically.
Expected Exam Questions — Python Basics & Syntax
Q1.What is the difference between `//` and `/` in Python?
Answer: `/` performs true (float) division: `7/2 = 3.5`. `//` performs floor division (integer part): `7//2 = 3`. For negative: `-7//2 = -4` (rounds towards negative infinity).
Q2.What are Python's mutable and immutable types?
Answer: Immutable (cannot change after creation): int, float, str, tuple, bool, frozenset. Mutable (can be modified in-place): list, dict, set. Strings are immutable — `s[0] = 'a'` raises TypeError.
Q3.What is an f-string? Give an example.
Answer: F-strings (formatted string literals, Python 3.6+) embed expressions inside `{}`: `name='Alice'; print(f'Hello, {name}!')`. Supports expressions: `f'{2*3}'` = `'6'`. Replaces `.format()` and `%` formatting.
Q4.How does `is` differ from `==` in Python?
Answer: `==` checks value equality. `is` checks identity (same memory object). `a = [1,2]; b = [1,2]; a==b` is True, but `a is b` is False. For small integers (-5 to 256) Python caches objects, so `a=5; b=5; a is b` may be True.
Q5.What is PEP 8?
Answer: PEP 8 is Python's official style guide. Key rules: 4-space indentation, max 79 chars per line, snake_case for variables/functions, PascalCase for classes, two blank lines before top-level functions/classes.
🔘 MCQ Practice — Python Basics & Syntax
MCQ 1.What is the output of `print(type(5/2))` in Python 3?
A. <class 'int'>
B. <class 'float'> ✓
C. <class 'double'>
D. Error
✓ Correct Answer: <class 'float'>
MCQ 2.Which of the following is immutable in Python?
A. list
B. dict
C. tuple ✓
D. set
✓ Correct Answer: tuple
Download Python Basics & Syntax PDF Notes
Get the complete Python Basics & Syntax notes as a PDF — free for enrolled students, or browse our public study materials library.