SlideShare a Scribd company logo
1 of 28
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

What's hot

4 operators, expressions &amp; statements
4  operators, expressions &amp; statements4  operators, expressions &amp; statements
4 operators, expressions &amp; statementsMomenMostafa
 
My programming final proj. (1)
My programming final proj. (1)My programming final proj. (1)
My programming final proj. (1)aeden_brines
 
c++ pointers by Amir Hamza Khan (SZABISTIAN)
c++ pointers by Amir Hamza Khan (SZABISTIAN)c++ pointers by Amir Hamza Khan (SZABISTIAN)
c++ pointers by Amir Hamza Khan (SZABISTIAN)Ameer Hamxa
 
FP 201 Unit 2 - Part 3
FP 201 Unit 2 - Part 3FP 201 Unit 2 - Part 3
FP 201 Unit 2 - Part 3rohassanie
 
Asterisk: PVS-Studio Takes Up Telephony
Asterisk: PVS-Studio Takes Up TelephonyAsterisk: PVS-Studio Takes Up Telephony
Asterisk: PVS-Studio Takes Up TelephonyAndrey Karpov
 
Pointers & References in C++
Pointers & References in C++Pointers & References in C++
Pointers & References in C++Ilio Catallo
 
Demonstration on keyword
Demonstration on keywordDemonstration on keyword
Demonstration on keyworddeepalishinkar1
 
Python decorators
Python decoratorsPython decorators
Python decoratorsAlex Su
 
Let's make a contract: the art of designing a Java API
Let's make a contract: the art of designing a Java APILet's make a contract: the art of designing a Java API
Let's make a contract: the art of designing a Java APIMario Fusco
 
Decorators in Python
Decorators in PythonDecorators in Python
Decorators in PythonBen James
 
Advanced Patterns with io.ReadWriter
Advanced Patterns with io.ReadWriterAdvanced Patterns with io.ReadWriter
Advanced Patterns with io.ReadWriterWeaveworks
 
9 character string &amp; string library
9  character string &amp; string library9  character string &amp; string library
9 character string &amp; string libraryMomenMostafa
 
CONST-- once initialised, no one can change it
CONST-- once initialised, no one can change itCONST-- once initialised, no one can change it
CONST-- once initialised, no one can change itAjay Chimmani
 

What's hot (20)

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

Similar to Python Programming Essentials - M5 - Variables

Variables in python
Variables in pythonVariables in python
Variables in pythonJaya Kumari
 
Fundamentals of programming angeli
Fundamentals of programming angeliFundamentals of programming angeli
Fundamentals of programming angelibergonio11339481
 
PHP Built-in String Validation Functions
PHP Built-in String Validation FunctionsPHP Built-in String Validation Functions
PHP Built-in String Validation FunctionsAung Khant
 
Fpga 06-data-types-system-tasks-compiler-directives
Fpga 06-data-types-system-tasks-compiler-directivesFpga 06-data-types-system-tasks-compiler-directives
Fpga 06-data-types-system-tasks-compiler-directivesMalik Tauqir Hasan
 
C programming language tutorial
C programming language tutorial C programming language tutorial
C programming language tutorial javaTpoint s
 
Python Programming Essentials - M35 - Iterators & Generators
Python Programming Essentials - M35 - Iterators & GeneratorsPython Programming Essentials - M35 - Iterators & Generators
Python Programming Essentials - M35 - Iterators & GeneratorsP3 InfoTech Solutions Pvt. Ltd.
 
Nitin Mishra 0301EC201039 Internship PPT.pptx
Nitin Mishra 0301EC201039 Internship PPT.pptxNitin Mishra 0301EC201039 Internship PPT.pptx
Nitin Mishra 0301EC201039 Internship PPT.pptxshivam460694
 
IT2255 Web Essentials - Unit III Client-Side Processing and Scripting
IT2255 Web Essentials - Unit III Client-Side Processing and ScriptingIT2255 Web Essentials - Unit III Client-Side Processing and Scripting
IT2255 Web Essentials - Unit III Client-Side Processing and Scriptingpkaviya
 
Learn To Code: Introduction to java
Learn To Code: Introduction to javaLearn To Code: Introduction to java
Learn To Code: Introduction to javaSadhanaParameswaran
 
Lecture 2 php basics (1)
Lecture 2  php basics (1)Lecture 2  php basics (1)
Lecture 2 php basics (1)Core Lee
 

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

unit 1 (1).pptx
unit 1 (1).pptxunit 1 (1).pptx
unit 1 (1).pptx
 
Variables and Data Types
Variables and Data TypesVariables and Data Types
Variables and Data Types
 
Variables in python
Variables in pythonVariables in python
Variables in python
 
Fundamentals of programming angeli
Fundamentals of programming angeliFundamentals of programming angeli
Fundamentals of programming angeli
 
Python Programming Essentials - M7 - Strings
Python Programming Essentials - M7 - StringsPython Programming Essentials - M7 - Strings
Python Programming Essentials - M7 - Strings
 
Introduction to php basics
Introduction to php   basicsIntroduction to php   basics
Introduction to php basics
 
PHP Built-in String Validation Functions
PHP Built-in String Validation FunctionsPHP Built-in String Validation Functions
PHP Built-in String Validation Functions
 
Fpga 06-data-types-system-tasks-compiler-directives
Fpga 06-data-types-system-tasks-compiler-directivesFpga 06-data-types-system-tasks-compiler-directives
Fpga 06-data-types-system-tasks-compiler-directives
 
C programming language tutorial
C programming language tutorial C programming language tutorial
C programming language tutorial
 
Object Oriented Programming with C++
Object Oriented Programming with C++Object Oriented Programming with C++
Object Oriented Programming with C++
 
Lecture06
Lecture06Lecture06
Lecture06
 
Python Programming Essentials - M35 - Iterators & Generators
Python Programming Essentials - M35 - Iterators & GeneratorsPython Programming Essentials - M35 - Iterators & Generators
Python Programming Essentials - M35 - Iterators & Generators
 
C# - Part 1
C# - Part 1C# - Part 1
C# - Part 1
 
Nitin Mishra 0301EC201039 Internship PPT.pptx
Nitin Mishra 0301EC201039 Internship PPT.pptxNitin Mishra 0301EC201039 Internship PPT.pptx
Nitin Mishra 0301EC201039 Internship PPT.pptx
 
C language basics
C language basicsC language basics
C language basics
 
Java Fx
Java FxJava Fx
Java Fx
 
IT2255 Web Essentials - Unit III Client-Side Processing and Scripting
IT2255 Web Essentials - Unit III Client-Side Processing and ScriptingIT2255 Web Essentials - Unit III Client-Side Processing and Scripting
IT2255 Web Essentials - Unit III Client-Side Processing and Scripting
 
Learn To Code: Introduction to java
Learn To Code: Introduction to javaLearn To Code: Introduction to java
Learn To Code: Introduction to java
 
Lecture02
Lecture02Lecture02
Lecture02
 
Lecture 2 php basics (1)
Lecture 2  php basics (1)Lecture 2  php basics (1)
Lecture 2 php basics (1)
 

More from P3 InfoTech Solutions Pvt. Ltd.

Python Programming Essentials - M44 - Overview of Web Development
Python Programming Essentials - M44 - Overview of Web DevelopmentPython Programming Essentials - M44 - Overview of Web Development
Python Programming Essentials - M44 - Overview of Web DevelopmentP3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M40 - Invoking External Programs
Python Programming Essentials - M40 - Invoking External ProgramsPython Programming Essentials - M40 - Invoking External Programs
Python Programming Essentials - M40 - Invoking External ProgramsP3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M37 - Brief Overview of Misc Concepts
Python Programming Essentials - M37 - Brief Overview of Misc ConceptsPython Programming Essentials - M37 - Brief Overview of Misc Concepts
Python Programming Essentials - M37 - Brief Overview of Misc ConceptsP3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M29 - Python Interpreter and Files
Python Programming Essentials - M29 - Python Interpreter and FilesPython Programming Essentials - M29 - Python Interpreter and Files
Python Programming Essentials - M29 - Python Interpreter and FilesP3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M28 - Debugging with pdb
Python Programming Essentials - M28 - Debugging with pdbPython Programming Essentials - M28 - Debugging with pdb
Python Programming Essentials - M28 - Debugging with pdbP3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M21 - Exception Handling
Python Programming Essentials - M21 - Exception HandlingPython Programming Essentials - M21 - Exception Handling
Python Programming Essentials - M21 - Exception HandlingP3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M20 - Classes and Objects
Python Programming Essentials - M20 - Classes and ObjectsPython Programming Essentials - M20 - Classes and Objects
Python Programming Essentials - M20 - Classes and ObjectsP3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M19 - Namespaces, Global Variables and Docstr...
Python Programming Essentials - M19 - Namespaces, Global Variables and Docstr...Python Programming Essentials - M19 - Namespaces, Global Variables and Docstr...
Python Programming Essentials - M19 - Namespaces, Global Variables and Docstr...P3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M18 - Modules and Packages
Python Programming Essentials - M18 - Modules and PackagesPython Programming Essentials - M18 - Modules and Packages
Python Programming Essentials - M18 - Modules and PackagesP3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M16 - Control Flow Statements and Loops
Python Programming Essentials - M16 - Control Flow Statements and LoopsPython Programming Essentials - M16 - Control Flow Statements and Loops
Python Programming Essentials - M16 - Control Flow Statements and LoopsP3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M10 - Numbers and Artihmetic Operators
Python Programming Essentials - M10 - Numbers and Artihmetic OperatorsPython Programming Essentials - M10 - Numbers and Artihmetic Operators
Python Programming Essentials - M10 - Numbers and Artihmetic OperatorsP3 InfoTech Solutions Pvt. Ltd.
 

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

Python Programming Essentials - M44 - Overview of Web Development
Python Programming Essentials - M44 - Overview of Web DevelopmentPython Programming Essentials - M44 - Overview of Web Development
Python Programming Essentials - M44 - Overview of Web Development
 
Python Programming Essentials - M40 - Invoking External Programs
Python Programming Essentials - M40 - Invoking External ProgramsPython Programming Essentials - M40 - Invoking External Programs
Python Programming Essentials - M40 - Invoking External Programs
 
Python Programming Essentials - M37 - Brief Overview of Misc Concepts
Python Programming Essentials - M37 - Brief Overview of Misc ConceptsPython Programming Essentials - M37 - Brief Overview of Misc Concepts
Python Programming Essentials - M37 - Brief Overview of Misc Concepts
 
Python Programming Essentials - M29 - Python Interpreter and Files
Python Programming Essentials - M29 - Python Interpreter and FilesPython Programming Essentials - M29 - Python Interpreter and Files
Python Programming Essentials - M29 - Python Interpreter and Files
 
Python Programming Essentials - M28 - Debugging with pdb
Python Programming Essentials - M28 - Debugging with pdbPython Programming Essentials - M28 - Debugging with pdb
Python Programming Essentials - M28 - Debugging with pdb
 
Python Programming Essentials - M27 - Logging module
Python Programming Essentials - M27 - Logging modulePython Programming Essentials - M27 - Logging module
Python Programming Essentials - M27 - Logging module
 
Python Programming Essentials - M24 - math module
Python Programming Essentials - M24 - math modulePython Programming Essentials - M24 - math module
Python Programming Essentials - M24 - math module
 
Python Programming Essentials - M23 - datetime module
Python Programming Essentials - M23 - datetime modulePython Programming Essentials - M23 - datetime module
Python Programming Essentials - M23 - datetime module
 
Python Programming Essentials - M22 - File Operations
Python Programming Essentials - M22 - File OperationsPython Programming Essentials - M22 - File Operations
Python Programming Essentials - M22 - File Operations
 
Python Programming Essentials - M21 - Exception Handling
Python Programming Essentials - M21 - Exception HandlingPython Programming Essentials - M21 - Exception Handling
Python Programming Essentials - M21 - Exception Handling
 
Python Programming Essentials - M20 - Classes and Objects
Python Programming Essentials - M20 - Classes and ObjectsPython Programming Essentials - M20 - Classes and Objects
Python Programming Essentials - M20 - Classes and Objects
 
Python Programming Essentials - M19 - Namespaces, Global Variables and Docstr...
Python Programming Essentials - M19 - Namespaces, Global Variables and Docstr...Python Programming Essentials - M19 - Namespaces, Global Variables and Docstr...
Python Programming Essentials - M19 - Namespaces, Global Variables and Docstr...
 
Python Programming Essentials - M18 - Modules and Packages
Python Programming Essentials - M18 - Modules and PackagesPython Programming Essentials - M18 - Modules and Packages
Python Programming Essentials - M18 - Modules and Packages
 
Python Programming Essentials - M17 - Functions
Python Programming Essentials - M17 - FunctionsPython Programming Essentials - M17 - Functions
Python Programming Essentials - M17 - Functions
 
Python Programming Essentials - M16 - Control Flow Statements and Loops
Python Programming Essentials - M16 - Control Flow Statements and LoopsPython Programming Essentials - M16 - Control Flow Statements and Loops
Python Programming Essentials - M16 - Control Flow Statements and Loops
 
Python Programming Essentials - M15 - References
Python Programming Essentials - M15 - ReferencesPython Programming Essentials - M15 - References
Python Programming Essentials - M15 - References
 
Python Programming Essentials - M14 - Dictionaries
Python Programming Essentials - M14 - DictionariesPython Programming Essentials - M14 - Dictionaries
Python Programming Essentials - M14 - Dictionaries
 
Python Programming Essentials - M12 - Lists
Python Programming Essentials - M12 - ListsPython Programming Essentials - M12 - Lists
Python Programming Essentials - M12 - Lists
 
Python Programming Essentials - M10 - Numbers and Artihmetic Operators
Python Programming Essentials - M10 - Numbers and Artihmetic OperatorsPython Programming Essentials - M10 - Numbers and Artihmetic Operators
Python Programming Essentials - M10 - Numbers and Artihmetic Operators
 
Python Programming Essentials - M8 - String Methods
Python Programming Essentials - M8 - String MethodsPython Programming Essentials - M8 - String Methods
Python Programming Essentials - M8 - String Methods
 

Recently uploaded

Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piececharlottematthew16
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Manik S Magar
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfPrecisely
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningLars Bell
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clashcharlottematthew16
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostZilliz
 
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo DayH2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo DaySri Ambati
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfRankYa
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsMiki Katsuragi
 

Recently uploaded (20)

Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piece
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine Tuning
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clash
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
 
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo DayH2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdf
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering Tips
 

Python Programming Essentials - M5 - Variables

  • 1. http://www.skillbrew.com /SkillbrewTalent brewed by the industry itself Variables Pavan Verma @YinYangPavan Python Programming Essentials
  • 2. © SkillBrew http://skillbrew.com What is a variable 2 >>> print 100 100 >>> counter = 100 >>> print counter 100
  • 3. © 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
  • 4. © 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’
  • 5. © SkillBrew http://skillbrew.com Multiple variable assignments in one statement 5 >>> length = height = breadth = 2 >>> length 2 >>> height 2 >>> breadth 2 2length 2breadth 2height
  • 6. © SkillBrew http://skillbrew.com Multiple variable assignments in one statement 6 >>> x, y = 2, 3 >>> x 2 >>> y 3 2 3y x
  • 7. © SkillBrew http://skillbrew.com Standard data types Standard Data Types Number String List Tuple Dictionary 7
  • 8. © SkillBrew http://skillbrew.com Standard data types (2) >>> length = 10 #integer >>> length 10 >>> motive = 'to learn Python' #string >>> motive 'to learn Python' 8
  • 9. © 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
  • 10. © SkillBrew http://skillbrew.com Standard data types (4) >>> contacts = {'Monty': 123445, 'Guido': 67788} #dictionary >>> contacts {'Monty': 123445, 'Guido': 67788} 10
  • 11. © SkillBrew http://skillbrew.com type function >>> var = "foo" >>> type(var) <type 'str'> >>> type(8) <type 'int'> >>> type(9.0) <type 'float'> 11
  • 12. © SkillBrew http://skillbrew.com type function >>> type([1, 2, 3]) <type 'list'> >>> type((1, 2, 3)) <type 'tuple'> >>> type({'1': 'one'}) <type 'dict'> 12
  • 13. © 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
  • 14. © 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'>
  • 15. © 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
  • 16. © 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)
  • 17. © 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)
  • 18. © 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.
  • 19. © 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
  • 20. © SkillBrew http://skillbrew.com Variable naming rules (2) 20 length Length myLength _x get_index num12 1var
  • 21. © SkillBrew http://skillbrew.com Variable naming rules (3) >>> 1var = 10 SyntaxError: invalid syntax >>> var1 = 10 >>> v1VAR = 10 >>> myVar = 10 >>> _var = 10 >>> Var = 10 21
  • 22. © SkillBrew http://skillbrew.com Variable naming rules (4) >>> myvar = 20 >>> myVar 10 >>> myvar 20 >>> myVar == myvar False 22
  • 23. © 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
  • 24. © 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
  • 25. © SkillBrew http://skillbrew.com Cannot use reserved keywords as variable names >>> and = 1 SyntaxError: invalid syntax >>> if = 1 SyntaxError: invalid syntax >>> 25
  • 26. © 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
  • 27. © 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. 28

Editor's Notes

  1. Example is from Python shell 100 is a constant Counter is a variable
  2. We will talk more about types later in this module
  3. There are multiple number types, which we’ll see soon
  4. This is just a very brief intro to all data types mentioned in previous slide Things after a # are comments
  5. 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)
  6. Disctionary is a bunch of key-value pairs
  7. 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
  8. x can be assigned any value. There is no type tied to variable x it can be assigned any value we like.
  9. 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.
  10. 1var : cannot start a variable name with integer
  11. 1var : cannot start a variable name with integer
  12. myVar == myvar : variables are case sensitive
  13. On accessing a non existing variable python throws NameError