1/31
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
1. DATA TYPES IN PYTHON
- Python has several built-in data types, categorized as follows:
int
A) NUMERIC TYPES
– Whole numbers (e.g., 10, -3, 1000)
float
A) NUMERIC TYPES
– Decimal numbers (e.g., 3.14, -0.001, 2.0)
complex
A) NUMERIC TYPES
– Numbers with real and imaginary parts (e.g., 3 + 4j)
str
B) SEQUENCE TYPES
– String (e.g., "Hello, World!")
list
B) SEQUENCE TYPES
– Ordered, mutable collection (e.g., [1, 2, 3, "hello"])
tuple
B) SEQUENCE TYPES
– Ordered, immutable collection (e.g., (10, 20, 30))
range
B) SEQUENCE TYPES
– Sequence of numbers (e.g., range(5) → 0, 1, 2, 3, 4)
set
C) SET TYPES
– Unordered collection of unique items (e.g., {1, 2, 3, 4})
frozenset
C) SET TYPES
– Immutable version of a set
dict
D) MAPPING TYPE
– Key-value pairs (e.g., {"name": "Alice", "age": 25})
bool
E) BOOLEAN TYPE
– Logical values (True, False)
None
F) NONE TYPE
– Represents a null value
2. OPERATORS IN PYTHON
- Python has various operators to perform operations
ARITHMETIC OPERATORS
• + (Addition) → 3 + 2 = 5
• - (Subtraction) → 7 - 4 = 3
• * (Multiplication) → 5 2 = 10
• / (Division) → 10 / 2 = 5.0
• // (Floor Division) → 10 // 3 = 3
• % (Modulus) → 10 % 3 = 1
• (Exponentiation) → 2 3 = 8
COMPARISON OPERATORS
• == (Equal to)
• != (Not equal to)
• > (Greater than)
• < (Less than)
• >= (Greater than or equal to)
• <= (Less than or equal to)
and
C) LOGICAL OPERATORS
(Both conditions must be true)
or
C) LOGICAL OPERATORS
(At least one condition must be true)
not
C) LOGICAL OPERATORS
(Reverses the Boolean value)
BITWISE OPERATORS
• & (AND)
• | (OR)
• ^ (XOR)
• ~ (NOT)
• << (Left Shift)
• >> (Right Shift)
in, not in
F) MEMBERSHIP AND IDENTITY OPERATORS
(Check membership in a sequence)
is, is not
F) MEMBERSHIP AND IDENTITY OPERATORS
(Check object identity)
INPUT STATEMENTS
• Used to take user input
• input() always returns a string
• Example:
name = input("Enter your name: ")
age = int(input("Enter your age: ")) # Convert string to int
print(f"Hello, {name}. You are {age} years old.")
OUTPUT STATEMENTS
• print() is used to display output
• Example:
print("Hello, World!")
print("The sum is:", 10 + 5)
print(f"The sum of {10} and {5} is {10 + 5}") # f-string
IF STATEMENT
• Executes a block of code only if a condition is true
• Example:
num = int(input("Enter a number: "))
if num > 0:
print("Positive number")
IF-ELSE STATEMENT
• Provides an alternative block of code if the condition is false
• Example:
num = int(input("Enter a number: "))
if num > 0:
print("Positive number")
else:
print("Non-positive number")
IF-ELIF-ELSE STATEMENT
• Allows multiple conditions to be checked sequentially
• Example:
num = int(input("Enter a number: "))
if num > 0:
print("Positive number")
elif num < 0:
print("Negative number")
else:
print("Zero")
FOR LOOP
• Used to iterate over sequences
• Example:
for i in range(5):
print(i) # Prints 0 to 4
WHILE LOOP
• Repeats a block of code as long as a condition is true
• Example:
num = 5
while num > 0:
print(num)
num -= 1 # Decrease num by 1
break
C) LOOP CONTROL STATEMENTS
– Exits the loop
Example:
for i in range(10): if i == 5:
break # Stops the loop when i is 5
print(i)
continue
C) LOOP CONTROL STATEMENTS
– Skips the current iteration and moves to the next
Example:
for i in range(10):
if i % 2 == 0:
continue # Skips even numbers print(i)
pass
C) LOOP CONTROL STATEMENTS
– Placeholder for future code