Skip to content

Latest commit

 

History

History
212 lines (149 loc) · 2.32 KB

File metadata and controls

212 lines (149 loc) · 2.32 KB

Python Concepts Notes (Simple Format)

VARIABLES

  • Used to store data.
  • No need to declare type.
  • Syntax:
x = 10
name = "Shreyas"

DATA TYPES

  • int → whole numbers
  • float → decimals
  • str → text
  • bool → True/False
  • list → ordered collection
  • tuple → fixed collection
  • dict → key-value pairs
  • set → unique items

TYPE CASTING

  • Convert one type to another.
int("5")
float(10)
str(20)

INPUT & OUTPUT

  • Input always comes as string.
name = input("Enter name: ")
print(name)

OPERATORS

  • Arithmetic: +, -, *, /, %, //, **
  • Comparison: ==, !=, >, <, >=, <=
  • Logical: and, or, not
  • Assignment: =, +=, -=, *=, /=

IF CONDITIONS

  • Used for decision-making.
if condition:
    # code
elif condition:
    # code
else:
    # code

LOOPS

FOR LOOP

  • Used when you know how many times to repeat.
for i in range(5):
    print(i)

WHILE LOOP

  • Runs until condition becomes false.
while condition:
    # code

BREAK & CONTINUE

  • break → stop loop
  • continue → skip iteration

LISTS

  • Ordered, changeable.
arr = [1, 2, 3]
arr.append(4)
arr.remove(2)

TUPLES

  • Ordered, unchangeable.
t = (10, 20, 30)

SETS

  • Unordered, unique items.
s = {1, 2, 2, 3}

DICTIONARIES

  • Key-value pairs.
student = {"name": "Shreyas", "age": 21}
print(student["name"])

FUNCTIONS

  • Reusable blocks of code.
def add(a, b):
    return a + b

LAMBDA

  • Small anonymous function.
square = lambda x: x*x

MODULES

  • Reuse code from other files.
import math

FILE HANDLING

f = open("data.txt", "r")
data = f.read()
f.close()

EXCEPTION HANDLING

try:
    x = 10/0
except ZeroDivisionError:
    print("Cannot divide")

CLASSES & OBJECTS

class Car:
    def __init__(self, brand):
        self.brand = brand

c = Car("BMW")

INHERITANCE

class A:
    pass
class B(A):
    pass

OOP CONCEPTS

  • Class
  • Object
  • Encapsulation
  • Abstraction
  • Inheritance
  • Polymorphism

LIST COMPREHENSION

nums = [x for x in range(5)]

GENERATORS

def gen():
    yield 1
    yield 2

PACKAGES

pip install package_name