Python Basics - Class 11 Computer Science
1. Introduction to Python
Python is a high-level, interpreted programming language that is simple and widely used in various
fields.
Features of Python:
- Simple & Easy to Learn
- Interpreted (Line-by-Line Execution)
- Dynamically Typed (No Type Declaration Needed)
- 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')
3. Data Types in Python
Common Data Types:
- int: Whole Numbers
- float: Decimal Numbers
- str: Text
- bool: True or False
- list: Ordered, Mutable Collection
- tuple: Ordered, Immutable Collection
- dict: Key-Value Pairs
4. Conditional Statements
Python Basics - Class 11 Computer Science
Example:
if age >= 18:
print('You are an adult.')
elif age >= 13:
print('You are a teenager.')
else:
print('You are a child.')
5. Loops in Python
For Loop Example:
for i in range(1, 6):
print(i)
While Loop Example:
x=1
while x <= 5:
print(x)
x += 1
6. Functions in Python
def greet(name):
print('Hello,', name)
greet('Alice')
7. Lists and Tuples
Lists (Mutable):
fruits = ['Apple', 'Banana', 'Cherry']
fruits.append('Mango')
Python Basics - Class 11 Computer Science
Tuples (Immutable):
colors = ('Red', 'Green', 'Blue')
8. 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 an easy and powerful programming language. Mastering these basics will help you build
advanced programs in the future.