








Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!









These notes cover Python from the basics to advanced concepts in great detail. Whether you're a beginner or an experienced programmer, this guide will help you understand Python’s key concepts and give you the knowledge needed to develop applications, analyze data, or perform machine learning tasks.
Python is a high-level, interpreted, and general-purpose programming language. It was created by Guido van Rossum and first released in
Python's syntax emphasizes readability and simplicity, making it an excellent choice for beginners. Python supports multiple programming paradigms, including procedural, object-oriented, and functional programming.
Easy to Learn: Python is known for its easy-to-read syntax and high-level nature. Interpreted: Python code runs line by line, making it easier to test and debug. Cross-Platform: Python works on different operating systems like Windows, macOS, and Linux. Huge Community: Python has one of the largest programming communities and a wealth of resources, tutorials, and libraries.
Install Python: Download from python.org. IDE Options: Use Integrated Development Environments (IDEs) like VSCode, PyCharm, or Jupyter Notebook for better productivity.
Variables store data values. In Python, you don't need to declare the data type explicitly. x = 5 # Integer name = "John" # String height = 5.9 # Float is_active = True # Boolean
int: Integer numbers (e.g., 5, 100, -3) float: Floating-point numbers (e.g., 3.14, -0.001) str: Strings (e.g., "Hello", "Python") bool: Boolean values (True or False) list: Ordered, mutable collections (e.g., [1, 2, 3]) tuple: Ordered, immutable collections (e.g., (1, 2, 3)) set: Unordered collection of unique elements (e.g., {1, 2, 3}) dict: Dictionary with key-value pairs (e.g., {"name": "John", "age": 25})
Assign values to variables. = # Assign += # Add and assign
Check if two variables refer to the same object in memory. is # Returns True if both variables point to the same object is not # Returns True if both variables do not point to the same object
Check if a value is present in a sequence (list, tuple, string). in # Returns True if value is found in the sequence not in # Returns True if value is not found in the sequence
Used to execute code based on conditions. x = 10 if x > 5: print("x is greater than 5") else: print("x is less than or equal to 5")
Used for checking multiple conditions. x = 10 if x < 5: print("x is less than 5") elif x == 10: print("x is 10") else: print("x is greater than 5")
A shorthand way of writing if-else. x = 5 result = "Yes" if x > 0 else "No"
Functions are blocks of code designed to do a specific task. def greet(name): print(f"Hello, {name}!") greet("Alice") # Output: Hello, Alice!
Functions can accept arguments and return values. def add(a, b): return a + b result = add(2, 3) print(result) # Output: 5
A small anonymous function. square = lambda x: x ** 2 print(square(4)) # Output: 16
A function that calls itself. def factorial(n): if n == 1: return 1 return n * factorial(n - 1)
print(factorial(5)) # Output: 120
Ordered, mutable collections. my_list = [1, 2, 3, 4] my_list.append(5) # Adds an element to the list
Ordered, immutable collections. my_tuple = (1, 2, 3)
Key-value pairs. my_dict = {"name": "Alice", "age": 25} print(my_dict["name"]) # Output: Alice
with open("example.txt", "r") as file: content = file.read() print(content)
A class is a blueprint for creating objects (instances). class Person: def init(self, name, age): self.name = name self.age = age def greet(self): print(f"Hello, {self.name}!") person1 = Person("John", 30) person1.greet() # Output: Hello, John!
A way for a class to inherit attributes and methods from another class. class Employee(Person): def init(
self, name, age, position): super().init(name, age) self.position = position def describe(self): print(f"{self.name} is a {self.position}.") emp = Employee("Alice", 25, "Software Engineer") emp.describe() # Output: Alice is a Software Engineer.
class BankAccount: def __init__(self, balance): self.__balance = balance # Private attribute def deposit(self, amount): self.__balance += amount def get_balance(self): return self.__balance account = BankAccount(1000) account.deposit(500) print(account.get_balance()) # Output: 1500 ## Polymorphism A feature that allows objects of different classes to be treated as objects of a common superclass. class Dog: def speak(self): Functions that modify the behavior of other functions. def decorator(func): def wrapper(): print("Before function call.") func() print("After function call.") return wrapper @decorator def greet(): print("Hello!") greet() # Output: Before function call. Hello! After function call. ## Generators Functions that return an iterator and generate values on the fly using yield. def count_up_to(n): count = 1 while count <= n: yield count count += 1 counter = count_up_to(5) for num in counter: print(num) ## Conclusion These are the fundamental topics you’ll encounter when learning Python. By mastering these concepts, you'll be ready to build applications, analyze data, automate tasks, or explore machine learning and AI. Python is a versatile language, and as you gain more experience, you’ll discover new tools and libraries that can enhance your projects. Keep practicing by working on small projects and coding challenges to solidify your understanding of Python!