Python Basics - Class 11 Computer Science
1. Introduction to Python
Python is a high-level, interpreted programming language that is easy to learn and widely used in
various fields.
Features of Python:
- Simple & Easy to Learn
- Interpreted Language (line-by-line execution)
- Dynamically Typed (no need to declare variable types)
- Open Source & Free
- Platform-Independent (runs on Windows, macOS, and Linux)
2. Python Syntax and Variables
Python uses indentation instead of {} or ; like other languages.
Example:
if 10 > 5:
print("10 is greater than 5")
Variables store values:
name = "Alice"
age = 16
height = 5.7
3. Data Types in Python
- int: 10, -5, 100 (whole numbers)
- float: 3.14, -2.5 (decimal numbers)
- str: "Hello", 'Python' (text)
- bool: True, False (boolean values)
- list: [1, 2, 3] (ordered, mutable collection)
- tuple: (4, 5, 6) (ordered, immutable collection)
- dict: {"name": "Bob", "age": 20} (key-value pairs)
4. Input and Output in Python
name = input("Enter your name: ")
print("Hello, " + name)
5. Conditional Statements
age = int(input("Enter your age: "))
if age >= 18:
print("You are an adult.")
elif age >= 13:
print("You are a teenager.")
else:
print("You are a child.")
6. Loops in Python
For loop:
for i in range(1, 6):
print(i)
While loop:
x=1
while x <= 5:
print(x)
x += 1
7. Functions in Python
def greet(name):
print("Hello,", name)
greet("Alice")
8. Lists and Tuples
Lists (Mutable):
fruits = ["Apple", "Banana", "Cherry"]
fruits.append("Mango")
Tuples (Immutable):
colors = ("Red", "Green", "Blue")
9. Dictionaries (Key-Value Pairs)
student = {"name": "Alice", "age": 16, "class": "11th"}
print(student["name"])
10. File Handling in Python
Writing to a file:
with open("file.txt", "w") as f:
f.write("Hello, Python!")
Reading from a file:
with open("file.txt", "r") as f:
content = f.read()
print(content)
Conclusion
Python is a powerful yet easy-to-learn programming language. Understanding the basics such as
syntax, variables, loops, functions, and file handling is essential for Class 11 Computer Science.