Notes-1
Notes-1
h
ng
1 Introduction to Python
Si
Python is a high-level, interpreted programming language known for its simplicity and
readability.
h
1.1 Origins of Python
as
Python was created by Guido van Rossum in 1989 and officially released in 1991. It
was developed as a successor to the ABC programming language. The name Python was
nd
inspired by the British comedy show Monty Python’s Flying Circus.
Key Milestones:
Cross-platform compatibility
Widely used in AI, ML, Web Development, Automation, and Data Science
1
2 Variables and Assignment
Variables store values in memory. Variables in Python are dynamically typed and do not
require explicit declaration and are case-sensitive.
Example:
1 x = 10 # Integer
2 y = 3.14 # Float
3 name = " John " # String
flag = True # Boolean
h
4
Multiple Assignments:
ng
1 a , b , c = 10 , 20 , " Hello "
2 x = y = z = 50 # Assign the same value to multiple variables
Si
Swapping Variables:
1 a , b = 5 , 10
2 a , b = b , a # Swap values without a temporary variable
h
3 print (a , b ) # Output : 10 5
3 Python Basics
as
nd
Python script files have a .py extension. Execution is done via the Python interpreter:
Se
1 python my_script . py
Python syntax is simple and uses indentation to define blocks of code. Python uses
indentation (instead of { } like C/C++). Comments are written using # for single-line
ba
Example:
1 # This is a single - line comment
ga
2
3 """
4 This is a
n
7
8 print ( " Hello , World ! " ) # Output : Hello , World !
2
3.2 Identifiers
Identifiers: Names given to variables, functions, classes, etc.
h
Cannot contain special characters (e.g., Roll#, US$ are invalid)
ng
Cannot use Python keywords (e.g., def, class, return)
Si
4 Standard Types and Other Built-in Types
h
Python has various data types:
as
nd
Numeric Types: int, float, complex
Mutable/Immutable Types:
ga
Example:
1 lst = [1 , 2 , 3 , " Python " ] # List
2 tpl = (10 , 20 , 30) # Tuple
3 st = {1 , 2 , 3 , 3} # Set ( removes duplicates )
4 dct = { " name " : " John " , " age " : 25} # Dictionary
5 Operators in Python
Operators are special symbols used to perform operations on variables and values.
3
5.1 Arithmetic Operators
Addition: + Example: 5 + 3 ⇒ 8
Subtraction: - Example: 10 - 4 ⇒ 6
Multiplication: * Example: 6 * 7 ⇒ 42
h
Floor Division: // Example: 15 // 2 ⇒ 7
ng
Modulus: % Example: 10 % 3 ⇒ 1
Exponentiation: ** Example: 2 ** 3 ⇒ 8
Si
5.2 Relational Operators
Equal to: ==
h
Example: 5 == 5 ⇒ True
as
Example: 5 != 3 ⇒ True
OR: | Example: 5 | 3 ⇒ 7
XOR: ∧ Example: 5 ∧ 3 ⇒ 6
m
NOT: ∼ Example: ∼5 ⇒ -6
ga
Logical Operators: and, or, not Example: (5 > 3) and (10 > 7) ⇒ True
4
6 Built-in Functions
Python provides many built-in functions:
Function Purpose
type(x) Returns type of x
len(x) Returns length of x
max(x), min(x) Find maximum/minimum value
Sorts a sequence
h
sorted(x)
ng
Example:
1 numbers = [4 , 7 , 1 , 9]
2 print ( max ( numbers ) ) # Output : 9
Si
3 print ( min ( numbers ) ) # Output : 1
4 print ( sorted ( numbers ) ) # Output : [1 , 4 , 7 , 9]
h
6.1 Standard Type Built-in Functions
as
Function Purpose
type(x) Returns type of x
nd
len(x) Returns length of x
str(x), int(x), float(x) Type conversion
max(x), min(x) Find maximum/minimum value
Se
sorted(x) Sorts a sequence
m
ba
m
n ga
Pa