0% found this document useful (0 votes)
10 views5 pages

Py Chapter 2 Topic 3

ALLADI CLOUD TRAINING

Uploaded by

actnowcloud
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views5 pages

Py Chapter 2 Topic 3

ALLADI CLOUD TRAINING

Uploaded by

actnowcloud
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

VARIABLES AND RULES FOR NAMING

When naming variables in Python, there are specific rules and conventions to
follow:

a) Variable names must start with a letter or an underscore ( _ )

 You cannot start a variable name with a number.


 Valid examples:

name = "alladi"
_name = "cloud"
name_1 = "srini"

 Invalid examples:

1name = "Eve" # Starts with a number (invalid)

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 treats uppercase and lowercase letters as distinct. For example,


age, Age, and AGE are three different variables.

python
Copy code
age = 25
Age = 30
AGE = 35

d) Variable names cannot be Python keywords

 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:

if = 10 # Invalid (if is a keyword)

You can check all the Python keywords using the keyword module:

import keyword
print(keyword.kwlist)

e) Avoid using built-in function names as variable names

 Python has several built-in functions like print(), input(), sum(),


etc. While it's not strictly forbidden to use these as variable names, doing so
can overwrite the function and lead to errors in the program.

Example (not recommended):


print = "Hello" # This will overwrite the built-in
print() function

f) Variable names should be meaningful and descriptive

 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

g) No spaces are allowed in variable names

 Variable names cannot contain spaces. Instead, use underscores (_) to


separate words.

Example:

user_name = "Alladi" # Correct (underscore used)


user name = "alladi" # Incorrect (spaces not
allowed)
3. Examples of Valid and Invalid Variable Names
Valid Variable Names Invalid Variable Names

age 2nd_number (cannot start with a number)

_name first-name (hyphen not allowed)

first_name first name (spaces not allowed)

total3 for (keyword)

user_height class (keyword)

PI (for constants) 123abc (starts with a number)

4. Best Practices for Naming Variables

 Use descriptive names: Choose names that reflect the value or purpose of
the variable.

temperature = 30
student_name = "venkat"

 Use underscores to separate words: This is called snake_case and is


commonly used in Python.

max_value = 100
user_input = "Yes"

 Use lowercase letters: By convention, variable names in Python are written


in all lowercase letters.

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

5. Dynamic Typing in Python

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

 Variables are containers for storing data values.


 They must start with a letter or an underscore.
 They cannot start with a number or contain spaces.
 Variable names are case-sensitive.
 Python keywords cannot be used as variable names.
 Use meaningful, descriptive names to improve code readability.

You might also like