Python Syntax & Variables
Foundational programming syntax, identifiers, variables, data types. Aligned with neural network training simulator.
Python Syntax & Variables
Python Syntax & Variables
What you'll learn
- Python's basic syntax rules and how to write clean code.
- How to declare and use variables and understand data types.
- Input/output with
print()andinput(). - Type conversion and basic operators.
Key concepts
Python Basics
- Python is interpreted — runs line by line; no compilation needed.
- Indentation is syntax: blocks are defined by spaces (not
{}). - Case-sensitive:
Name≠name. - Comments:
# single lineor"""docstring""".
Variables
name = "Alice" # str
age = 17 # int
gpa = 9.5 # float
is_student = True # bool
- No type declaration needed (dynamic typing).
- Variable names: letters, digits, underscores; cannot start with digit.
- Convention:
snake_casefor variables and functions.
Data Types
| Type | Example | Notes |
|---|---|---|
int | 42, -7 | Whole numbers |
float | 3.14, -0.5 | Decimal numbers |
str | "hello", 'world' | Text; immutable |
bool | True, False | Capitalized in Python |
NoneType | None | Represents no value |
Type Conversion
int("42") # → 42
float("3.14") # → 3.14
str(100) # → "100"
bool(0) # → False (0, "", None, [] are falsy)
Input / Output
name = input("Enter your name: ") # always returns str
print("Hello,", name)
print(f"Hello, {name}!") # f-string (preferred)
Arithmetic Operators
| Operator | Meaning | Example | Result |
|---|---|---|---|
+ | Addition | 5 + 3 | 8 |
- | Subtraction | 10 - 4 | 6 |
* | Multiplication | 3 * 4 | 12 |
/ | Division (float) | 7 / 2 | 3.5 |
// | Floor division | 7 // 2 | 3 |
% | Modulus (remainder) | 7 % 2 | 1 |
** | Exponentiation | 2 ** 3 | 8 |
Comparison & Logical Operators
x == y # Equal
x != y # Not equal
x > y # Greater than
x >= y # Greater than or equal
# Logical
x and y
x or y
not x
String Operations
s = "Python"
len(s) # 6
s[0] # 'P' (indexing)
s[-1] # 'n' (last char)
s[1:4] # 'yth' (slicing)
s.upper() # 'PYTHON'
s.lower() # 'python'
s + " 3" # 'Python 3' (concatenation)
"Py" in s # True
Multiple Assignment
a, b, c = 1, 2, 3
x = y = z = 0
a, b = b, a # swap
Quick check
- What is the difference between
/and//in Python? - Write code to take two numbers as input and print their sum.
- What is the output of
bool("")andbool(1)? - What is an f-string? Show an example.
- What does
7 % 3evaluate to?
Open the Practice tab for graded questions on Python Syntax & Variables.
Key Takeaways (TL;DR)
- What you'll learn
- Key concepts
- Quick check
Master this topic with Drishti OS
Get unlimited mock tests, AI-powered mentorship, and complete video courses when you join.
Start Free Practice