Friday, December 15, 2023

LAST Minutes PYTHON NOTES FOR PGT CS

 

Last Minutes Notes for Python Basics



  1. Variables and Data Types:
    • Variables store data values.
    • Common data types: int, float, str, bool.

  2. Example:
  3. name = "John"
    age = 25
    height = 5.9
    is_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, and else for conditional statements.
  • for and while for loops.
if age < 18:
print("Minor")
elif 18 <= age < 65:
print("Adult")
else:
print("Senior")

Functions:

  1. 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:

  1. Classes and Objects:

  1. 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:

  1. 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]})

      • Matplotlib:
        • For data visualization.
        • import matplotlib.pyplot as plt plt.plot([1, 2, 3, 4], [2, 4, 6, 8]) plt.show()


Feel free to ask for more specific information or examples!

Dr.Poonam

No comments:

Post a Comment

Input Text and Display in tkinter

  import tkinter as tk def display_text():     text = entry.get()     label.config(text="You entered: " + text) root = tk.Tk() roo...