Identifiers - An identifier is a name used to identify a variable, function,
class, or object in Python.
Rules for Identifiers
1) Must start with a letter (a–z, A–Z) or underscore (_)
2) Cannot start with a number
3) Can contain letters, numbers, and underscore
4) No spaces allowed
5) Keywords cannot be used as identifiers
6) Python identifiers are case-sensitive
EXAMPLE –
Name = “Sakshi” # name is an identifier
_Age = 20 # age is an identifier
VALID INDETIFIERS-
student_name
_age
totalMarks
Python123
INVALID INDETIFIERS-
1name # starts with number
total-marks # hyphen not allowed
class # keyword
student name # space not allowed
CASE SENSITIVITY –
A = 10
A = 20
Print(a) # 10
Print(A) # 20
KEYWORDS-
Keywords are predefined words in Python that are used to perform
specific actions in a program.
EXPRESSIONS AND STATEMENTS-
A statement is an instruction that Python executes to perform an
action.
Types of Statements
Assignment statement
Conditional statements (if, else, elif)
Looping statements (for, while)
Control statements (break, continue, pass)
Function statement (def)
Import statement (import)
EXAMPLE -
X = 10 # assignment statement
Print(x) # print statement
If x > 5: # if statement
Print(“Big”)
EXPRESSIONS IN PYTHON -
An expression is a combination of values, variables, and operators that
produces a result.
Expressions return a value.
EXAMPLE –
10 + 5 # arithmetic expression
X>3 # relational expression
A and b # logical expression
“Py” + “thon” # string expression
Difference Between Statement and Expression -
Statement Expression
Performs an action. Produces a value
May not return value. Always returns value
Example: print(x) Example: x + 2
VARIABLE –
A variable stores data in memory.
Example:
Age = 20
Name = “Sakshi”
Python is dynamically typed (no need to declare data type).
EXAMPLE-
X = 10
Name = “Sakshi”
Price = 99.50
a, b, c = 1, 2, 3
PRECEDENCE -
Operator precedence decides which operator is evaluated first in an
expression.
When more than one operator appears in an expression, higher-
precedence operators are evaluated first.
EXAMPLE –
Result = 10 + 5 * 2
Print(result)
According to precedence rules:
5 * 2 = 10 ← multiplication first
10 + 10 = 20 ← then addition
If you want addition first
Use parentheses:
Result = (10 + 5) * 2
Print(result)
ASSOCIATIVITY -
Associativity tells us the direction in which operators of the same
precedence are evaluated.
*Left-to-right
Result = 10 – 5 – 2
Print(result)
*Right-to-left
a = b = c = 10
Indentation-
Python uses indentation instead of braces {}.
EXAMPLE –
If 10 > 5:
Print(“Correct”)
Print(“Python”)
ERROR –
If 10 > 5:
Print(“Wrong”) # IndentationError
Comments in Python-
Comments are used to explain code.
Single-line Comment:
# This is a comment
Multi-line Comment:
“””
This is
Multi-line
Comment
“””
DATA TYPES -
A data type tells Python what kind of value a variable is storing.
Numeric Data Types:-
Used to store numbers.
a) Integer (int)
Whole numbers (positive or negative)
A = 10
B = -25
print(a, b)
b) Float (float)
Numbers with decimal point
X = 3.14
Y = 10.5
print(x,y)