Identifiers in Python
Definition
An identifier is the name given to any variable, function, class, module, or
object in a Python program. It is used to identify or refer to these entities
uniquely.In simple terms, whenever we create something in a program, we
must give it a name, and that name is called an identifier.
Example:
student_name = "Rahul"
roll_number = 101
Here, student_name, roll_number are identifiers.
Rules for Forming Identifiers in Python
Python strictly follows certain rules for naming identifiers:
1. Allowed Characters:
o Identifiers can only contain letters (A–Z, a–z), digits (0–9), and
underscore (_).
o Example: student1, total_marks, _value.
2. Must Start with Letter or Underscore:
o An identifier cannot start with a digit.
o Valid: marks1, _name
o Invalid: 1marks, 123value.
3. Cannot be a Keyword:
o Python keywords (reserved words) cannot be used as
identifiers.
o Example: for, while, class, if cannot be identifiers.
4. Case Sensitivity:
o Python treats uppercase and lowercase letters differently.
o Example: Name and name are two different identifiers.
5. No Special Characters or Spaces:
o Identifiers cannot include symbols like @, #, $, %, or spaces.
o Invalid: roll-no, student name.
6. Length of Identifiers:
o Python does not limit the length of identifiers, but they should
be kept reasonable for readability.
3. Examples of Valid Identifiers
• x, totalMarks, student_name, value1, _temp
4. Examples of Invalid Identifiers
• 2value → starts with a digit
• roll no → contains space
• for → keyword
• @name → contains special symbol
5. Reserved Keywords in Python
Python has around 35 keywords (depending on the version) such as:
if, else, while, for, def, class, import, break, continue, return, pass, True,
False, None.
These words have predefined meaning in Python and cannot be used as
identifiers.
Data Types in Python
In Python, every value stored in a program has a specific type, which tells
the interpreter how that value will be used in operations. These types are
called data types. Since Python is a dynamically typed language, the
programmer does not need to declare the type of a variable explicitly; the
interpreter automatically assigns the appropriate type based on the value
that is given. The most commonly used data types in Python can be broadly
divided into numeric,sequence and boolean categories.
Numeric types include int, float. An integer (int) represents whole
numbers without a fractional part, such as 10 or -25. A floating-point
number (float) represents numbers with a decimal point, such as 3.14 or -
0.5.
A string is a sequence of characters enclosed in either single or double
quotes, for example "Hello" or 'Python'.
Another important data type is the boolean type, which has only two
values: True and False. These are used for logical operations and conditional
statements. Python also has a special data type called NoneType,
represented by the keyword None, which indicates the absence of a value
or a null reference.
In summary, Python data types provide a way of classifying values and
determining how they can be manipulated. From basic numeric and textual
types to more advanced collection types such as lists, tuples, sets, and
dictionaries, Python offers a wide range of built-in data types that make it
flexible and powerful for programming. Since type declarations are not
required, beginners find Python easier to learn, but at the same time,
understanding these data types is essential for writing correct and efficient
programs.
Data Type Example Description
integer 5, -10 Whole numbers
float 5.5, -3.14 Decimal numbers
string "Hello" Text (string)
bool True, False Boolean values
NoneType None Absence of a value
Imagine logging into Instagram:
• Your username (string) → "rkasa_21"
• Your followers count (integer) → 1050
• Your profile completion (float) → 85.5
• Your is_private setting (boolean) → True
Variables in Python
A variable in Python is simply a name that is used to store a value. When
we write a program, we often need to remember data for later use, such
as a student’s marks, the result of a calculation, or a piece of text. Instead
of working directly with values, we assign them to variables. In Python,
variables act like containers or labels that point to the data stored in the
computer’s memory. For example, if we write x = 10, then the variable x
refers to the value 10.
Unlike some programming languages where the type of a variable must be
declared before use, Python is dynamically typed, meaning we can assign
any type of value to a variable without specifying its type. The interpreter
automatically decides the data type based on the assigned value. For
instance, x = 5 will make x an integer, but if later we write x = "Hello", the
same variable x will now refer to a string.
They are case sensitive.
Creating Variables
Here, Python automatically understands that:
• name is a string
• age is an integer
• marks is a float
Variable Naming Rules
Just like identifiers, variables must follow certain naming rules in Python:
1. A variable name can contain letters (A–Z, a–z), digits (0–9), and the
underscore (_).
2. It must begin with a letter or underscore, not with a digit.
3. Spaces and special characters such as @, #, $, % are not allowed.
4. Variable names are case-sensitive, meaning Name and name are
different.
5. Keywords in Python cannot be used as variable nam
Multiple Variable Assignment
Python allows assigning values to multiple variables in one line
Changing Values of Variables
Since Python allows re-assignment, the value of a variable can be changed
during program execution. For example:
Now the variable age refers to the new value 21.
Operators in Python
In Python, an operator is a special symbol that is used to perform
operations on values or variables. For example, the symbol + adds two
numbers, while * multiplies them. Operators are essential in programming
because they allow us to manipulate data, perform calculations, compare
values, and control the flow of a program. The values or variables on which
operators act are called operands. For example, in the expression 10 +
20, 10 and 20 are operands, + is the operator, and the result is 30.
Python provides a rich set of operators which can be grouped into different
categories based on the type of operation they perform.
1. Arithmetic Operators
These are used for basic mathematical operations.
• + (Addition) → 10 + 5 = 15
• - (Subtraction) → 10 - 5 = 5
• * (Multiplication) → 10 * 5 = 50
• / (Division) → 10 / 5 = 2.0 (result is always float)
• // (Floor Division) → 10 // 3 = 3 (gives quotient without decimal part)
• % (Modulus) → 10 % 3 = 1 (gives remainder)
• ** (Exponent) → 2 ** 3 = 8 (2 raised to power 3)
2. Relational (Comparison) Operators
These are used to compare two values. The result is always a boolean (True
or False).
• == (Equal to) → 5 == 5 → True
• != (Not equal to) → 5 != 3 → True
• > (Greater than) → 10 > 5 → True
• < (Less than) → 10 < 5 → False
• >= (Greater than or equal to) → 10 >= 10 → True
• <= (Less than or equal to) → 7 <= 8 → True
3. Logical Operators
These are used to combine conditions and return a boolean result.
• and → Returns True if both conditions are true.
Example: (5 > 2) and (10 > 5) → True
• or → Returns True if at least one condition is true.
Example: (5 > 10) or (3 < 8) → True
• not → Returns the opposite of the condition.
Example: not(5 > 2) → False
4. Assignment Operators
These are used to assign values to variables and also perform operations
in a shorter way.
• = → Simple assignment (x = 10)
• += → Add and assign (x += 5 means x = x + 5)
• -= → Subtract and assign (x -= 5)
• *= → Multiply and assign (x *= 5)
• /= → Divide and assign (x /= 5)
• //= → Floor divide and assign (x //= 2)
• %= → Modulus and assign (x %= 3)
• **= → Power and assign (x **= 2)
5. Membership Operators
These are used to test whether a value is present in a sequence such as a
string, list, or tuple.
• in → Returns True if the value exists in the sequence.
Example: "a" in "apple" → True
• not in → Returns True if the value does not exist.
Example: "x" not in "apple" → True
6. Identity Operators
These are used to check whether two variables point to the same object in
memory.
• is → Returns True if both variables refer to the same object.
• is not → Returns True if they refer to different objects.
Example:
x = 100000
y=x
z = 100000
print(x is y) # True (same object)
print(x is z) # False (different objects, even if contents are same)
Basic Input and Output Operations in Python
Every program becomes useful only when it can interact with the user. This
interaction happens in two ways: taking input from the user and showing
output to the user. In Python, input is taken using the input() function, and
output is shown using the print() function.
Input in Python
The input() function allows the program to pause and wait for the user
to type something on the keyboard. Whatever the user types is taken as
a string by default. This means even if the user types a number, Python will
treat it as text unless we convert it to another type.
The syntax of the input function is:
variable = input("Message for the user: ")
• Here, the text inside the quotes is shown to the user as a prompt.
• The value entered by the user is stored in the variable.
Example 1: Taking a name as input
name = input("Enter your name: ")
print("Hello", name)
If the user types Rahul, the output will be:
Hello Rahul
Example 2: Taking numbers as input
num1 = input("Enter first number: ")
num2 = input("Enter second number: ")
print("Sum is", num1 + num2)
If the user enters 10 and 20, the output will be:
Sum is 1020
This happens because input() stores everything as a string, so "10" + "20"
becomes "1020" (string joining).
To make it behave like numbers, we must convert the input into integers
using the int() function.
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
print("Sum is", num1 + num2)
Now the output will be:
Sum is 30
Similarly, for decimal numbers we can use float() instead of int().
Output in Python
The print() function is used to display output to the user. We can display
text, numbers, variables, or even the result of expressions.
The syntax is:
print(values)
Example 1: Printing simple text
print("Welcome to Python")
Output:
Welcome to Python
Example 2: Printing variables
name = "Rahul"
age = 20
print("Name:", name)
print("Age:", age)
Output:
Name: Rahul
Age: 20
Example 3: Printing results of expressions
x=5
y = 10
print("Sum is", x + y)
Output:
Sum is 15
Special Features of print()
The print() function has some useful options:
sep (separator): By default, print() separates multiple values with a
space. We can change this using sep.
print("A", "B", "C", sep="-")
Output:
A-B-C
end: By default, print() ends with a newline. We can change this
behavior.
print("Hello", end=" ")
print("World")
Output:
Hello World
Summary
• input() is used to take input from the user. It always returns data as
a string, so conversion to int or float is needed when working with
numbers.
• print() is used to display output to the user. It can show text,
variables, and results of calculations.
• Options like sep and end make output formatting more flexible.
With these two simple functions, Python allows communication between the
user and the program, which is the foundation of all interactive applications.