Python
Keywords: Python keywords are reserved words with special meanings that cannot be
used as variable names.
False await else import pass
None break except in raise
True class finally is return
and continue for lambda try
as def from nonlocal while
assert del global not with
async elif if or yield
Literal: A literal is a fixed value assigned to a variable. It can be of different types:
String Literal: "Hello", 'Python'
Numeric Literal: 10, 3.14
Boolean Literal: True, False
None Literal: None
Constant: In Python, constants are variables whose values should not change during
program execution. Python does not have built-in constant declaration like other languages
(e.g., const in C++), but by convention, constants are written in UPPERCASE.
PI = 3.14159
GRAVITY = 9.81
SPEED_OF_LIGHT = 299792458 # in m/s
Numbers: Python supports different types of numeric data:
Integer (int): 10, -5, 1000
Floating-point (float): 3.14, -0.01, 2.0
Complex (complex): 2 + 3j, -4j
Variable: A variable is a named storage for a value.
Example: x = 10
name = "Alice"
Data type: Python has various data types:
int (Integer)
float (Floating-point)
str (String)
bool (Boolean)
list, tuple, set, dict (Collections)
Type conversion: Conversion between different data types:
Implicit conversion: Done automatically by Python
x=5 # int
y = 2.5 # float
z = x + y # float (Python converts int to float)
Explicit conversion: Done using functions like int(), float(), str()
a = "123"
b = int(a) # Converts string to integer
String: A string is a sequence of characters:
s = "Hello, World!"
You can use single ' or double " quotes.
Escape Sequence: Escape sequences allow special characters in strings:
print("Hello\nWorld") # New line
print("Tab\tSpace") # Tab space
print("He said \"Hello\"") # Double quote inside string
Expression: An expression is a combination of variables, constants, and operators that
evaluates to a value.
Example: x = 10 + 5 * 2
Operator: Operators perform operations on variables:
Arithmetic: +, -, *, /, //, %, **
Comparison: ==, !=, <, >, <=, >=
Logical: and, or, not
Bitwise: &, |, ^, ~, <<, >>
Assignment: =, +=, -=, *=, /=
Evaluation order: Python follows PEMDAS (Parentheses, Exponents,
Multiplication/Division, Addition/Subtraction) for evaluating expressions.
result = 5 + 2 * 3 # Multiplication first, then addition → 5 + 6 = 11
Indentation: Python uses indentation to define blocks of code.
if True:
print("Indented correctly") # Indented block
Incorrect indentation will result in an error.
Data input and output function:
Input (input()): name = input("Enter your name: ")
Output (print()): print("Hello, World!")
print(f"Your name is {name}")
Comments:
Single-line comment: # This is a comment
Multi-line comment:
"""
This is a multi-line comment
in Python.
"""
Conditional Statements: Conditional statements allow the program to make decisions
based on certain conditions.
if Statement: Executes a block of code if the condition is True.
age = 18
if age >= 18:
print("You are eligible to vote")
Output:
You are eligible to vote
if-else Statement: Executes one block if the condition is True, otherwise executes another
block.
age = 16
if age >= 18:
print("You can vote")
else:
print("You cannot vote yet")
Output:
You cannot vote yet
Nested if-else Statement: An if statement inside another if statement.
num = 10
if num > 0:
if num % 2 == 0:
print("Positive Even Number")
else:
print("Positive Odd Number")
else:
print("Negative Number")
Output:
Positive Even Number
Iterative Statements (Loops): Loops allow repetitive execution of a block of
code.
for Loop: Executes a block of code multiple times based on a sequence.
for i in range(5): # Loop from 0 to 4
print("Iteration:", i)
Output:
Iteration: 0
Iteration: 1
Iteration: 2
Iteration: 3
Iteration: 4
while Loop: Executes a block of code as long as a condition is True.
count = 0
while count < 5:
print("Count:", count)
count += 1
Output:
Count: 0
Count: 1
Count: 2
Count: 3
Count: 4
Nested Loops: A loop inside another loop.
for i in range(3): # Outer loop
for j in range(2): # Inner loop
print(f"i={i}, j={j}")
Output:
i=0, j=0
i=0, j=1
i=1, j=0
i=1, j=1
i=2, j=0
i=2, j=1
Control Statements: Control statements modify the flow of loops.
break Statement: Exits the loop immediately.
for i in range(5):
if i == 3:
break
print(i)
Output:
0
1
2
continue Statement: Skips the current iteration and moves to the next one.
for i in range(5):
if i == 3:
continue
print(i)
Output:
0
1
2
4
lambda Function: A small anonymous function using lambda keyword.
lambda Function Example:
square = lambda x: x * x
print(square(5)) # Output: 25
More Complex Example:
max_num = lambda a, b: a if a > b else b
print(max_num(10, 20)) # Output: 20