0% found this document useful (0 votes)
2 views10 pages

Lect 2 Python

The document provides an overview of Python variables, including how to create them, their types, and naming conventions. It explains variable assignment, casting, and the use of global variables, along with examples of legal and illegal variable names. Additionally, it covers techniques for improving variable readability and methods for outputting variables in Python.

Uploaded by

darkluci702
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)
2 views10 pages

Lect 2 Python

The document provides an overview of Python variables, including how to create them, their types, and naming conventions. It explains variable assignment, casting, and the use of global variables, along with examples of legal and illegal variable names. Additionally, it covers techniques for improving variable readability and methods for outputting variables in Python.

Uploaded by

darkluci702
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

Python Variables

Variables
Variables are containers for storing data values.
Creating Variables
Python has no command for declaring a variable.
A variable is created the moment you first assign a value to it.
E.g. 1
x = 108
y = "Ram"
print(x)
print(y)
O/P:

Variables do not need to be declared with any particular type, and can even change type after
they have been set.
E.g. 2
x = 109 # x is of type int
x = "Siya" # x is now of type str
print(x)
O/P:

Casting
If you want to specify the data type of a variable, this can be done with casting.
E.g. 3
x = str(45) # x will be '45'
y = int(56) # y will be 56
z = float(10) # z will be 10.0
O/P:
E.g. 4
w = str('shikha')
x = str(45)
y = int(546.78)
z = float(345.987)

print(w)
print(x)
print(y)
print(z)
O/P:

Get the Type


You can get the data type of a variable with the type() function.
E.g. 5
a = 'A3'
b= "A3"
c = 'Shikha'
d = "Shikha"
e = 45
f = 10.57

print(type(a))
print(type(b))
print(type(c))
print(type(d))
print(type(e))
print(type(f))
O/P:

Single or Double Quotes?


String variables can be declared either by using single or double quotes:
E.g. 6
x = 'Shikha' # is the same as
x = "Shikha"
O/P:

Case-Sensitive
Variable names are case-sensitive.
E.g. 7
This will create two variables:
a = 109
A = "Siya"
#A will not overwrite a
O/P:
Variable Names
A variable can have a short name (like x and y) or a more descriptive name (age, carname,
total_volume). Rules for Python variables:
• A variable name must start with a letter or the underscore character
• A variable name cannot start with a number
• A variable name can only contain alpha-numeric characters and underscores (A-z, 0-
9, and _)
• Variable names are case-sensitive (age, Age and AGE are three different variables)
• A variable name cannot be any of the Python keywords.
E.g. 8
Legal variable names:
myvar = "Siya"
my_var = "Ram"
_my_var = "SiyaRam"
myVar = "RamSiya"
MYVAR = "SiYaRam"
myvar2 = "RAmSiya"
O/P:

E.g. 9
Illegal variable names:
2myvar = "RAM"
my-var = "Siya"
my var = "SiyaRam"
O/P:
Note: Remember that variable names are case-sensitive.

Multi Words Variable Names


Variable names with more than one word can be difficult to read.
There are several techniques you can use to make them more readable:
Camel Case
Each word, except the first, starts with a capital letter:
myVariableName = "Siya"
Pascal Case
Each word starts with a capital letter:
MyVariableName = "Siya"
Snake Case
Each word is separated by an underscore character:
my_variable_name = "Siya"
Many Values to Multiple Variables
Python allows you to assign values to multiple variables in one line:
E.g. 10
print ('Fruits Name')
a, b, c = "1. Apple", "2. Banana", "3. Cherry"
print(a)
print(b)
print(c)
O/P:

Note: Make sure the number of variables matches the number of values, or else you will get
an erro

One Value to Multiple Variables


And you can assign the same value to multiple variables in one line:
E.g. 11
print ('Fruit Name')
x = y = z = "Orange"
print(x)
print(y)
print(z)
O/P:

Unpack a Collection
If you have a collection of values in a list, tuple etc. Python allows you to extract the values
into variables. This is called unpacking.
E.g. 12
Unpack a list:
print("Name of Students:")
Name_of_Students = ["Aayush", "Aayan", "Aarav"]
x, y, z = Name_of_Students
O/P:

Output Variables
The Python print() function is often used to output variables.
E.g. 13
x = "Exercise is good for health."
print(x)
O/P:
In the print() function, you output multiple variables, separated by a comma:
E.g. 14
a = " Exercise "
b = "is"
c = "good"
d = “for”
e = “health”
print(a, b, c, d, e)
O/P:

You can also use the + operator to output multiple variables:


E.g. 15
a = " Exercise "
b = "is"
c = "good"
d = “for”
e = “health”
print(a + b + c + d + e)
O/P:

Notice the space character after "Exercise " and "is "and so on , without them the result
would be "Exerciseisgoodforhealth".
For numbers, the + character works as a mathematical operator:
E.g. 16
x = 67
y = 59
print(x + y)
O/P:
In the print() function, when you try to combine a string and a number with the + operator,
Python will give you an error:
E.g. 17
x = 109
y = "Siya"
print(x + y)
O/P:

The best way to output multiple variables in the print() function is to separate them with
commas, which even support different data types:
E.g. 18
x = 109
y = "Siya"
print(x, y)
O/P:

Global Variables
Variables that are created outside of a function (as in all of the examples in the previous
pages) are known as global variables.
Global variables can be used by everyone, both inside of functions and outside.
E.g. 19
Create a variable outside of a function, and use it inside the function
x = "awesome"

def myfunc():
print("Python is " + x)
myfunc()
O/P:

If you create a variable with the same name inside a function, this variable will be local, and
can only be used inside the function. The global variable with the same name will remain as it
was, global and with the original value.
E.g. 20
Create a variable inside a function, with the same name as the global variable
x = "awesome"

def myfunc():
x = "fantastic"
print("Python is " + x)

myfunc()

print("Python is " + x)
O/P:

The global Keyword


Normally, when you create a variable inside a function, that variable is local, and can only be
used inside that function.
To create a global variable inside a function, you can use the global keyword.
E.g. 21
If you use the global keyword, the variable belongs to the global scope:
def myfunc():
global x
x = "fantastic"

myfunc()
print("Python is " + x)
O/P:

Also, use the global keyword if you want to change a global variable inside a function.
E.g. 22
To change the value of a global variable inside a function, refer to the variable by using
the global keyword:
x = "awesome"

def myfunc():
global x
x = "fantastic"

myfunc()

print("Python is " + x)
O/P:

You might also like