Python Programming Fundamentals – Study Notes
Tokens in Python
Tokens are the smallest individual units in a Python program.
Types of Tokens
1. Keywords
2. Identifiers (Names)
3. Literals (Values)
4. Operators
5. Punctuators
Keywords
Keywords are reserved words in Python that have special meanings.
Examples:
if, else, while, for, break, continue, True, False, None
Example:
if x > 10:
print("Large number")
Identifiers (Names)
Identifiers are names given to variables, functions, classes, etc.
Rules for Naming Identifiers
• Must begin with a letter or underscore _
• Cannot start with a digit
• Cannot contain spaces
• Cannot use keywords
• Case-sensitive
1
Valid:
age
_total
student1
Invalid:
1name
class
my name
Literals (Values)
Literals are fixed values used in a program.
String Literals
Strings are sequences of characters enclosed in quotes.
Example:
name = "Python"
city = 'Madurai'
Numeric Literals
Numeric literals represent numbers.
Integer
x = 10
Float
y = 3.14
2
Complex
z = 2 + 3j
Boolean Literals
Boolean values are:
True
False
Example:
is_valid = True
Special Literal None
None represents the absence of a value.
Example:
x = None
Operators
Operators perform operations on variables and values.
Arithmetic Operators
+, -, *, /, %, //, **
Example:
3
a = 10
b = 3
print(a + b)
print(a ** b)
Relational Operators
>, <, >=, <=, ==, !=
Logical Operators
and, or, not
Punctuators
Punctuators are symbols used to organize Python statements.
Examples:
( ) [ ] { } , : ; .
Variables and Assignments
Variables store data values.
Example:
name = "Arun"
age = 15
Dynamic Typing
Python automatically determines the data type.
4
Example:
x = 10
x = "Hello"
Multiple Assignments
Assigning Same Value to Multiple Variables
a = b = c = 100
Assigning Multiple Values to Multiple Variables
x, y, z = 1, 2, 3
Input and Output
input( ) Function
Used to get input from the user.
Example:
name = input("Enter your name: ")
print( ) Function
Used to display output.
Example:
print("Welcome")
5
Data Types
Data Types for Numbers
• int
• float
• complex
Example:
x = 5
y = 3.5
z = 2 + 4j
Data Types for Strings
Example:
message = "Hello Python"
String Operations
print(message[0])
print(len(message))
Lists
Lists are ordered and mutable collections.
Example:
numbers = [1, 2, 3, 4]
List Features
• Ordered
• Mutable
• Allows duplicates
6
Tuples
Tuples are ordered and immutable collections.
Example:
data = (10, 20, 30)
Set
Sets are unordered collections without duplicates.
Example:
items = {1, 2, 3}
Dictionaries
Dictionaries store data as key-value pairs.
Example:
student = {
"name": "Ravi",
"age": 16
}
Expressions
Expressions are combinations of variables, values, and operators.
Evaluating Arithmetic Operations
Example:
7
x = 5 + 3 * 2
print(x)
Evaluating Relational Expressions
Example:
print(10 > 5)
Evaluating Logical Expressions
Example:
print(True and False)
Type Casting
Type casting converts one data type into another.
Example:
x = "10"
y = int(x)
print(y + 5)
Common Functions:
int()
float()
str()
bool()
8
Math Library Functions
Python provides mathematical functions through the math module.
Example:
import math
print([Link](25))
print([Link](5))
Common Functions:
• [Link]()
• [Link]()
• [Link]()
• [Link]()
• [Link]()
Statement Flow Control
Compound Statement
A compound statement contains multiple statements.
Example:
if x > 0:
print("Positive")
print("Number checked")
Simple Statement
A single line statement.
Example:
9
x = 10
Empty Statement
The pass statement does nothing.
Example:
if x > 0:
pass
The if Conditionals
Plain if Conditional Statement
Example:
if x > 0:
print("Positive")
The if-else Conditional Statement
Example:
if x % 2 == 0:
print("Even")
else:
print("Odd")
The if-elif Conditional Statement
Example:
10
marks = 75
if marks >= 90:
print("A")
elif marks >= 75:
print("B")
else:
print("C")
Nested if Statements
Example:
x = 10
if x > 0:
if x < 20:
print("Between 1 and 19")
Storing Conditions
Example:
condition = x > 10
print(condition)
Looping Statements
The for Loop
Used for iterating over sequences.
Example:
11
for i in [1, 2, 3]:
print(i)
General for Loop Statement
Syntax:
for variable in sequence:
statements
The range( ) Based for Loop
Example:
for i in range(5):
print(i)
Variations
range(start, stop)
range(start, stop, step)
The while Loop
Repeats while a condition is true.
Example:
count = 1
while count <= 5:
print(count)
count += 1
12
Jump Statements
The break Statement
Terminates the loop.
Example:
for i in range(10):
if i == 5:
break
print(i)
The continue Statement
Skips the current iteration.
Example:
for i in range(5):
if i == 2:
continue
print(i)
More on Loops
Loop else Statement
The else block executes when the loop finishes normally.
Example:
for i in range(3):
print(i)
else:
print("Loop completed")
13
Nested Loops
A loop inside another loop.
Example:
for i in range(3):
for j in range(2):
print(i, j)
Summary
Python programming basics include:
• Tokens and identifiers
• Variables and data types
• Input and output
• Operators and expressions
• Conditional statements
• Looping structures
• Jump statements
Mastering these concepts forms the foundation for advanced Python programming.
14