This study note document covers a Python Revision Tour based on the CBSE syllabus for Computer Science. This guide will break down complex concepts into manageable sections, providing examples, tips, and notes to aid in understanding.
Python is an interpreted, high-level, general-purpose programming language. It emphasizes code readability with its notable use of significant whitespace.
Variables in Python do not require explicit declaration to reserve memory space. The declaration happens automatically when you assign a value to a variable.
Example
x = 5 # integer
y = "Hello" # string
z = 3.14 # float
Tip
Python variables are case-sensitive. For example, var
and Var
would be considered different variables.
Example
a = 10
b = 3
print(a + b) # 13
print(a - b) # 7
print(a * b) # 30
print(a / b) # 3.333...
print(a % b) # 1
print(a ** b) # 1000
print(a // b) # 3
<$
Example
a = 5
b = 10
print(a == b) # False
print(a != b) # True
print(a > b) # False
print(a
< b) # True
Python uses if
, elif
, and else
statements for decision-making.
Example
x = 10
if x > 0:
print("Positive number")
elif x == 0:
print("Zero")
else:
print("Negative number")
Note
Indentation is crucial in Python. It defines the scope of loops, functions, and conditional blocks.
for
LoopThe for
loop in Python is used to iterate over a sequence (like a list, tuple, or string).
Example
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)
while
LoopThe while
loop executes as long as the condition is true.
Example
i = 1
while i
< 6:
print(i)
i += 1
Common Mistake
Forgetting to update the loop variable in a while
loop can lead to an infinite loop.
Functions in Python are defined using the def
keyword.
Example
def greet(name):
print(f"Hello, {name}")
greet("Alice")
Functions can return values using the return
statement.
Example
def add(a, b):
return a + b
result = add(2, 3)
print(result) # 5
Lists are ordered, mutable collections of items.
Example
my_list = [1, 2, 3, "a", "b", "c"]
print(my_list[0]) # 1
my_list.append("d")
print(my_list) # [1, 2, 3, 'a', 'b', 'c', 'd']
Tuples are ordered, immutable collections of items.
Example
my_tuple = (1, 2, 3, "a", "b", "c")
print(my_tuple[0]) # 1
Dictionaries are unordered collections of key-value pairs.
Example
my_dict = {"name": "Alice", "age": 25}
print(my_dict["name"]) # Alice
my_dict["age"] = 26
print(my_dict) # {'name': 'Alice', 'age': 26}
Example
with open('example.txt', 'r') as file:
content = file.read()
print(content)
Example
with open('example.txt', 'w') as file:
file.write("Hello, World!")
Tip
Always close the file after performing file operations to free up system resources. Using with open
automatically closes the file.
This guide has covered a comprehensive revision of Python basics, including syntax, control structures, functions, and data structures. Understanding these concepts is crucial for mastering Python and applying it to solve real-world problems.
Note
Practice is key to becoming proficient in Python. Write and execute code snippets to reinforce your understanding.