[go: up one dir, main page]

0% found this document useful (0 votes)
4 views3 pages

Chapter 1

Chapter 1 provides a comprehensive overview of Python, covering its basics, features, data types, and operators. It explains control flow statements including conditional and looping structures, as well as function types such as built-in, user-defined, and lambda functions. Additionally, the chapter discusses file handling, detailing how to open, read, write, and understand file modes.

Uploaded by

ashim05birbhum
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views3 pages

Chapter 1

Chapter 1 provides a comprehensive overview of Python, covering its basics, features, data types, and operators. It explains control flow statements including conditional and looping structures, as well as function types such as built-in, user-defined, and lambda functions. Additionally, the chapter discusses file handling, detailing how to open, read, write, and understand file modes.

Uploaded by

ashim05birbhum
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Chapter 1: Python Revision Tour (Class 12 Computer Science - CBSE)

1.1 Python Basics Recap

Python is a high-level, interpreted programming language known for its simplicity and
readability.

1.1.1 Python Features:

 Easy to learn and read.


 Interpreted (executes line by line).
 Supports object-oriented and functional programming.
 Extensive libraries for various applications.

1.1.2 Python Keywords and Identifiers:

 Keywords: Reserved words like if, elif, while, import, def, class, etc.
 Identifiers: Variable, function, and class names. Must start with a letter or _, and
cannot be a keyword.

1.2 Data Types & Type Conversion

Data Type Example Description


int 10, -5 Integer values
float 3.14 Decimal values
str "Hello" Text data
bool True, False Boolean values
list [1, 2, 3] Ordered, mutable collection
tuple (1, 2, 3) Ordered, immutable collection
dict {"name": "Alice"} Key-value pairs
set {1, 2, 3} Unordered, unique values

 Type Conversion:

python
Copy
x = int(3.7) # Converts float to int → 3
y = float(10) # Converts int to float → 10.0
z = str(25) # Converts int to string → "25"

1.3 Operators in Python

1. Arithmetic Operators: +, -, *, /, %, // (floor division), ** (exponentiation).


2. Relational Operators: ==, !=, >, <, >=, <=.
3. Logical Operators: and, or, not.
4. Bitwise Operators: &, |, ^, ~, <<, >>.
5. Assignment Operators: =, +=, -=, *=, /=, etc.

Example:

python
Copy
a, b = 10, 20
print(a + b) # 30
print(a > b and b > 15) # False

1.4 Control Flow Statements

1.4.1 Conditional Statements

python
Copy
x = 10
if x > 0:
print("Positive")
elif x == 0:
print("Zero")
else:
print("Negative")

1.4.2 Looping Statements

 For Loop:

python
Copy
for i in range(5):
print(i) # Prints 0 to 4

 While Loop:

python
Copy
x = 5
while x > 0:
print(x)
x -= 1

1.5 Functions in Python

1.5.1 Defining and Calling Functions

python
Copy
def greet(name):
return "Hello, " + name

print(greet("Alice")) # Hello, Alice

1.5.2 Types of Functions


 Built-in Functions: len(), sum(), max(), min(), type(), etc.
 User-defined Functions: Created by the user using def.
 Lambda Functions:

python
Copy
square = lambda x: x * x
print(square(5)) # 25

1.6 File Handling in Python

1.6.1 Opening and Closing Files

python
Copy
f = open("file.txt", "r") # Open in read mode
data = f.read() # Read the content
f.close() # Close the file

1.6.2 Writing to a File

python
Copy
f = open("file.txt", "w")
f.write("Hello, World!")
f.close()

1.6.3 File Modes

Mode Description
r Read mode
w Write mode (Overwrites existing file)
a Append mode (Adds to the existing file)
rb Read binary mode
wb Write binary mode

Summary of Chapter 1

 Python basics, data types, and operators.


 Control flow: if-else, loops.
 Functions: built-in, user-defined, lambda.
 File handling: reading/writing files.

You might also like