Variables and Datatypes1.0 (Python) Presentation
Variables and Datatypes1.0 (Python) Presentation
ES
PYTHON VARIABLES
Variables are the containers for
storing data values.
OUT P U T
:5
Example: john
X=5
Y=
“john”
Print(x)
Print(y)
1.PYTHON VARIABLES
NAMING RULES
1. Variable name should start with letter(a-z/A-Z) or
an underscore (_).
Valid : age , _age , Age Invalid : 1age
2.In variable name, no special characters allowed
other than underscore (_).
Valid : age_ , _age Invalid : age_*
3.Variables are case sensitive. age and Age are
different, since variable names are case sensitive.
4.Variable name can have numbers but not at the
beginning. Example: Age1
5.Reserve words (keyword)cannot be used as
identifier names . Keywords are also called as
reserved words.
print
b da
l am or
try
in
rai ass
se IF and ert
glo
exc fin
b
def elif
al
ept ally
return del
cla KEYWORD fals for
ss S e
as
wit im is
h tru por
not t
e pas
co
fro
n
m s
ti n
els whi
l
ue
bre
a
oc
e le
ak
nl
no
yield
To assign a value of python variable , you don’t need to declare
it’s type.
You name it according to rules started in section 2a,and type
the value after the equal sign(=).
1. >>> age=7
1. >>> age= OUTPUT 2. >>> print
‘Dinosaur’ Output :
(age)
2. >>> print (age) Dinosaur 7
1. >>> age
,
city=21,’Indo
OUTPU
re’
T
21
Indore
Or you can assign the same value to multiple
Python variables.
l
ba
l
ca
lo
Lo
os
-in
G
cl
ilt
d
En
Bu
SCOPE OF VARIABLE
Global variables
Local variables
1.PYTHON LOCAL
VARIABLES
When you declare a variable in a function ,class or so , it is only
visible in that scope . If you call it outside of that scope ,you get an
‘undefined’ error.
Example:
def myfunc():
x = 300
print(x) O/P:
300
myfunc()
2.Global Variables
When you declare a variable outside any scope/Context , it
is visible in the whole program.
O/ P :
300
Example:
x = 3003
00
def
myfunc():
print(x)
myfunc()
Global variable and Local variable with same name
X=5
def foo():
local x: 10 x = 10
global x: 5 print (“local x:”, x)
foo()
Print (“global x:”, x)
1.What are variables and data types in python?
COMPLEX
NUMBER LIST
1.PYTHON NUMBERS
There are four numeric PYTHON data types.
a.in
a.Int
Intt stands for integer . This python Data Type holds signed integers . We can
use the type()function to find which class it belong to.
1.>>> a=-7
2.>>> type(a)
O/P:
<class ‘int’ >
An integer can be of any length ,with the only limitation being the available
memory.
1. >>>
a=99999999999999999999999999
2. >>> type(a)
b.float
O/P:
<class ‘int’>
b. float This python data types holds floating-point real
values . An integer can only store the number 3,but float can store 3.25 if you
want.
1. >>>a=3.0
c.long
This python data type holds a long integer of unlimited length .But this
construct does not exist in Python 3.x.
d.complex
This Python data type holds a complex number . A complex number looks like
this:a+bj Here , a and b are the real parts of the number, and j is imaginary.
O/P:
<class ‘complex’>
2.STRINGS
A string is a sequence of characters . Python does not a char data type , unlike
C++ or Java . You can delimit a string using single quotes or double-quotes.
1. >>> city=‘Ahmedabad’
2. >>> city
B. Displaying Part of a String
You can display a character from a string using its index in
the string . Remember , indexing starts with 0.
1. >>> O/P:
lesson[5:1
0] ‘point’
String Concatenation
O/P:
Traceback (most recent call last) :File
1. >>> “<pyshe11#89>” , line 1,in <module>;
print(’10’+10) Print(‘10’+10)
Type Error : must be str ,not int
3. PYTHON LISTS
A list is a collection of values. Remember ,it may contain different types of values.
To define a list , you must put values separated with commas in square brackets .You
don’t need to declare a type for a list either.
1. >>>
days==[‘Monday’,’Tuesday’,3,4,5,6,7] O/P: [‘Monday’, ’Tuesday’, 3, 4, 5,
2. >>> days 6, 7]
A . Slicing a List
B . Length of a List
Python supports an inbuilt function to calculate
the length of a list.
1. >>> len (days) O/P: 7
O/P:
1.nums = [1,2,5,6,8]
1
2.for n in nums:
3.print(n) 2
5
6
8
E . Multidimensional List
A list may have more than one dimension . Have a detailed look on this in
DataFlair ’s tutorial on Python Lists .
O/P:
1. >>> a=[[1,2,3] , [4,5,6]]
[[1 , 2, 3] , [4, 5, 6]]
2. >>>
a
4.PYTHON TUPLES
A tuples is like a list . You declare it using parentheses instead.
1. O/P:
>>>subjects=(‘physics’,‘Chemistry’,’Ma (‘Physics’, ‘Chemistry’, ‘Maths’)
ths’)
2. >>> subjects
Accessing and Slicing a Tuple
You access a tuple the same way as you’d access a list . The same
goes for slicing it.
O/P:
1. >>>
subjects[1] ‘chemistry’
O/P:
1.
>>>subjects[0:2] (‘Physics’ ,’Maths’)
5.Dictionaries
A dictionary holds key-value pairs . Declare it in curly braces , with pairs separated
by commas . Separate keys and values by a colon (:).
1. >>>
person={‘city’:’Ahmedabad’,’age’:7}
2. >>> person
O/P:
{ ‘city’: ‘Ahmedabad’ , ‘age’ : 7 }
The type() function works with
dictionaries too.
O/P:
1.
‘Ahmedabad’
>>>person[‘city’]
Reassigning Elements
O/P:
1.
{1, 2,
>>> a={1,2,2,3}
3}
2.
>>> a