Python Notes for Students
1. Introduction to Python
- What is Python?
Python is a high-level, interpreted programming language that is known for its simplicity and readability.
It is widely used in various fields such as web development, data science, artificial intelligence, and autom
- Features of Python:
- Simple and easy to learn: Python's syntax is similar to English, making it beginner-friendly.
- Interpreted: Python code is executed line by line, which makes debugging easier.
- Object-Oriented: Python supports object-oriented programming, which helps in organizing complex prog
- Cross-platform: Python runs on different platforms like Windows, macOS, and Linux.
2. What are Variables?
- A variable is a container used to store data. The value of a variable can change during the execution of a
- In Python, you do not need to declare the type of variable explicitly. The type is inferred from the value as
Example:
age = 25 # 'age' is a variable storing an integer value.
name = "John" # 'name' is a variable storing a string value.
3. Data Types in Python
Python supports three main data types, which define the kind of value a variable can hold:
- int: Integer values (e.g., 10, -5, 0)
- float: Floating-point numbers (e.g., 5.2, -3.14)
- str: String, a sequence of characters (e.g., "Hello")
Example:
num = 10 # int
pi = 3.14 # float
name = "Alice" # str
4. Iterative Statements (Loops)
- Loops allow us to repeat a block of code multiple times.
- For Loop: Used to iterate over a sequence (such as a list or a range of numbers).
Example:
for i in range(5):
print(i) # This will print numbers 0 to 4.
- While Loop: Repeats the block of code as long as the condition is True.
Example:
x=0
while x < 5:
print(x)
x += 1 # This will print numbers 0 to 4.
5. Conditional Statements
- Conditional statements help in decision-making based on conditions.
- If Statement: Executes a block of code if the condition is true.
Example:
age = 18
if age >= 18:
print("You are an adult.")
- If-Else Statement: Executes one block if the condition is true, and another block if the condition is false.
Example:
age = 16
if age >= 18:
print("You are an adult.")
else:
print("You are a minor.")
- Elif (Else If): Used for multiple conditions.
Example:
marks = 75
if marks >= 90:
print("Grade A")
elif marks >= 70:
print("Grade B")
else:
print("Grade C")
6. What are Functions?
- A function is a block of reusable code that performs a specific task. Functions make the code modular and
- You can define a function using the def keyword.
Example:
def greet(name):
print(f"Hello, {name}!")
greet("Alice") # Output: Hello, Alice!
- Return Statement: Functions can also return values using the return statement.
Example:
def add(a, b):
return a + b
result = add(5, 3)
print(result) # Output: 8
7. The print() Function
- The print() function is used to display output on the screen.
Example:
print("Hello, World!") # Output: Hello, World!
8. The input() Function
- The input() function is used to take input from the user. The input is stored as a string by default.
Example:
name = input("Enter your name: ")
print("Hello, " + name)
9. Typecasting in Python
- Typecasting is converting a value from one data type to another.
- Converting to Integer (int):
num_str = "5"
num_int = int(num_str) # Converts string '5' to integer 5
- Converting to Float (float):
num_str = "5.7"
num_float = float(num_str) # Converts string '5.7' to float 5.7
- Converting to String (str):
num = 10
num_str = str(num) # Converts integer 10 to string "10"