Python Programming - Module 1 (Simplified Notes)
1. Python Basics
1.1 Using the Python Shell (IDLE)
- IDLE is where we can type and run Python code line-by-line.
- Example:
>>> 2 + 2
1.2 Data Types in Python
- int: Whole numbers (e.g., 10)
- float: Decimal numbers (e.g., 3.14)
- str: Text values in quotes (e.g., 'Hello')
- Examples:
age = 20 # int
pi = 3.14 # float
name = 'Alice' # str
1.3 String Concatenation and Replication
- '+' joins strings: 'Hello' + ' World' -> 'Hello World'
- '*' repeats strings: 'Hi' * 3 -> 'HiHiHi'
1.4 Variables
- Variables store data.
- Example:
x=5
y=x+3
print(y) # Output: 8
1.5 Your First Program
print("Hello, World!")
Python Programming - Module 1 (Simplified Notes)
1.6 Input and Output
- input(): Takes user input as a string.
- print(): Displays output.
- Example:
name = input("Enter your name: ")
print("Hello, " + name)
2. Flow Control
2.1 Boolean Values
- Only two values: True or False
2.2 Comparison Operators
- ==, !=, <, >, <=, >=
2.3 Boolean Operators
- and: True if both are true
- or: True if at least one is true
- not: Reverses the value
2.4 If, elif, else Statements
- if: runs a block if condition is True
- elif: more conditions
- else: if nothing above is true
2.5 While Loops
- Repeats a block while a condition is True
2.6 For Loops with range()
Python Programming - Module 1 (Simplified Notes)
- Repeat a block for a fixed number of times
- Example:
for i in range(5):
print(i)
2.7 Break and Continue
- break: Exit the loop early
- continue: Skip the rest and go to next loop
2.8 Importing Modules
- Use `import module_name` to use extra features
- Example: import random
2.9 sys.exit()
- Use to exit the program early
3. Functions
3.1 Defining a Function
- def hello(): defines a function
3.2 Parameters and Return
- Functions can take inputs and return values
- Example:
def add(a, b):
return a + b
3.3 None Value
- Special value meaning "nothing"
Python Programming - Module 1 (Simplified Notes)
3.4 Keyword Arguments in print()
- print('Hi', end='!') prints without newline
3.5 Local and Global Variables
- Local: inside function
- Global: outside all functions
3.6 Global Statement
- Allows modifying global variables inside functions
3.7 Exception Handling
- try and except to handle errors
3.8 Example: Guess the Number Game
- Uses input(), random, if-else, loops, etc.