SlideShare a Scribd company logo
http://www.skillbrew.com
/SkillbrewTalent brewed by the industry itself
Variables
Pavan Verma
@YinYangPavan
Python Programming Essentials
© SkillBrew http://skillbrew.com
What is a variable
2
>>> print 100
100
>>> counter = 100
>>> print counter
100
© SkillBrew http://skillbrew.com
What is a variable (2)
3
• A variable is a memory location that stores a value
• The value of a variable can change. That’s why it’s
called a variable!
• A variable is the basic unit of storing data in a
computer program
100counter
Somewhere in memory
© SkillBrew http://skillbrew.com
What is a variable (3)
4
>>> name = 'Henry Ford'
>>> print name
Henry Ford
• Variables can store values of different types
• In this slide, name is a variable of type ‘string’
• In previous slide, counter is a variable of type
‘int’
© SkillBrew http://skillbrew.com
Multiple variable assignments in one statement
5
>>> length = height = breadth = 2
>>> length
2
>>> height
2
>>> breadth
2
2length
2breadth
2height
© SkillBrew http://skillbrew.com
Multiple variable assignments in one statement
6
>>> x, y = 2, 3
>>> x
2
>>> y
3
2
3y
x
© SkillBrew http://skillbrew.com
Standard data types
Standard Data Types
Number String List Tuple Dictionary
7
© SkillBrew http://skillbrew.com
Standard data types (2)
>>> length = 10 #integer
>>> length
10
>>> motive = 'to learn Python' #string
>>> motive
'to learn Python'
8
© SkillBrew http://skillbrew.com
Standard data types (3)
>>> colors = ['brown', 'black', 'orange']
#list
>>> colors
['brown', 'black', 'orange']
>>> logs = ('skillbrew.com', 500) #tuple
>>> logs
('skillbrew.com', 500)
9
© SkillBrew http://skillbrew.com
Standard data types (4)
>>> contacts = {'Monty': 123445, 'Guido':
67788} #dictionary
>>> contacts
{'Monty': 123445, 'Guido': 67788}
10
© SkillBrew http://skillbrew.com
type function
>>> var = "foo"
>>> type(var)
<type 'str'>
>>> type(8)
<type 'int'>
>>> type(9.0)
<type 'float'>
11
© SkillBrew http://skillbrew.com
type function
>>> type([1, 2, 3])
<type 'list'>
>>> type((1, 2, 3))
<type 'tuple'>
>>> type({'1': 'one'})
<type 'dict'>
12
© SkillBrew http://skillbrew.com
Python is a dynamically typed language
 Variable type is determined at runtime
 A variable is bound only to an object
and the object can be of any type
 If a name is assigned to an object of
one type it may later be used to an
object of different type
13
Variable
Object
Type
is of
© SkillBrew http://skillbrew.com
Python is a dynamically typed language (2)
14
>>> x = 10
>>> x
10
>>> type(x)
<type 'int'>
>>> x = 'foo'
>>> x
'foo'
>>> type(x)
<type 'str'>
>>> x = ['one', 2, 'three']
>>> type(x)
<type 'list'>
© SkillBrew http://skillbrew.com
Dynamically typed vs Statically typed
 The way we used variable in previous slide
won’t work in statically typed languages
like C, C++, Java
 In a statically typed language, a variable is
bound to:
• an object – at runtime
• a type – at compile time
15
Variable
Object
Type
is of
Type
must match
© SkillBrew http://skillbrew.com
Statically Typed (C/C++/Java)
int x;
char *y;
x = 10;
printf(“%d”, x)
y = “Hello World”
printf(“%s”, y)
Dynamically Typed – Python
x = 10
print x
x = “Hello World”
print x
16
Dynamically typed vs Statically typed (2)
© SkillBrew http://skillbrew.com
Statically Typed (C/C++/Java)
 Need to declare variable
type before using it
 Cannot change variable
type at runtime
 Variable can hold only
one type of value
throughout its lifetime
Dynamically Typed – Python
 Do not need to declare
variable type
 Can change variable type
at runtime
 Variable can hold
different types of value
through its lifetime
17
Dynamically typed vs Statically typed (3)
© SkillBrew http://skillbrew.com
Python is a strongly typed language
>>> 10 + "10"
TypeError: unsupported operand type(s) for
+: 'int' and 'str'
>>> 10 + int("10")
20
18
In a weakly typed language (eg. perl), variables can be
implicitly coerced to unrelated types
Not so in Python! Type conversion must be done explicitly.
© SkillBrew http://skillbrew.com
Variable naming rules
 Variable names must begin with a letter [a-zA-
Z] or an underscore (_)
 Other characters can be letters[a-zA-Z],
numbers [0-9] or _
 Variable names are case sensitive
 There are some reserved keywords that cannot
be used as variable names
19
© SkillBrew http://skillbrew.com
Variable naming rules (2)
20
length
Length
myLength
_x
get_index
num12
1var
© SkillBrew http://skillbrew.com
Variable naming rules (3)
>>> 1var = 10
SyntaxError: invalid syntax
>>> var1 = 10
>>> v1VAR = 10
>>> myVar = 10
>>> _var = 10
>>> Var = 10
21
© SkillBrew http://skillbrew.com
Variable naming rules (4)
>>> myvar = 20
>>> myVar
10
>>> myvar
20
>>> myVar == myvar
False
22
© SkillBrew http://skillbrew.com
Accessing non-existent name
 Cannot access names before declaring it
23
>>> y
Traceback (most recent call last):
File "<pyshell#46>", line 1, in <module>
y
NameError: name 'y' is not defined
© SkillBrew http://skillbrew.com
Reserved keywords
and
del
from
not
while
as
elif
global
or
with
assert
else
if
pass
yield
break
except
import
print
class
exec
in
raise
continue
finally
is
return
def
for
lambda
try
24
© SkillBrew http://skillbrew.com
Cannot use reserved keywords as variable names
>>> and = 1
SyntaxError: invalid syntax
>>> if = 1
SyntaxError: invalid syntax
>>>
25
© SkillBrew http://skillbrew.com
Summary
 What is a variable
 Different types of variable assignments
 Brief introduction to standard data types
 type function
 Python is a dynamically typed language
 Dynamically types versus statically types languages
 Python is a strongly typed language
 Variable naming rules
 Reserved keywords
26
© SkillBrew http://skillbrew.com
Resources
 Python official tutorial
http://docs.python.org/2/tutorial/introduction.html
 Blog post on python and Java comparison
http://pythonconquerstheuniverse.wordpress.com/2009/10/03/pyt
hon-java-a-side-by-side-comparison/
 Python guide at tutorialspoint.com
http://www.tutorialspoint.com/Python/Python_quick_guide.html
 Python type function
http://docs.Python.org/2/library/functions.html#type
 Blog post on static vs dynamic languages
http://Pythonconquerstheuniverse.wordpress.com/2009/10/03/stat
ic-vs-dynamic-typing-of-programming-languages/
27
28

More Related Content

PPTX
Python Programming Essentials - M13 - Tuples
PPTX
Python Programming Essentials - M31 - PEP 8
PPTX
Python Programming Essentials - M11 - Comparison and Logical Operators
PPTX
Python Programming Essentials - M6 - Code Blocks and Indentation
PPTX
Python Programming Essentials - M9 - String Formatting
PPT
Buffer OverFlow
PPTX
Python Programming Essentials - M25 - os and sys modules
PPTX
Python Programming Essentials - M34 - List Comprehensions
Python Programming Essentials - M13 - Tuples
Python Programming Essentials - M31 - PEP 8
Python Programming Essentials - M11 - Comparison and Logical Operators
Python Programming Essentials - M6 - Code Blocks and Indentation
Python Programming Essentials - M9 - String Formatting
Buffer OverFlow
Python Programming Essentials - M25 - os and sys modules
Python Programming Essentials - M34 - List Comprehensions

What's hot (20)

PPT
C++ programming
PPTX
Python Programming Essentials - M39 - Unit Testing
PDF
4 operators, expressions &amp; statements
PPTX
Unit2 input output
PPT
My programming final proj. (1)
PPTX
Rust Intro
PPTX
c++ pointers by Amir Hamza Khan (SZABISTIAN)
PPTX
Oop object oriented programing topics
PPT
FP 201 Unit 2 - Part 3
PDF
Asterisk: PVS-Studio Takes Up Telephony
PDF
Lecture 4
PDF
Pointers & References in C++
PDF
Demonstration on keyword
PPTX
Python decorators
PDF
Let's make a contract: the art of designing a Java API
ODP
Decorators in Python
PDF
Advanced Patterns with io.ReadWriter
PDF
9 character string &amp; string library
PDF
4th_Ed_Ch03.pdf
PDF
CONST-- once initialised, no one can change it
C++ programming
Python Programming Essentials - M39 - Unit Testing
4 operators, expressions &amp; statements
Unit2 input output
My programming final proj. (1)
Rust Intro
c++ pointers by Amir Hamza Khan (SZABISTIAN)
Oop object oriented programing topics
FP 201 Unit 2 - Part 3
Asterisk: PVS-Studio Takes Up Telephony
Lecture 4
Pointers & References in C++
Demonstration on keyword
Python decorators
Let's make a contract: the art of designing a Java API
Decorators in Python
Advanced Patterns with io.ReadWriter
9 character string &amp; string library
4th_Ed_Ch03.pdf
CONST-- once initialised, no one can change it
Ad

Similar to Python Programming Essentials - M5 - Variables (20)

PDF
The python fundamental introduction part 1
PDF
Free Complete Python - A step towards Data Science
PPTX
python
PPTX
python_class.pptx
PDF
CS-XII Python Fundamentals.pdf
PPTX
INTRODUCTION TO PYTHON.pptx
PDF
Python Basics Understanding Variables.pdf
PDF
Python Basics Understanding Variables.pdf
PDF
Introduction to Python - Jouda M Qamar.pdf
PDF
Programming in Civil Engineering_UNIT 2_NOTES
PPTX
Introduction to Python Programming Language
PDF
python- Variables and data types
PPT
Python - Module 1.ppt
PPTX
Python basics
PPTX
UNIT 1 .pptx
PPTX
Python 01.pptx
PDF
Python - variable types
PPTX
Python Programming 1.pptx
PPTX
Python variables and data types.pptx
PPTX
Python (Data Analysis) cleaning and visualize
The python fundamental introduction part 1
Free Complete Python - A step towards Data Science
python
python_class.pptx
CS-XII Python Fundamentals.pdf
INTRODUCTION TO PYTHON.pptx
Python Basics Understanding Variables.pdf
Python Basics Understanding Variables.pdf
Introduction to Python - Jouda M Qamar.pdf
Programming in Civil Engineering_UNIT 2_NOTES
Introduction to Python Programming Language
python- Variables and data types
Python - Module 1.ppt
Python basics
UNIT 1 .pptx
Python 01.pptx
Python - variable types
Python Programming 1.pptx
Python variables and data types.pptx
Python (Data Analysis) cleaning and visualize
Ad

More from P3 InfoTech Solutions Pvt. Ltd. (20)

PPTX
Python Programming Essentials - M44 - Overview of Web Development
PPTX
Python Programming Essentials - M40 - Invoking External Programs
PPTX
Python Programming Essentials - M37 - Brief Overview of Misc Concepts
PPTX
Python Programming Essentials - M35 - Iterators & Generators
PPTX
Python Programming Essentials - M29 - Python Interpreter and Files
PPTX
Python Programming Essentials - M28 - Debugging with pdb
PPTX
Python Programming Essentials - M27 - Logging module
PPTX
Python Programming Essentials - M24 - math module
PPTX
Python Programming Essentials - M23 - datetime module
PPTX
Python Programming Essentials - M22 - File Operations
PPTX
Python Programming Essentials - M21 - Exception Handling
PPTX
Python Programming Essentials - M20 - Classes and Objects
PPTX
Python Programming Essentials - M19 - Namespaces, Global Variables and Docstr...
PPTX
Python Programming Essentials - M18 - Modules and Packages
PPTX
Python Programming Essentials - M17 - Functions
PPTX
Python Programming Essentials - M16 - Control Flow Statements and Loops
PPTX
Python Programming Essentials - M15 - References
PPTX
Python Programming Essentials - M14 - Dictionaries
PPTX
Python Programming Essentials - M12 - Lists
PPTX
Python Programming Essentials - M10 - Numbers and Artihmetic Operators
Python Programming Essentials - M44 - Overview of Web Development
Python Programming Essentials - M40 - Invoking External Programs
Python Programming Essentials - M37 - Brief Overview of Misc Concepts
Python Programming Essentials - M35 - Iterators & Generators
Python Programming Essentials - M29 - Python Interpreter and Files
Python Programming Essentials - M28 - Debugging with pdb
Python Programming Essentials - M27 - Logging module
Python Programming Essentials - M24 - math module
Python Programming Essentials - M23 - datetime module
Python Programming Essentials - M22 - File Operations
Python Programming Essentials - M21 - Exception Handling
Python Programming Essentials - M20 - Classes and Objects
Python Programming Essentials - M19 - Namespaces, Global Variables and Docstr...
Python Programming Essentials - M18 - Modules and Packages
Python Programming Essentials - M17 - Functions
Python Programming Essentials - M16 - Control Flow Statements and Loops
Python Programming Essentials - M15 - References
Python Programming Essentials - M14 - Dictionaries
Python Programming Essentials - M12 - Lists
Python Programming Essentials - M10 - Numbers and Artihmetic Operators

Recently uploaded (20)

PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PPTX
Cloud computing and distributed systems.
PDF
GDG Cloud Iasi [PUBLIC] Florian Blaga - Unveiling the Evolution of Cybersecur...
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
NewMind AI Monthly Chronicles - July 2025
PDF
Modernizing your data center with Dell and AMD
PDF
madgavkar20181017ppt McKinsey Presentation.pdf
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Advanced IT Governance
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Advanced Soft Computing BINUS July 2025.pdf
PDF
cuic standard and advanced reporting.pdf
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
How Onsite IT Support Drives Business Efficiency, Security, and Growth.pdf
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Advanced methodologies resolving dimensionality complications for autism neur...
Cloud computing and distributed systems.
GDG Cloud Iasi [PUBLIC] Florian Blaga - Unveiling the Evolution of Cybersecur...
20250228 LYD VKU AI Blended-Learning.pptx
Understanding_Digital_Forensics_Presentation.pptx
NewMind AI Monthly Chronicles - July 2025
Modernizing your data center with Dell and AMD
madgavkar20181017ppt McKinsey Presentation.pdf
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
“AI and Expert System Decision Support & Business Intelligence Systems”
Advanced IT Governance
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Diabetes mellitus diagnosis method based random forest with bat algorithm
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Advanced Soft Computing BINUS July 2025.pdf
cuic standard and advanced reporting.pdf
The Rise and Fall of 3GPP – Time for a Sabbatical?
How Onsite IT Support Drives Business Efficiency, Security, and Growth.pdf
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx

Python Programming Essentials - M5 - Variables

Editor's Notes

  • #3: Example is from Python shell 100 is a constant Counter is a variable
  • #5: We will talk more about types later in this module
  • #8: There are multiple number types, which we’ll see soon
  • #9: This is just a very brief intro to all data types mentioned in previous slide Things after a # are comments
  • #10: List if a sequence of things Tuple is also a sequence of things Difference between list and tuple: a list is mutable (can be modified) while a tuple is immutable (cannot be modified)
  • #11: Disctionary is a bunch of key-value pairs
  • #12: The ‘type’ function returns the datatype of the variable Type of a variable = the type of data it is storing currently The notion of variable type in Python is different in Python versus C/C++/Java. Python is dynamically typed which means that the variable can hold different types of data during its lifetime. However, C/C++/Java are statically types, which means that the variable can hold only one type of data during its entire lifetime When a constant is passed to the type function, Python internally creates a temporary variables and passes that to the function Float is another of number type
  • #15: x can be assigned any value. There is no type tied to variable x it can be assigned any value we like.
  • #19: An operation like 10 + “10” would work in a weakly typed language like perl, it would do the conversions implicitly Python does not allow this. You have to type conversion yourself.
  • #21: 1var : cannot start a variable name with integer
  • #22: 1var : cannot start a variable name with integer
  • #23: myVar == myvar : variables are case sensitive
  • #24: On accessing a non existing variable python throws NameError