Last Minutes Notes for Python Basics
- Variables and Data Types:
- Variables store data values.
- Common data types: int, float, str, bool.
- Example:
- name = "John"age = 25height = 5.9is_student = False
Lists and Tuples:
- Lists are mutable, tuples are immutable.
- Indexing starts at 0.
my_list = [1, 2, 3, 4]
my_tuple = (1, 2, 3, 4)
Control Flow:
if,elif, andelsefor conditional statements.forandwhilefor loops.
if age < 18:
print("Minor")
elif 18 <= age < 65:
print("Adult")
else:
print("Senior")
Functions:
- 1.Defining Functions:
def greet(name):
print(f"Hello, {name}!")
greet("Alice")
2.Return Statement:
def square(x):
return x ** 2
result = square(5)
Advanced Concepts:
- Classes and Objects:
- class Dog: def __init__(self, name, age): self.name = name self.age = age def bark(self): print("Woof!") my_dog = Dog("Buddy", 3) my_dog.bark()
Inheritance:
class Student(Person):
def __init__(self, name, age, student_id):
super().__init__(name, age)
self.student_id = student_id
Exception Handling:
try:
result = 10 / 0
except ZeroDivisionError:
print("Cannot divide by zero!")
finally:
print("This code always runs.")
File Handling:
# Reading from a file
with open("example.txt", "r") as file:
content = file.read()
# Writing to a file
with open("example.txt", "w") as file:
file.write("Hello, World!")
External Libraries:
- NumPy:
- For numerical operations.
- import numpy as np array = np.array([1, 2, 3, 4])
- Pandas:
- For data manipulation and analysis.
- import pandas as pd df = pd.DataFrame({"Name": ["Alice", "Bob"], "Age": [25, 30]})
- For data visualization.
- import matplotlib.pyplot as plt plt.plot([1, 2, 3, 4], [2, 4, 6, 8]) plt.show()
Matplotlib:
Feel free to ask for more specific information or examples!
Dr.Poonam
No comments:
Post a Comment