Basic Python Syntax Summary
1. Expressions
✅ Syntax:
expression = value1 operator value2
💡 Explanation:
- An expression is any valid combination of values, variables, and operators that Python
can evaluate.
- Used in calculations, assignments, conditions, etc.
🔍 Example:
result = 5 + 3
print(result) # Output: 8
2. Variables
✅ Syntax:
variable_name = value
💡 Explanation:
- A variable stores a value (number, string, etc.) that can be used later.
- Variable names must start with a letter or underscore.
🔍 Example:
name = "Nilesh"
age = 30
3. Quotes (Strings)
✅ Syntax:
'string in single quotes'
"string in double quotes"
'''multiline string'''
💡 Explanation:
- Strings are enclosed in quotes and represent text.
- Triple quotes can span multiple lines.
🔍 Example:
s1 = 'Hello'
s2 = "World"
s3 = '''This is
a multiline string'''
4. Basic Math Operations
✅ Syntax:
Addition: +
Subtraction: -
Multiplication: *
Division: /
Integer Division: //
Modulus: %
Exponentiation: **
💡 Explanation:
- Basic arithmetic operators are used to perform operations on numbers.
🔍 Example:
a = 10
b=3
print(a % b) # Output: 1
5. Decision-Making (Conditional Statements)
✅ Syntax:
if condition:
# code block
if condition:
# true block
else:
# false block
if condition1:
# block1
elif condition2:
# block2
else:
# block3
💡 Explanation:
- Used to execute code based on conditions.
- `if`, `if-else`, and `if-elif-else` provide branching logic.
🔍 Example:
age = 20
if age >= 18:
print("Adult")
score = 45
if score >= 50:
print("Pass")
else:
print("Fail")
marks = 75
if marks >= 90:
print("A Grade")
elif marks >= 60:
print("B Grade")
else:
print("C Grade")