0% found this document useful (0 votes)
3 views

VARIABLE

Dna fingerprint

Uploaded by

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

VARIABLE

Dna fingerprint

Uploaded by

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

VARIABLE

Variables are containers for storing data values.

Python has no command for declaring a variable.

A variable is created the moment you fi rst assign a value to it.


Input output
◦x = 5 o 5
y = "John" John
print(x)
print(y)

Variable Names
• A variable can have a short name (like x and y) or a more descriptive name (age, car name, 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 keyword.
Example
Legal variable names:
◦ myvar = "John"
◦ my_var = "John"
◦ _my_var = "John"
◦ myVar = "John"
◦ MYVAR = "John"
◦ myvar2 = "John“

Illegal variable names:


o 2myvar = "John“
o my-var = "John“
o my var = "John"
Assign Multiple Values

◦ Python allows you to assign values to multiple variables in one line:


Input
Answer:
◦ x, y, z = "Orange", "Banana", "Cherry"
print(x) Orange
print(y) Banana
print(z) Cherry
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.
Example:
Answer:
input
◦ fruits = ["apple", "banana", "cherry"] apple
x, y, z = fruits banana
print(x) cherry
print(y)
print(z)
Output variable
The Python print() function is often used to output variables.

x = "Python is awesome"
print(x)
Answer:
In the print() function, you output multiple variables, separated
by a comma
Python is awesome
x = "Python"
y = "is"
z = "awesome"
print(x, y, z)

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

x = "Python "
y = "is "
z = "awesome"
print(x + y + z)

Notice the space character after "Python " and "is ", without them the result would be "Pythonisawesome".
Global Variables
◦ Variables that are created outside of a function (as in all of the examples above) are known as global
variables.
◦ Global variables can be used by everyone, both inside of functions and outside.

Example:
Answer:
x = "awesome"

def myfunc(): Python is awesome


print("Python is " + x)

myfunc()
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.
Example:
def myfunc(): Answer:
global x
x = "fantastic"
Python is fantastic
myfunc()

print("Python is " + x)
THANK YOU
◦ Tshering Ghalley
◦ Jurme Dorji
◦ Samten Tamang
◦ Ngawang Eden Gyeltshen
◦ Tsheyang Youden

You might also like