Python Programming

 

Python Notes 


1. Introduction to Python

1.1 What is Python?
  • Python is a high-level, interpreted programming language known for its readability and simplicity. It supports multiple programming paradigms, including procedural, object-oriented, and functional programming.
1.2 Features of Python
  • Easy to learn and use
  • Extensive libraries and frameworks
  • Platform-independent
  • Supports object-oriented programming
  • High-level and interpreted

2. Python Syntax

2.1 Variables and Data Types

In Python, you don't need to declare the type of variable explicitly. Python determines the type dynamically.


# Variable assignments x = 5 # Integer y = "Hello" # String z = 3.14 # Float
2.2 Basic Data Types
  • int: Integer numbers.
  • float: Decimal numbers.
  • str: Strings (text).
  • bool: Boolean values (True/False).

a = 10 # Integer b = 3.14 # Float name = "John" # String is_active = True # Boolean
2.3 Input and Output

You can take input using input() and print output using print().


# Taking input name = input("Enter your name: ") age = int(input("Enter your age: ")) # Outputting data print("Hello, " + name + "! You are " + str(age) + " years old.")

3. Control Flow Statements

3.1 Conditional Statements (if, elif, else)

age = 20 if age < 18: print("You are a minor.") elif age >= 18 and age < 65: print("You are an adult.") else: print("You are a senior citizen.")
3.2 Loops
  • for loop: Used to iterate over a sequence (like a list, string, or range).

# Using a for loop for i in range(5): print("This is iteration number", i)
  • while loop: Runs as long as the condition is true.

count = 0 while count < 5: print("Count is", count) count += 1

4. Functions

4.1 Defining Functions

# Function to calculate the square of a number def square(num): return num * num result = square(5) print("The square of 5 is:", result)
4.2 Function with Multiple Parameters

# Function to add two numbers def add_numbers(a, b): return a + b sum_result = add_numbers(10, 15) print("The sum is:", sum_result)

5. Lists and Tuples

5.1 Lists
  • Lists are ordered, mutable, and allow duplicate elements.

# List definition fruits = ["apple", "banana", "cherry"] fruits.append("orange") # Adding an item to the list print(fruits)
5.2 Tuples
  • Tuples are ordered, immutable, and do not allow modification.

# Tuple definition coordinates = (1, 2, 3) print(coordinates[0]) # Accessing tuple elements

6. Dictionaries

  • Dictionaries store key-value pairs.

# Dictionary definition person = { "name": "John", "age": 25, "city": "New York" } # Accessing dictionary values print(person["name"]) # Output: John

7. File Handling

7.1 Opening a File

# Open a file in write mode file = open("example.txt", "w") file.write("Hello, this is a test file.") file.close() # Always close the file after operation
7.2 Reading from a File

# Open a file in read mode file = open("example.txt", "r") content = file.read() print(content) file.close()

8. Object-Oriented Programming (OOP) in Python

8.1 Classes and Objects

# Defining a class class Car: def __init__(self, make, model, year): self.make = make self.model = model self.year = year def display_info(self): print(f"{self.year} {self.make} {self.model}") # Creating an object my_car = Car("Toyota", "Corolla", 2020) my_car.display_info() # Output: 2020 Toyota Corolla
8.2 Inheritance

class ElectricCar(Car): # Inheriting from Car class def __init__(self, make, model, year, battery_size): super().__init__(make, model, year) self.battery_size = battery_size def display_battery(self): print(f"This car has a {self.battery_size}-kWh battery.") # Creating an object of ElectricCar my_electric_car = ElectricCar("Tesla", "Model S", 2021, 85) my_electric_car.display_info() my_electric_car.display_battery()

9. Modules and Libraries

9.1 Importing Built-in Modules

import math # Using math module result = math.sqrt(16) print(result) # Output: 4.0
9.2 Creating Your Own Module
  1. Create a Python file named mymodule.py.
  2. Define functions inside it.

# mymodule.py def greet(name): print(f"Hello, {name}!")
  1. Import and use it in another Python file.

import mymodule mymodule.greet("Alice") # Output: Hello, Alice!

10. Exception Handling

10.1 Try-Except Block

try: result = 10 / 0 except ZeroDivisionError: print("Error: Division by zero is not allowed.")
10.2 Catching Multiple Exceptions

try: value = int(input("Enter a number: ")) except ValueError: print("Error: Invalid input! Please enter a valid number.") except Exception as e: print(f"An error occurred: {e}")

Assessment: Regular Tests, Assignments, and Final Project Evaluation

Certification: Certificate of Completion from Disha Institute

 

Number of Days Depends on your practice and feedback. The more you practice and review your work, the faster you will complete the course.

For Batch time and Fess contact to Below Address and Number.

Zamanat Sir

(MCA / Bsc. I.T / ‘A’ / ‘O’ / CCC / Govt. Certified  Domain Skill Trainer )

   Disha Institute 153 Vijay Nagar Opp. Rg Pg College W.K Road Meerut 

(9411617329 , 9458516690) 

Comments

Popular posts from this blog

VBA Modules for Excel Automation (Advance Excel)

Java Programming

Excel Shortcuts