Day 2 of Learning Python: Variables, Identifiers, and Data Types
Welcome to Day 2 of my Python learning journey!
Today, I learned about identifiers, variables, assignment operators, reserved words, and basic data types in Python.
Let's understand each concept with simple examples.
1. What Is an Identifier?
An identifier is a name used to identify a variable, function, class, or other named object in a Python program.
For example:
name = "Loki"
question = 5
doubt = False
Here, name, question, and doubt are identifiers.
Rules for Naming Identifiers
- Identifiers can contain uppercase and lowercase letters (
A-Z,a-z). - They can contain digits (
0-9), but cannot start with a digit. - Underscores (
_) are allowed, including at the beginning. - Spaces and special characters such as
@and#are not allowed in ordinary identifiers. - Python identifiers are case-sensitive.
Examples:
name = "Loki" # Valid
_name = "Stark" # Valid
name2 = "Tony" # Valid
2name = "Thor" # Invalid
The last example is invalid because an identifier cannot start with a digit.
2. What Is a Variable?
A variable is a name that refers to an object in Python.
For example:
name = "Loki"
Here, name refers to a string object containing "Loki".
We can assign a different object to the same name:
doubt = False
doubt = True
print(doubt)
Output:
True
The second assignment makes doubt refer to True instead of False.
In Python, variables are often described as names or references rather than fixed containers that permanently store a particular type of value.
3. What Is the Assignment Operator?
The assignment operator (=) assigns an object to a name.
Example:
name = "Loki"
question = 5
doubt = True
In the statement:
question = 5
- Left side:
question— the identifier. -
=— the assignment operator. - Right side:
5— an integer object.
Remember: In Python, = means assignment, whereas == is used to compare two values for equality.
Example:
x = 10
print(x == 10)
Output:
True
4. Single and Double Underscores in Python
Python uses underscores in naming conventions, but their meanings are different.
Single Leading Underscore (_name)
_name = "Stark"
A single leading underscore is a convention indicating that a name is intended for internal use.
It does not make the variable truly private.
Double Leading Underscore (__name)
class Company:
def __init__(self):
self.__name = "Stark Industries"
Inside a class, a name beginning with two underscores triggers name mangling. Python changes the internal attribute name to help avoid accidental naming conflicts in subclasses.
It is not a strict privacy mechanism.
Double Underscores at Both Ends (__name__)
Names such as __init__ and __str__ are commonly called dunder or special names.
Python uses these names for special methods and other language-defined purposes.
We should generally avoid inventing such names for our own ordinary variables.
5. What Are Keywords?
Keywords are reserved words that have special meaning in Python's syntax.
Examples include:
if
else
for
while
break
continue
return
class
def
import
We cannot use keywords as ordinary variable names.
For example:
if = 10
This produces a syntax error because if is a keyword.
A small correction: Python has 35 keywords in Python 3.13, but the number can change between Python versions. Also, elif is Python's keyword for the condition commonly called "else if" in other languages.
6. Basic Data Types in Python
A data type describes the kind of object a value represents.
Let's look at some common built-in types.
Integer (int)
Used for whole numbers.
age = 25
quantity = -10
print(type(age))
Output:
<class 'int'>
Floating-Point Number (float)
Used for numbers with a fractional part.
price = 99.50
temperature = 36.6
String (str)
Used to represent text.
Strings can be enclosed in single or double quotes.
name = "Loki"
city = 'Chennai'
Boolean (bool)
Represents one of two values: True or False.
is_learning = True
has_doubt = False
Note that True and False must start with uppercase letters in Python.
Top comments (0)