Essentials of Python - Unit 1 Revision Sheet
Introduction
- What can Python do?
Web development, AI/ML, automation, data analysis, gaming, etc.
- Why Python?
Easy syntax, readable, portable, vast libraries, interpreted language.
- Python Syntax vs Other Languages
Uses indentation instead of braces {} or semicolons.
- Python Installation
From python.org, install and verify with python --version.
- Basic Elements
- print() -> Display output
- Comments: # single line, ''' multi-line '''
- Data Structures: List, Tuple, Set, Dictionary
- Data Types: int, float, str, bool, complex
- String Operations: Concatenation (+), Slicing ([:]), Methods (.upper(), .lower(), etc.)
- Input & Output
- input() -> takes input as string
- print() -> displays output
- Output Formatting
name = "Priyanshu"
print(f"Hello {name}")
print("Hello {}".format(name))
- Operators
Arithmetic (+, -, *, /, //, %, **)
Relational (>, <, ==, !=, >=, <=)
Logical (and, or, not)
Assignment (=, +=, -=, etc.)
Bitwise, Membership (in, not in), Identity (is, is not)
Python Program Flow
- Indentation: Controls program block (4 spaces standard)
- Decision Making
if condition:
# code
elif condition:
# code
else:
# code
- Loops
while loop
for loop with range()
Break: Exit loop
Continue: Skip iteration
- range() Syntax
range(start, stop, step)
- Assert
assert 5 > 2 # True -> no error
- Loop Example
for i in range(5):
print(i)
Functions & Modules
- Defining a Function
def greet():
print("Hello")
- Parameters: Default, Keyword, Variable-length (*args, **kwargs)
- Scope of Variables: Local & Global
- Function Documentation
def func():
'''This is a docstring'''
pass
- Lambda Functions
square = lambda x: x*x
- map() Function
result = list(map(lambda x: x+2, [1, 2, 3]))
- Modules
Importing modules:
import math
print(math.sqrt(16))
Create your own modules as .py files.