0% found this document useful (0 votes)
4 views5 pages

Python Notes

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views5 pages

Python Notes

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Python

1. Variable
A variable is a named memory location used to store data.
Assignment & Examples:
x = 10

name = "Python"

Print(x, name)

You can assign multiple values in one line:


a, b, c = 5, 3.2, "Hello"

Variables can be reassigned to different values:


department_name = "CS"

department_name = "CS & AI"

2. Identifiers & Keywords


Identifiers are names for variables, functions, classes, etc.

 Must start with a letter or underscore


 Case-sensitive
 Cannot be Python keywords
 No spaces or special symbols like !, @, #
Keywords are reserved words used to define Python syntax and cannot be used as identifiers.
Examples of keywords:
False, await, else, import, for, def, class, with, yield, etc.

3. Tokens
Tokens are the smallest building blocks of a Python program.
Types of Tokens:

 Identifiers
 Keywords
 Literals (constants like numbers, strings)
 Operators (+, -, *, /)
 Delimiters ((), {}, [], :, ;)
4. Interactive vs Script Mode
Interactive Mode: Run commands in the Python shell with immediate output.
Script Mode: Write code in .py and run it as a program.

5. Data Types

Python data types include:

 Numeric: int, float, complex


 String: str
 Sequence: list, tuple, range
 Mapping: dict
 Boolean: bool
Everything is an object—data types are classes, and variables are their instances.

6. Difference: List vs Tuple


List ([]): Ordered, mutable, supports duplicates, flexible.
Tuple (()): Ordered, immutable, faster and safer when data shouldn't change.
Examples:
list1 = [1, 2, 3] # mutable

tuple1 = (1, 2, 3) # immutable


7. Compiler vs Interpreter
Compiler: Translates whole program; faster execution but error detection is post-compilation.
Interpreter: Executes line-by-line; slower but errors are shown instantly.
Python uses an interpreter (CPython).

8. Comments
Single-line: # This is a comment
Multi-line:
'''

This is

a multi-line comment

'''

9. Type Conversion
 Implicit: Automatic type conversion (e.g., int + float → float)
x = 10

y = 3.5

print(x + y) # 13.5

 Explicit: Using int(), float(), str()


a = "100"

b = int(a)

print(b + 50) # 150

10. Operators
Arithmetic: + - * / % // **
Relational: > < == != <= >=
Logical: and or not
Assignment: = += -= *= /=
Bitwise: & | ^ ~ << >>
Membership: in, not in
Identity: is, is not

11. Input Syntax


Variable = input("Enter something: ")

Example:
name = input ("Enter your name: ")

print ("Hello", name)

num = int (input("Enter a number: "))

print ("Square:", num * num)

12. Control Flow Syntax


 If-Else
age = 18

if age >= 18:

print("Eligible to vote")

else:

print("Not eligible")

 For Loop
for i in range(1, 6):

print(i)

 While Loop
n = 1

while n <= 5:

print(n)

n += 1
13. Sample Programs
 Hello, World!
print("Hello, world!")

 Addition / Multiplication / Division


a, b = 10, 5

print("Addition:", a + b)

print("Multiplication:", a * b)

print("Division:", a / b)

 Odd or Even
n = int(input("Enter a number: "))

if n % 2 == 0:

print("Even")

else:

print("Odd")

 Prime Check
n = 7

flag = True

for i in range(2, n):

if n % i == 0:

flag = False

break

print("Prime" if flag else "Not Prime")

 Armstrong Number
n = 153

s = 0

temp = n

while temp > 0:

digit = temp % 10

s += digit ** 3

temp //= 10

print("Armstrong" if s == n else "Not Armstrong")

You might also like