SlideShare a Scribd company logo
1 of 44
Download to read offline
Jagannath Institute of Management Sciences
Vasant Kunj-II, New Delhi - 110070
Subject Name: BVITSD 205 : Programming And
Problem Solving Though PYTHON
Department of Information Technology
Created By: Dr. Arpana Chaturvedi
@Dr. Arpana Chaturvedi
Subject: BVITSD 205 : PROGRAMMING AND
PROBLEM SOLVING THOUGH PYTHON
Topic: Unit II- Python Variables and Data Types
@Dr. Arpana Chaturvedi
Unit-II Python Variables and Data Types
@Dr. Arpana Chaturvedi
An Introduction to
Variables
Data Types
Types
Conversion
Local and
Global
Variables
Variable Naming
Rules
Assigning
Variables
Numbers
Strings
int()
float()
Local
Variables
Global
Variables
str()
bool()
list()
List
Tuple
Dictionaries
Multiple
Variables
Deleting
Variables
Unit-II Python Variables
@Dr. Arpana Chaturvedi
Python Variables
X=45
Type=Integer
name=“ JIMS ”
Type=String
nums=[ 1,3,5,7 ] Type=Lists
45
x
“ JIMS ”
name
[ 1,3,5,7 ]
nums
▰ A variable is a container for a value. It can be assigned a name, you can use it to
refer to it later in the program.
▰ Based on the value assigned, the interpreter decides its data type. You can always
store a different type in a variable.
Unit-II Python Variable Naming Convention
There are certain rules to what you can name a variable(called an identifier).
▰ Python variables can only begin with a letter(A-Z/a-z) or an underscore(_).
▰ The rest of the identifier may contain letters(A-Z/a-z), underscores(_), and
numbers(0-9).
▰ Python is case-sensitive, and so are Python identifiers. Name and name are two
different identifiers.
▰ Reserved words (keywords) cannot be used as identifier names.
@Dr. Arpana Chaturvedi
Unit-II Python Variables Examples
@Dr. Arpana Chaturvedi
Unit-II 2. Assigning and Reassigning Python
Variables
▰ To assign a value to Python variables, you don‟t need to declare its type.
▰ You name it according to the rules of variable naming convention, and type the
value after the equal sign(=).
@Dr. Arpana Chaturvedi
You can‟t put the identifier on the right-hand side of the equal sign, though. The
following code causes an error.
Neither can you assign Python variables to a keyword.
Unit-II 3. Multiple Assignment
▰ You can assign values to multiple Python variables in one statement.
▰ Or you can assign the same value to multiple Python variables.
@Dr. Arpana Chaturvedi
Unit-II 4. Swapping Python Variables
▰ Swapping means interchanging values. To swap Python variables, you don‟t need
to do much.
@Dr. Arpana Chaturvedi
Unit-II 4.Deleting Python Variables
▰ You can also delete Python variables using the keyword „del‟.
@Dr. Arpana Chaturvedi
Unit-II Python Datatypes
▰ Although we don‟t have to declare a type for Python variables, a value does have a
type. This information is vital to the interpreter.
▰ Python supports the following data types.
1. Python Numbers
2. Strings
3. Python Lists
4. Python Tuples
5. Dictionaries
7. Sets
@Dr. Arpana Chaturvedi
Unit-II 1. Python Numbers
▰ There are four numeric Python data types.
a. int
b. float
c. long
d. complex
@Dr. Arpana Chaturvedi
Unit-II 1. Python Numbers
a. int
▰ int stands for integer. This Python Data Type holds signed integers. We can use the
type() function to find which class it belongs to.
@Dr. Arpana Chaturvedi
An integer can be of any length, with the only limitation being the available memory.
Unit-II 1. Python Numbers
b. float
▰ This Python Data Type holds floating-point real values. An int can only store the
number 3, but float can store 3.25 if you want.
@Dr. Arpana Chaturvedi
c. long
This Python Data type holds a long integer of unlimited length. But this construct does
not exist in Python 3.x.
Unit-II 1. Python Numbers
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.
@Dr. Arpana Chaturvedi
Use the isinstance() function to tell if Python variables belong to a particular class. It
takes two parameters- the variable/value, and the class.
Unit-II 2. Strings
▰ A String is a sequence of characters. Python does not have a char data type, unlike
C++ or Java. You can delimit a string using single quotes or double-quotes.
@Dr. Arpana Chaturvedi
a. Spanning a String Across Lines
▰ To span a string across multiple lines, you can use triple quotes.
As you can see, the quotes
preserved the formatting (n is the
escape sequence for newline, t is
for tab).
Unit-II 2. Strings
@Dr. Arpana Chaturvedi
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.
You can also display a burst of characters in a string using the slicing operator [].
This prints the characters from 0 to 5.
Unit-II 2. Strings
@Dr. Arpana Chaturvedi
c. String Formatters
String formatters allow us to print characters and values at once. You can use the %
operator.
Or you can use the format method.
A third option is to use f-strings.
Unit-II 2. Strings
@Dr. Arpana Chaturvedi
d. String Concatenation
You can concatenate(join) strings.
However, you cannot concatenate values of different types.
Unit-II 3. Python Lists
@Dr. Arpana Chaturvedi
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.
a. Slicing a List
You can slice a list the way you‟d slice a string- with the slicing operator.
Indexing for a list begins with 0, like for a string. A Python doesn‟t have arrays.
Unit-II 3. Python Lists
@Dr. Arpana Chaturvedi
c. Length of a List
Python supports an inbuilt function to calculate the length of a list.
c. Reassigning Elements of a List
A list is mutable. This means that you can reassign elements later on.
Unit-II 3. Python Lists
@Dr. Arpana Chaturvedi
d. Iterating on the List
To iterate over the list we can use the for loop. By iterating, we can access each
element one by one which is very helpful when we need to perform some operations
on each element of list.
Unit-II 3. Python Lists
@Dr. Arpana Chaturvedi
e. Multidimensional Lists
A list may have more than one dimension.
Unit-II Python Tuples
▰ A tuple is like a list. You declare it using parentheses instead.
@Dr. Arpana Chaturvedi
a. Accessing and Slicing a Tuple
You access a tuple the same way as you‟d access a list. The same goes for slicing
it.
Unit-II Python Tuples
@Dr. Arpana Chaturvedi
b. A tuple is Immutable
Python tuple is immutable. Once declared, you can‟t change its size or elements.
Unit-II 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(:).
▰ The type() function works with dictionaries too.
@Dr. Arpana Chaturvedi
Unit-II 5. Dictionaries
▰ a. Accessing a Value
▰ To access a value, you mention the key in square brackets.
▰ b. Reassigning Elements
▰ You can reassign a value to a key.
@Dr. Arpana Chaturvedi
Unit-II 5. Dictionaries
▰ c. List of Keys
▰ Use the keys() function to get a list of keys in the dictionary.
@Dr. Arpana Chaturvedi
Unit-II 6. bool
▰ A Boolean value can be True or False.
@Dr. Arpana Chaturvedi
Unit-II 7. Sets
▰ 7. Sets
▰ A set can have a list of values. Define it using curly braces.
@Dr. Arpana Chaturvedi
It returns only one instance of any value present more than once.
However, a set is unordered, so it doesn‟t support indexing.
Unit-II 7. Sets
▰ 7. Sets
▰ Also, it is mutable. You can change its elements or add more. Use the add() and
remove() methods to do so.
@Dr. Arpana Chaturvedi
Unit-II Python Type Conversion
▰ Since Python is dynamically-typed, you may want to convert a value into another
type. Python supports a list of functions for the same.
▰ 1. int()
▰ It converts the value into an int.
@Dr. Arpana Chaturvedi
Notice how it truncated 0.7 instead of rounding the number off to 4. You can also turn
a Boolean into an int.
Unit-II Python Type Conversion
▰ However, you cannot turn a string into an int. It throws an error.
@Dr. Arpana Chaturvedi
 However, if the string has only numbers, then you can.
Unit-II Python Type Conversion
▰ 2. float()
▰ It converts the value into a float.
@Dr. Arpana Chaturvedi
However, this number works even without the float() function.
You can also use ‘e’ to denote an exponential number.
Unit-II Python Type Conversion
▰ 3. str()
▰ It converts the value into a string.
@Dr. Arpana Chaturvedi
Unit-II Python Type Conversion
▰ 4. bool()
▰ It converts the value into a Boolean.
@Dr. Arpana Chaturvedi
Unit-II 5. Python Sets
▰ 5. set()
▰ It converts the value into a set.
@Dr. Arpana Chaturvedi
Unit-II 6. Python list()
▰ 6. list()
▰ It converts the value into a list.
@Dr. Arpana Chaturvedi
Unit-II 7. Python tuple
▰ 7. tuple()
▰ It converts the value into a tuple.
@Dr. Arpana Chaturvedi
You can try your own combinations. Also
try composite functions.
Unit-II Python Local and Global 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.
@Dr. Arpana Chaturvedi
Here, the variable num is local to function func1().
Unit-II Python Local and Global Variables
▰ 2. Global Variables
▰ When you declare a variable outside any context/scope, it is visible in the whole
program.
@Dr. Arpana Chaturvedi
Unit-II Python Local and Global Variables
▰ 2. Global Variables
▰ You can use the „global‟ keyword when you want to treat a variable as global in a
local scope.
@Dr. Arpana Chaturvedi
Unit-II Python Variables Assignment
▰ What are variables and data types in Python?
▰ What is type () in Python?
▰ What are Local and Global variables in Python?
▰ Explain various naming rules for Python Variables.
▰ How to display part of a string?
@Dr. Arpana Chaturvedi
Thank You !!

More Related Content

Similar to Unit 2 Part 1.2 Data Types.pdf

Basic of Python- Hands on Session
Basic of Python- Hands on SessionBasic of Python- Hands on Session
Basic of Python- Hands on SessionDharmesh Tank
 
Python_Unit_1.pdf
Python_Unit_1.pdfPython_Unit_1.pdf
Python_Unit_1.pdfalaparthi
 
Python For Data Science.pptx
Python For Data Science.pptxPython For Data Science.pptx
Python For Data Science.pptxrohithprabhas1
 
Zero to Hero - Introduction to Python3
Zero to Hero - Introduction to Python3Zero to Hero - Introduction to Python3
Zero to Hero - Introduction to Python3Chariza Pladin
 
Introduction to Python for Data Science and Machine Learning
Introduction to Python for Data Science and Machine Learning Introduction to Python for Data Science and Machine Learning
Introduction to Python for Data Science and Machine Learning ParrotAI
 
trisha comp ppt.pptx
trisha comp ppt.pptxtrisha comp ppt.pptx
trisha comp ppt.pptxTapaswini14
 
13- Data and Its Types presentation kafss
13- Data and Its Types presentation kafss13- Data and Its Types presentation kafss
13- Data and Its Types presentation kafssAliKhokhar33
 
Python (Data Analysis) cleaning and visualize
Python (Data Analysis) cleaning and visualizePython (Data Analysis) cleaning and visualize
Python (Data Analysis) cleaning and visualizeIruolagbePius
 
Basic data types in python
Basic data types in pythonBasic data types in python
Basic data types in pythonsunilchute1
 
L6 - Loops.pptx
L6 - Loops.pptxL6 - Loops.pptx
L6 - Loops.pptxEloAOgardo
 
L6 - Loops.pptx
L6 - Loops.pptxL6 - Loops.pptx
L6 - Loops.pptxEloAOgardo
 
L5 - Data Types, Keywords.pptx
L5 - Data Types, Keywords.pptxL5 - Data Types, Keywords.pptx
L5 - Data Types, Keywords.pptxEloAOgardo
 
Python interview questions
Python interview questionsPython interview questions
Python interview questionsPragati Singh
 

Similar to Unit 2 Part 1.2 Data Types.pdf (20)

Basic of Python- Hands on Session
Basic of Python- Hands on SessionBasic of Python- Hands on Session
Basic of Python- Hands on Session
 
Python_Unit_1.pdf
Python_Unit_1.pdfPython_Unit_1.pdf
Python_Unit_1.pdf
 
Python For Data Science.pptx
Python For Data Science.pptxPython For Data Science.pptx
Python For Data Science.pptx
 
Unit 2 python
Unit 2 pythonUnit 2 python
Unit 2 python
 
Python basics
Python basicsPython basics
Python basics
 
Zero to Hero - Introduction to Python3
Zero to Hero - Introduction to Python3Zero to Hero - Introduction to Python3
Zero to Hero - Introduction to Python3
 
Introduction to Python for Data Science and Machine Learning
Introduction to Python for Data Science and Machine Learning Introduction to Python for Data Science and Machine Learning
Introduction to Python for Data Science and Machine Learning
 
trisha comp ppt.pptx
trisha comp ppt.pptxtrisha comp ppt.pptx
trisha comp ppt.pptx
 
13- Data and Its Types presentation kafss
13- Data and Its Types presentation kafss13- Data and Its Types presentation kafss
13- Data and Its Types presentation kafss
 
Python (Data Analysis) cleaning and visualize
Python (Data Analysis) cleaning and visualizePython (Data Analysis) cleaning and visualize
Python (Data Analysis) cleaning and visualize
 
Data Types In Python.pptx
Data Types In Python.pptxData Types In Python.pptx
Data Types In Python.pptx
 
Basic data types in python
Basic data types in pythonBasic data types in python
Basic data types in python
 
Python slide.1
Python slide.1Python slide.1
Python slide.1
 
L6 - Loops.pptx
L6 - Loops.pptxL6 - Loops.pptx
L6 - Loops.pptx
 
L6 - Loops.pptx
L6 - Loops.pptxL6 - Loops.pptx
L6 - Loops.pptx
 
Structured Languages
Structured LanguagesStructured Languages
Structured Languages
 
L5 - Data Types, Keywords.pptx
L5 - Data Types, Keywords.pptxL5 - Data Types, Keywords.pptx
L5 - Data Types, Keywords.pptx
 
Python
PythonPython
Python
 
set.pptx
set.pptxset.pptx
set.pptx
 
Python interview questions
Python interview questionsPython interview questions
Python interview questions
 

More from Arpana Awasthi

Introduction To Python.pdf
Introduction To Python.pdfIntroduction To Python.pdf
Introduction To Python.pdfArpana Awasthi
 
Unit 2 Part 1 POLYMORPHISM.pdf
Unit 2 Part 1 POLYMORPHISM.pdfUnit 2 Part 1 POLYMORPHISM.pdf
Unit 2 Part 1 POLYMORPHISM.pdfArpana Awasthi
 
Unit 2 Part 1 Constructors.pdf
Unit 2 Part 1 Constructors.pdfUnit 2 Part 1 Constructors.pdf
Unit 2 Part 1 Constructors.pdfArpana Awasthi
 
Eclipse - GUI Palette
Eclipse - GUI Palette Eclipse - GUI Palette
Eclipse - GUI Palette Arpana Awasthi
 
Introduction to Eclipse
Introduction to Eclipse Introduction to Eclipse
Introduction to Eclipse Arpana Awasthi
 
Arrays 2 Dimensional Unit 2 Part 1.pdf
Arrays 2 Dimensional Unit 2 Part 1.pdfArrays 2 Dimensional Unit 2 Part 1.pdf
Arrays 2 Dimensional Unit 2 Part 1.pdfArpana Awasthi
 
Arrays Fundamentals Unit II
Arrays  Fundamentals Unit IIArrays  Fundamentals Unit II
Arrays Fundamentals Unit IIArpana Awasthi
 
Machine Learning: Need of Machine Learning, Its Challenges and its Applications
Machine Learning: Need of Machine Learning, Its Challenges and its ApplicationsMachine Learning: Need of Machine Learning, Its Challenges and its Applications
Machine Learning: Need of Machine Learning, Its Challenges and its ApplicationsArpana Awasthi
 
File Handling in C Part I
File Handling in C Part IFile Handling in C Part I
File Handling in C Part IArpana Awasthi
 
Role of machine learning in detection, prevention and treatment of cancer
Role of machine learning in detection, prevention and treatment of cancerRole of machine learning in detection, prevention and treatment of cancer
Role of machine learning in detection, prevention and treatment of cancerArpana Awasthi
 

More from Arpana Awasthi (12)

Unit 5 Part 1 Macros
Unit 5 Part 1 MacrosUnit 5 Part 1 Macros
Unit 5 Part 1 Macros
 
Introduction To Python.pdf
Introduction To Python.pdfIntroduction To Python.pdf
Introduction To Python.pdf
 
Unit 2 Part 1 POLYMORPHISM.pdf
Unit 2 Part 1 POLYMORPHISM.pdfUnit 2 Part 1 POLYMORPHISM.pdf
Unit 2 Part 1 POLYMORPHISM.pdf
 
Unit 2 Part 1 Constructors.pdf
Unit 2 Part 1 Constructors.pdfUnit 2 Part 1 Constructors.pdf
Unit 2 Part 1 Constructors.pdf
 
Eclipse - GUI Palette
Eclipse - GUI Palette Eclipse - GUI Palette
Eclipse - GUI Palette
 
Introduction to Eclipse
Introduction to Eclipse Introduction to Eclipse
Introduction to Eclipse
 
Arrays 2 Dimensional Unit 2 Part 1.pdf
Arrays 2 Dimensional Unit 2 Part 1.pdfArrays 2 Dimensional Unit 2 Part 1.pdf
Arrays 2 Dimensional Unit 2 Part 1.pdf
 
Arrays Fundamentals Unit II
Arrays  Fundamentals Unit IIArrays  Fundamentals Unit II
Arrays Fundamentals Unit II
 
Machine Learning: Need of Machine Learning, Its Challenges and its Applications
Machine Learning: Need of Machine Learning, Its Challenges and its ApplicationsMachine Learning: Need of Machine Learning, Its Challenges and its Applications
Machine Learning: Need of Machine Learning, Its Challenges and its Applications
 
File Handling in C Part I
File Handling in C Part IFile Handling in C Part I
File Handling in C Part I
 
Programming language
Programming languageProgramming language
Programming language
 
Role of machine learning in detection, prevention and treatment of cancer
Role of machine learning in detection, prevention and treatment of cancerRole of machine learning in detection, prevention and treatment of cancer
Role of machine learning in detection, prevention and treatment of cancer
 

Recently uploaded

JohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptxJohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptxJohnPollard37
 
TEST BANK For, Information Technology Project Management 9th Edition Kathy Sc...
TEST BANK For, Information Technology Project Management 9th Edition Kathy Sc...TEST BANK For, Information Technology Project Management 9th Edition Kathy Sc...
TEST BANK For, Information Technology Project Management 9th Edition Kathy Sc...marcuskenyatta275
 
TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...
TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...
TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...TrustArc
 
Human Expert Website Manual WCAG 2.0 2.1 2.2 Audit - Digital Accessibility Au...
Human Expert Website Manual WCAG 2.0 2.1 2.2 Audit - Digital Accessibility Au...Human Expert Website Manual WCAG 2.0 2.1 2.2 Audit - Digital Accessibility Au...
Human Expert Website Manual WCAG 2.0 2.1 2.2 Audit - Digital Accessibility Au...Skynet Technologies
 
Introduction to use of FHIR Documents in ABDM
Introduction to use of FHIR Documents in ABDMIntroduction to use of FHIR Documents in ABDM
Introduction to use of FHIR Documents in ABDMKumar Satyam
 
Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptx
Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptxHarnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptx
Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptxFIDO Alliance
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontologyjohnbeverley2021
 
Continuing Bonds Through AI: A Hermeneutic Reflection on Thanabots
Continuing Bonds Through AI: A Hermeneutic Reflection on ThanabotsContinuing Bonds Through AI: A Hermeneutic Reflection on Thanabots
Continuing Bonds Through AI: A Hermeneutic Reflection on ThanabotsLeah Henrickson
 
Top 10 CodeIgniter Development Companies
Top 10 CodeIgniter Development CompaniesTop 10 CodeIgniter Development Companies
Top 10 CodeIgniter Development CompaniesTopCSSGallery
 
ADP Passwordless Journey Case Study.pptx
ADP Passwordless Journey Case Study.pptxADP Passwordless Journey Case Study.pptx
ADP Passwordless Journey Case Study.pptxFIDO Alliance
 
Event-Driven Architecture Masterclass: Engineering a Robust, High-performance...
Event-Driven Architecture Masterclass: Engineering a Robust, High-performance...Event-Driven Architecture Masterclass: Engineering a Robust, High-performance...
Event-Driven Architecture Masterclass: Engineering a Robust, High-performance...ScyllaDB
 
ERP Contender Series: Acumatica vs. Sage Intacct
ERP Contender Series: Acumatica vs. Sage IntacctERP Contender Series: Acumatica vs. Sage Intacct
ERP Contender Series: Acumatica vs. Sage IntacctBrainSell Technologies
 
Event-Driven Architecture Masterclass: Integrating Distributed Data Stores Ac...
Event-Driven Architecture Masterclass: Integrating Distributed Data Stores Ac...Event-Driven Architecture Masterclass: Integrating Distributed Data Stores Ac...
Event-Driven Architecture Masterclass: Integrating Distributed Data Stores Ac...ScyllaDB
 
How to Check CNIC Information Online with Pakdata cf
How to Check CNIC Information Online with Pakdata cfHow to Check CNIC Information Online with Pakdata cf
How to Check CNIC Information Online with Pakdata cfdanishmna97
 
WebRTC and SIP not just audio and video @ OpenSIPS 2024
WebRTC and SIP not just audio and video @ OpenSIPS 2024WebRTC and SIP not just audio and video @ OpenSIPS 2024
WebRTC and SIP not just audio and video @ OpenSIPS 2024Lorenzo Miniero
 
Hyatt driving innovation and exceptional customer experiences with FIDO passw...
Hyatt driving innovation and exceptional customer experiences with FIDO passw...Hyatt driving innovation and exceptional customer experiences with FIDO passw...
Hyatt driving innovation and exceptional customer experiences with FIDO passw...FIDO Alliance
 
Working together SRE & Platform Engineering
Working together SRE & Platform EngineeringWorking together SRE & Platform Engineering
Working together SRE & Platform EngineeringMarcus Vechiato
 
Portal Kombat : extension du réseau de propagande russe
Portal Kombat : extension du réseau de propagande russePortal Kombat : extension du réseau de propagande russe
Portal Kombat : extension du réseau de propagande russe中 央社
 
The Zero-ETL Approach: Enhancing Data Agility and Insight
The Zero-ETL Approach: Enhancing Data Agility and InsightThe Zero-ETL Approach: Enhancing Data Agility and Insight
The Zero-ETL Approach: Enhancing Data Agility and InsightSafe Software
 
Vector Search @ sw2con for slideshare.pptx
Vector Search @ sw2con for slideshare.pptxVector Search @ sw2con for slideshare.pptx
Vector Search @ sw2con for slideshare.pptxjbellis
 

Recently uploaded (20)

JohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptxJohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptx
 
TEST BANK For, Information Technology Project Management 9th Edition Kathy Sc...
TEST BANK For, Information Technology Project Management 9th Edition Kathy Sc...TEST BANK For, Information Technology Project Management 9th Edition Kathy Sc...
TEST BANK For, Information Technology Project Management 9th Edition Kathy Sc...
 
TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...
TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...
TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...
 
Human Expert Website Manual WCAG 2.0 2.1 2.2 Audit - Digital Accessibility Au...
Human Expert Website Manual WCAG 2.0 2.1 2.2 Audit - Digital Accessibility Au...Human Expert Website Manual WCAG 2.0 2.1 2.2 Audit - Digital Accessibility Au...
Human Expert Website Manual WCAG 2.0 2.1 2.2 Audit - Digital Accessibility Au...
 
Introduction to use of FHIR Documents in ABDM
Introduction to use of FHIR Documents in ABDMIntroduction to use of FHIR Documents in ABDM
Introduction to use of FHIR Documents in ABDM
 
Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptx
Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptxHarnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptx
Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptx
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 
Continuing Bonds Through AI: A Hermeneutic Reflection on Thanabots
Continuing Bonds Through AI: A Hermeneutic Reflection on ThanabotsContinuing Bonds Through AI: A Hermeneutic Reflection on Thanabots
Continuing Bonds Through AI: A Hermeneutic Reflection on Thanabots
 
Top 10 CodeIgniter Development Companies
Top 10 CodeIgniter Development CompaniesTop 10 CodeIgniter Development Companies
Top 10 CodeIgniter Development Companies
 
ADP Passwordless Journey Case Study.pptx
ADP Passwordless Journey Case Study.pptxADP Passwordless Journey Case Study.pptx
ADP Passwordless Journey Case Study.pptx
 
Event-Driven Architecture Masterclass: Engineering a Robust, High-performance...
Event-Driven Architecture Masterclass: Engineering a Robust, High-performance...Event-Driven Architecture Masterclass: Engineering a Robust, High-performance...
Event-Driven Architecture Masterclass: Engineering a Robust, High-performance...
 
ERP Contender Series: Acumatica vs. Sage Intacct
ERP Contender Series: Acumatica vs. Sage IntacctERP Contender Series: Acumatica vs. Sage Intacct
ERP Contender Series: Acumatica vs. Sage Intacct
 
Event-Driven Architecture Masterclass: Integrating Distributed Data Stores Ac...
Event-Driven Architecture Masterclass: Integrating Distributed Data Stores Ac...Event-Driven Architecture Masterclass: Integrating Distributed Data Stores Ac...
Event-Driven Architecture Masterclass: Integrating Distributed Data Stores Ac...
 
How to Check CNIC Information Online with Pakdata cf
How to Check CNIC Information Online with Pakdata cfHow to Check CNIC Information Online with Pakdata cf
How to Check CNIC Information Online with Pakdata cf
 
WebRTC and SIP not just audio and video @ OpenSIPS 2024
WebRTC and SIP not just audio and video @ OpenSIPS 2024WebRTC and SIP not just audio and video @ OpenSIPS 2024
WebRTC and SIP not just audio and video @ OpenSIPS 2024
 
Hyatt driving innovation and exceptional customer experiences with FIDO passw...
Hyatt driving innovation and exceptional customer experiences with FIDO passw...Hyatt driving innovation and exceptional customer experiences with FIDO passw...
Hyatt driving innovation and exceptional customer experiences with FIDO passw...
 
Working together SRE & Platform Engineering
Working together SRE & Platform EngineeringWorking together SRE & Platform Engineering
Working together SRE & Platform Engineering
 
Portal Kombat : extension du réseau de propagande russe
Portal Kombat : extension du réseau de propagande russePortal Kombat : extension du réseau de propagande russe
Portal Kombat : extension du réseau de propagande russe
 
The Zero-ETL Approach: Enhancing Data Agility and Insight
The Zero-ETL Approach: Enhancing Data Agility and InsightThe Zero-ETL Approach: Enhancing Data Agility and Insight
The Zero-ETL Approach: Enhancing Data Agility and Insight
 
Vector Search @ sw2con for slideshare.pptx
Vector Search @ sw2con for slideshare.pptxVector Search @ sw2con for slideshare.pptx
Vector Search @ sw2con for slideshare.pptx
 

Unit 2 Part 1.2 Data Types.pdf

  • 1. Jagannath Institute of Management Sciences Vasant Kunj-II, New Delhi - 110070 Subject Name: BVITSD 205 : Programming And Problem Solving Though PYTHON Department of Information Technology Created By: Dr. Arpana Chaturvedi @Dr. Arpana Chaturvedi
  • 2. Subject: BVITSD 205 : PROGRAMMING AND PROBLEM SOLVING THOUGH PYTHON Topic: Unit II- Python Variables and Data Types @Dr. Arpana Chaturvedi
  • 3. Unit-II Python Variables and Data Types @Dr. Arpana Chaturvedi An Introduction to Variables Data Types Types Conversion Local and Global Variables Variable Naming Rules Assigning Variables Numbers Strings int() float() Local Variables Global Variables str() bool() list() List Tuple Dictionaries Multiple Variables Deleting Variables
  • 4. Unit-II Python Variables @Dr. Arpana Chaturvedi Python Variables X=45 Type=Integer name=“ JIMS ” Type=String nums=[ 1,3,5,7 ] Type=Lists 45 x “ JIMS ” name [ 1,3,5,7 ] nums ▰ A variable is a container for a value. It can be assigned a name, you can use it to refer to it later in the program. ▰ Based on the value assigned, the interpreter decides its data type. You can always store a different type in a variable.
  • 5. Unit-II Python Variable Naming Convention There are certain rules to what you can name a variable(called an identifier). ▰ Python variables can only begin with a letter(A-Z/a-z) or an underscore(_). ▰ The rest of the identifier may contain letters(A-Z/a-z), underscores(_), and numbers(0-9). ▰ Python is case-sensitive, and so are Python identifiers. Name and name are two different identifiers. ▰ Reserved words (keywords) cannot be used as identifier names. @Dr. Arpana Chaturvedi
  • 6. Unit-II Python Variables Examples @Dr. Arpana Chaturvedi
  • 7. Unit-II 2. Assigning and Reassigning Python Variables ▰ To assign a value to Python variables, you don‟t need to declare its type. ▰ You name it according to the rules of variable naming convention, and type the value after the equal sign(=). @Dr. Arpana Chaturvedi You can‟t put the identifier on the right-hand side of the equal sign, though. The following code causes an error. Neither can you assign Python variables to a keyword.
  • 8. Unit-II 3. Multiple Assignment ▰ You can assign values to multiple Python variables in one statement. ▰ Or you can assign the same value to multiple Python variables. @Dr. Arpana Chaturvedi
  • 9. Unit-II 4. Swapping Python Variables ▰ Swapping means interchanging values. To swap Python variables, you don‟t need to do much. @Dr. Arpana Chaturvedi
  • 10. Unit-II 4.Deleting Python Variables ▰ You can also delete Python variables using the keyword „del‟. @Dr. Arpana Chaturvedi
  • 11. Unit-II Python Datatypes ▰ Although we don‟t have to declare a type for Python variables, a value does have a type. This information is vital to the interpreter. ▰ Python supports the following data types. 1. Python Numbers 2. Strings 3. Python Lists 4. Python Tuples 5. Dictionaries 7. Sets @Dr. Arpana Chaturvedi
  • 12. Unit-II 1. Python Numbers ▰ There are four numeric Python data types. a. int b. float c. long d. complex @Dr. Arpana Chaturvedi
  • 13. Unit-II 1. Python Numbers a. int ▰ int stands for integer. This Python Data Type holds signed integers. We can use the type() function to find which class it belongs to. @Dr. Arpana Chaturvedi An integer can be of any length, with the only limitation being the available memory.
  • 14. Unit-II 1. Python Numbers b. float ▰ This Python Data Type holds floating-point real values. An int can only store the number 3, but float can store 3.25 if you want. @Dr. Arpana Chaturvedi c. long This Python Data type holds a long integer of unlimited length. But this construct does not exist in Python 3.x.
  • 15. Unit-II 1. Python Numbers 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. @Dr. Arpana Chaturvedi Use the isinstance() function to tell if Python variables belong to a particular class. It takes two parameters- the variable/value, and the class.
  • 16. Unit-II 2. Strings ▰ A String is a sequence of characters. Python does not have a char data type, unlike C++ or Java. You can delimit a string using single quotes or double-quotes. @Dr. Arpana Chaturvedi a. Spanning a String Across Lines ▰ To span a string across multiple lines, you can use triple quotes. As you can see, the quotes preserved the formatting (n is the escape sequence for newline, t is for tab).
  • 17. Unit-II 2. Strings @Dr. Arpana Chaturvedi 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. You can also display a burst of characters in a string using the slicing operator []. This prints the characters from 0 to 5.
  • 18. Unit-II 2. Strings @Dr. Arpana Chaturvedi c. String Formatters String formatters allow us to print characters and values at once. You can use the % operator. Or you can use the format method. A third option is to use f-strings.
  • 19. Unit-II 2. Strings @Dr. Arpana Chaturvedi d. String Concatenation You can concatenate(join) strings. However, you cannot concatenate values of different types.
  • 20. Unit-II 3. Python Lists @Dr. Arpana Chaturvedi 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. a. Slicing a List You can slice a list the way you‟d slice a string- with the slicing operator. Indexing for a list begins with 0, like for a string. A Python doesn‟t have arrays.
  • 21. Unit-II 3. Python Lists @Dr. Arpana Chaturvedi c. Length of a List Python supports an inbuilt function to calculate the length of a list. c. Reassigning Elements of a List A list is mutable. This means that you can reassign elements later on.
  • 22. Unit-II 3. Python Lists @Dr. Arpana Chaturvedi d. Iterating on the List To iterate over the list we can use the for loop. By iterating, we can access each element one by one which is very helpful when we need to perform some operations on each element of list.
  • 23. Unit-II 3. Python Lists @Dr. Arpana Chaturvedi e. Multidimensional Lists A list may have more than one dimension.
  • 24. Unit-II Python Tuples ▰ A tuple is like a list. You declare it using parentheses instead. @Dr. Arpana Chaturvedi a. Accessing and Slicing a Tuple You access a tuple the same way as you‟d access a list. The same goes for slicing it.
  • 25. Unit-II Python Tuples @Dr. Arpana Chaturvedi b. A tuple is Immutable Python tuple is immutable. Once declared, you can‟t change its size or elements.
  • 26. Unit-II 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(:). ▰ The type() function works with dictionaries too. @Dr. Arpana Chaturvedi
  • 27. Unit-II 5. Dictionaries ▰ a. Accessing a Value ▰ To access a value, you mention the key in square brackets. ▰ b. Reassigning Elements ▰ You can reassign a value to a key. @Dr. Arpana Chaturvedi
  • 28. Unit-II 5. Dictionaries ▰ c. List of Keys ▰ Use the keys() function to get a list of keys in the dictionary. @Dr. Arpana Chaturvedi
  • 29. Unit-II 6. bool ▰ A Boolean value can be True or False. @Dr. Arpana Chaturvedi
  • 30. Unit-II 7. Sets ▰ 7. Sets ▰ A set can have a list of values. Define it using curly braces. @Dr. Arpana Chaturvedi It returns only one instance of any value present more than once. However, a set is unordered, so it doesn‟t support indexing.
  • 31. Unit-II 7. Sets ▰ 7. Sets ▰ Also, it is mutable. You can change its elements or add more. Use the add() and remove() methods to do so. @Dr. Arpana Chaturvedi
  • 32. Unit-II Python Type Conversion ▰ Since Python is dynamically-typed, you may want to convert a value into another type. Python supports a list of functions for the same. ▰ 1. int() ▰ It converts the value into an int. @Dr. Arpana Chaturvedi Notice how it truncated 0.7 instead of rounding the number off to 4. You can also turn a Boolean into an int.
  • 33. Unit-II Python Type Conversion ▰ However, you cannot turn a string into an int. It throws an error. @Dr. Arpana Chaturvedi  However, if the string has only numbers, then you can.
  • 34. Unit-II Python Type Conversion ▰ 2. float() ▰ It converts the value into a float. @Dr. Arpana Chaturvedi However, this number works even without the float() function. You can also use ‘e’ to denote an exponential number.
  • 35. Unit-II Python Type Conversion ▰ 3. str() ▰ It converts the value into a string. @Dr. Arpana Chaturvedi
  • 36. Unit-II Python Type Conversion ▰ 4. bool() ▰ It converts the value into a Boolean. @Dr. Arpana Chaturvedi
  • 37. Unit-II 5. Python Sets ▰ 5. set() ▰ It converts the value into a set. @Dr. Arpana Chaturvedi
  • 38. Unit-II 6. Python list() ▰ 6. list() ▰ It converts the value into a list. @Dr. Arpana Chaturvedi
  • 39. Unit-II 7. Python tuple ▰ 7. tuple() ▰ It converts the value into a tuple. @Dr. Arpana Chaturvedi You can try your own combinations. Also try composite functions.
  • 40. Unit-II Python Local and Global 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. @Dr. Arpana Chaturvedi Here, the variable num is local to function func1().
  • 41. Unit-II Python Local and Global Variables ▰ 2. Global Variables ▰ When you declare a variable outside any context/scope, it is visible in the whole program. @Dr. Arpana Chaturvedi
  • 42. Unit-II Python Local and Global Variables ▰ 2. Global Variables ▰ You can use the „global‟ keyword when you want to treat a variable as global in a local scope. @Dr. Arpana Chaturvedi
  • 43. Unit-II Python Variables Assignment ▰ What are variables and data types in Python? ▰ What is type () in Python? ▰ What are Local and Global variables in Python? ▰ Explain various naming rules for Python Variables. ▰ How to display part of a string? @Dr. Arpana Chaturvedi