Python College Level
Python College Level
• Example:
• print('Hello, World!')
Variables and Data Types
• Variables store data. Python is dynamically
typed.
• Types: int, float, str, bool.
• Example:
• x = 10
• name = 'Alice'
Operators
• Arithmetic: +, -, *, /
• Comparison: ==, !=, >
• Logical: and, or, not
• Assignment: =, +=, etc.
Control Statements
• Use if, else, elif for decisions.
• Example:
• age = 18
• if age >= 18:
• print('Adult')
Loops
• for and while used for repetition.
• Example:
• for i in range(5): print(i)
• while count < 5: print(count)
Functions
• Block of reusable code.
• Example:
• def greet(name): return 'Hello ' + name
Data Structures
• List: [1,2,3] | Tuple: (1,2)
• Set: {1,2} | Dict: {'a':1}
• Each has unique properties.
Strings & Methods
• Strings use methods
like .upper(), .lower(), .replace(), .split().
• Example:
• 'str'.upper()
File Handling
• Open files to read/write.
• Example:
• f = open('file.txt', 'w')
• f.write('Hello')
• f.close()
OOP in Python
• Use class & objects.
• Example:
• class Person:
• def __init__(self,name):
• self.name = name
Modules & Packages
• Modules: Python files
• Packages: Collection of modules
• Example:
• import math
• math.sqrt(16)
Exception Handling
• Use try-except to handle errors.
• Example:
• try:
• x = 1/0
• except:
• print('Error')
Sample Programs
• 1. Swap: x, y = y, x
• 2. Even/Odd:
• print('Even' if num % 2 == 0 else 'Odd')