Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 15

Python identifiers

Python identifiers refer to a name used to identify a


variable, function, module, class, module or other
objects. There are few rules to follow while naming the
Python Variable.
• A variable name must start with either an English letter
or underscore (_).
• A variable name cannot start with the number.
• Special characters are not allowed in the variable
name.
• The variable's name is case sensitive.
The multi-word keywords can be created by the following
method.
 Camel Case - In the camel case, each word or
abbreviation in the middle of begins with a capital letter.
There is no intervention of whitespace. For example -
nameOfStudent, valueOfVaraible, etc.
 Pascal Case - It is the same as the Camel Case, but here
the first word is also capital. For example -
NameOfStudent, etc.
 Snake Case - In the snake case, Words are separated by
the underscore. For example - name_of_student, etc.
Assignment
• We don't need to declare explicitly variable in
Python. When we assign any value to the
variable, that variable is declared
automatically.
• The equal (=) operator is used to assign value
to a variable.
• X=10
Multiple assignment
• Python allows us to assign a value to multiple
variables in a single statement, which is also
known as multiple assignments.
• We can apply multiple assignments in two ways:
1. assigning a single value to multiple variables
or
2. assigning multiple values to multiple variables.
Single value to multiple variables
Example:
x=y=z=10
print(x)
print(y)
print(z)
Multiple values to multiple variables

Example:
x,y,z=10,20,30
print(x)
print(y)
print(z)
Python variable types
Two types of variables:
 Local variables
 Global variables
Local variables:
Local variables are the variables that declared
inside the function and have scope within the
function
Example:
def add()
a=20
b=30
c=a+b
print(c)
add()
Global Variables

• Global variables can be used throughout the program,


and its scope is in the entire program.
• We can use global variables inside or outside the
function.
• A variable declared outside the function is the global
variable by default.
• Python provides the global keyword to use global
variable inside the function. If we don't use the global
keyword, the function treats it as a local variable.
Example:
x=10
def add():
global(x)
print(x)
add()
Deleting a variable
We can delete variable by using del keyword
Syntax:
del <variable_name>
example:
x=30
Print(x)
del x
print(x)
Printing variables
• Printing single variable
print(a)
• Printing multiple variables
Print(a,b,c)
Print(1,2,3,4)
Exercises
1.Create a variable named carname and assign the
value Volvo to it.
carname=“Volvo”
2. Create a variable named x and assign the value
50 to it.x=50
3. Display the sum of 5 + 10, using two variables: x
and y
4.Create a variable called z, assign x + y to it, and
display the result.
5.Remove the illegal characters in the variable name:
2my-first_name = "John“
6. Insert the correct keyword to make the variable x
belong to the global scope
x=“hai”
def myfunc():
global x
x = “fantastic”
myfun()
print(x)

You might also like