Py Chapter 2 Topic 3
Py Chapter 2 Topic 3
When naming variables in Python, there are specific rules and conventions to
follow:
name = "alladi"
_name = "cloud"
name_1 = "srini"
Invalid examples:
b) The rest of the variable name can contain letters, numbers, and
underscores (_)
After the first letter or underscore, variable names can have any
combination of letters, digits, and underscores.
age2 = 30
height_in_cm = 170
first_name = "Mal"
c) Variable names are case-sensitive
python
Copy code
age = 25
Age = 30
AGE = 35
Python has reserved words (keywords) that have specific meanings in the
language, and they cannot be used as variable names. Some examples of
keywords are: if, else, while, for, True, False, None, etc.
Example:
You can check all the Python keywords using the keyword module:
import keyword
print(keyword.kwlist)
It's a good practice to choose meaningful names for variables to make the
code more readable and understandable.
Bad practice:
a = 10
b = 20
Good practice:
length = 10
width = 20
Example:
Use descriptive names: Choose names that reflect the value or purpose of
the variable.
temperature = 30
student_name = "venkat"
max_value = 100
user_input = "Yes"
employee_count = 50
Use uppercase letters for constants: Constants (variables that should not
change) are typically written in uppercase.
PI = 3.14159
GRAVITY = 9.8
Python is dynamically typed, meaning that you don't need to declare the type of a
variable when you create it. The type is inferred from the value assigned to the
variable.
x = 10 # x is an integer
x = "Hello" # Now x is a string
In this example, the type of x can change dynamically from an integer to a string
without an explicit type declaration.
6. Summary