SlideShare a Scribd company logo
PYTHON DATA TYPES
MUHAMMAD NAEEM AKHTAR
MS ENERGY SYSTEMS ENGINEERING
DEPARTMENT OF MECHANICAL ENGINEERING
INTERNATIONAL ISLAMIC UNIVERSITY, ISLAMABAD
DATA TYPE
Variables can store data of
different types
We will discuss following
built-in data types
str , int , float , bool , list , dict
STRING DATA TYPE
A string is simply a series of characters.
Anything inside quotes is considered a string in
Python, and you can use single or double quotes
around your strings like the following code:
x = "This is a string."
y = 'This is also a string.'
GET STRING INPUT FROM USER
Input function is used to get input from user.
print("Enter your name: ")
name = input()
# display value of name
print(name)
CHANGING STRING CASE
One of the simplest tasks you can do with strings
is change the case of the words in a string. Look
at the following code, and try to determine what’s
happening:
message = “hello world“
print( message.title() )
Save this file as message.py, and then run it. You
should see this output:
message = “hello world“
print( message.title() )
output:
Hello World
The lowercase string " hello world " is stored in the
variable message. The method title() appears after
the variable in the print() statement. A method is an
action that Python can perform on a piece of data.
The dot . after message in message.title() tells
Python to make the title() method act on the variable
CHANGING STRING CASE
you can change a string to all upper case letters
like this:
message = “hello world“
print( message.upper() )
output:
HELLO WORLD
CHANGING STRING CASE
you can change a string to all lower case letters
like this:
message = “Hello World“
print( message.lower() )
output:
hello world
CHANGING STRING CASE
COMBINING OR
CONCATENATING STRINGS
It’s often useful to combine / cancatenate strings. For
example, you might want to store a first name and a
last name in separate variables, and then combine
them when you want to display someone’s full name:
Python uses the plus symbol (+) to combine strings.
first_name = “Muhammad”
last_name = “Irfan"
full_name = first_name + " " + last_name
print(full_name)
Output:
Muhammad Irfan
COMBINING OR
CONCATENATING STRINGS
It’s often useful to combine / cancatenate strings. For
example, you might want to store a first name and a
last name in separate variables, and then combine
them when you want to display someone’s full name:
Python uses the plus symbol (+) to combine strings.
first_name = “Muhammad”
last_name = “Irfan"
full_name = first_name + " " + last_name
print(full_name)
Output:
Muhammad Irfan
ADDING WHITESPACE TO
STRINGS WITH TABS OR
NEWLINES
In programming, whitespace refers to any nonprinting
character, such as spaces, tabs, and end-of-line
symbols. You can use whitespace to organize your
output so it’s easier for users to read.
To add a tab to your text, use the character
combination t
>>> print("Python")
Python
>>> print("tPython")
ADDING WHITESPACE TO
STRINGS WITH TABS OR
NEWLINES
To add a newline in a string, use the character
combination n
>>> print("Languages:nPythonnCnJava")
Output:
Languages:
Python
C
Java
NUMERIC DATA TYPES
There are two numeric data
types that are used in python
 Integer
 float
INTEGER
Integers are zero, positive or negative whole numbers
without a fractional part
>>> x = 2
>>> y = 3
You can add (+), subtract (-), multiply (*), and divide (/)
integers in Python. Where + , - , * and / are called
arithmetic operators
>>> x + y
5
>>> y - x
1
>>> x * y
6
>>> y / x
FLOAT
Python calls any number with a decimal point
a float.
>>> x = 3.5
>>> y = 1.2
>>> x + y
4.7
>>> x - y
2.3
>>> x * y
4.2
>>> x / y
BOOLEAN
Booleans represent one of two values: True or
False.
you often need to know if an expression is
True or False.
You can evaluate any expression in Python,
and get one of two answers, True or False.
x = 10
y = 7
print("Is x Greater than y: ", x > y)
print("Is x Less than y: ", x < y)
BOOLEAN
x = 10
y = 7
print("Is x Greater than y: ", x > y)
print("Is x Less than y: ", x < y)
print("Is x Equal to y: ", x == y)
Output:
Is x Greater than y: True
Is x Less than y: False
Is x Equal to y: False
> , < , == are called Relational Operators
FINDING TYPE OF VARIABLE
USING TYPE()
You can find the data type of a variable with
the type() function.
x = 3.5
y = 1
name =”Irfan”
type(x)
<class 'float'>
type(y)
<class 'int'>
type(name)
<class 'str'>
FINDING TYPE OF VARIABLE
USING TYPE()
You can find the data type of a variable with
the type() function.
x = 3.5
y = 1
xGreaterthanY = x > y
print(type( xGreaterthanY) )
Output:
<class 'bool'>
DYNAMIC TYPING
In some programming languages such as C++, when declaring a
variable, you need to specify a data type for it like the following
code in which we specify type:
int x = 5;
float y = 3.5;
but in case of python there is no need to specify the data type of
variable , instead it automatically guess it from the value that is
assigned to variable. Same variable is assigned values of
different types.
# x is variable having integer data type.
x = 1
# x is variable having float data type.
x = 3.5
LIST
List is used to store multiple items in a
single variable.
It is a collection of items
students = ["ali", "imran", "irfan", "shoaib"]
marks=[10,9,7,8]
Here students and marks are two lists. First list stores names of
students and second list store marks.
LIST : ACCESSING ELEMENTS
To access an element in a list, write the
name of the list followed by the index of
the item enclosed in square brackets.
Index starts from 0 not from 1. the first
element have index 0.
students = ["ali", "imran", "irfan", "shoaib"]
print( students[0] )
print( students[1] )
print( students[2] )
LIST : MODIFYING ELEMENTS
To change an element, use the name of
the list followed by the index of the
element you want to change, and then
provide the new value you want that
item to have.
students = ["ali", "imran", "irfan", "shoaib"]
students[0] = “haris”
students[1] =“yousaf”
print( students)
LIST : ADDING ELEMENTS TO A LIST
The simplest way to add a new element
to a list is to append the item to the list.
When you append an item to a list, the
new element is added to the end
of the list.
students = ["ali", "imran", "irfan", "shoaib"]
students.append("haris")
print( students)
output:
LIST : INSERTING ELEMENTS INTO A LIST
You can add a new element at any
position in your list by using the insert()
method. You do this by specifying the
index of the new element and the
value of the new item.
students = ["ali", "imran", "irfan", "shoaib"]
students.insert(0,"akhtar")
print(students)
output:
LIST : REMOVING ELEMENTS FROM LIST
You can remove element from your list
by using the remove() method.
students = ["ali", "imran", "irfan", "shoaib"]
students.remove(”imran")
print(students)
output:
['ali', 'irfan', 'shoaib']
LIST : SORTING
The sort() method is used sorts the list
students = [“hamza", “shoaib", “basit", “aadil"]
students.sort()
print(students)
output:
['aadil', 'basit', 'hamza', 'shoaib']
The sort() method sorts the list
THANK YOU

More Related Content

PDF
beginners_python_cheat_sheet_pcc_all_bw.pdf
PDF
23UCACC11 Python Programming (MTNC) (BCA)
PDF
beginners_python_cheat_sheet_pcc_all.pdf
PDF
Beginner's Python Cheat Sheet.pdf
PDF
beginners_python_cheat_sheet_pcc_all.pdf
PDF
python cheat sheat, Data science, Machine learning
PDF
2. Python Cheat Sheet.pdf
PDF
Beginner's Python Cheat Sheet
beginners_python_cheat_sheet_pcc_all_bw.pdf
23UCACC11 Python Programming (MTNC) (BCA)
beginners_python_cheat_sheet_pcc_all.pdf
Beginner's Python Cheat Sheet.pdf
beginners_python_cheat_sheet_pcc_all.pdf
python cheat sheat, Data science, Machine learning
2. Python Cheat Sheet.pdf
Beginner's Python Cheat Sheet

Similar to Python Data Types with realistical approach.pptx (20)

PDF
beginners_python_cheat_sheet_pcc_all (1).pdf
PPTX
Python variables and data types.pptx
PDF
Python Cheatsheet_A Quick Reference Guide for Data Science.pdf
PPTX
beginners_python_cheat_sheet_pcc_all (3).pptx
PPTX
Python programming workshop
PDF
1. python
PDF
Beginners python cheat sheet - Basic knowledge
 
PDF
Python cheatsheet for beginners
PPTX
Data Type In Python.pptx
PPT
Chap 2 Arrays and Structures.ppt
PPTX
Chap 2 Arrays and Structures.pptx
PPTX
Introduction to python for the abs .pptx
PDF
Data Handling_XI_Finall for grade 11 cbse board
PPTX
Learn about Python power point presentation
PPTX
Basics of Python programming (part 2)
PDF
Data Handling_XI- All details for cbse board grade 11
PDF
The python fundamental introduction part 1
PPTX
python programming for beginners and advanced
PDF
beginners_python_cheat_sheet_pcc_functions.pdf
DOCX
CS 360 LAB 3 STRINGS, FUNCTIONS, AND METHODSObjective The purpos.docx
beginners_python_cheat_sheet_pcc_all (1).pdf
Python variables and data types.pptx
Python Cheatsheet_A Quick Reference Guide for Data Science.pdf
beginners_python_cheat_sheet_pcc_all (3).pptx
Python programming workshop
1. python
Beginners python cheat sheet - Basic knowledge
 
Python cheatsheet for beginners
Data Type In Python.pptx
Chap 2 Arrays and Structures.ppt
Chap 2 Arrays and Structures.pptx
Introduction to python for the abs .pptx
Data Handling_XI_Finall for grade 11 cbse board
Learn about Python power point presentation
Basics of Python programming (part 2)
Data Handling_XI- All details for cbse board grade 11
The python fundamental introduction part 1
python programming for beginners and advanced
beginners_python_cheat_sheet_pcc_functions.pdf
CS 360 LAB 3 STRINGS, FUNCTIONS, AND METHODSObjective The purpos.docx
Ad

Recently uploaded (20)

PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PPTX
UNIT 4 Total Quality Management .pptx
PPTX
Artificial Intelligence
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PPTX
Lecture Notes Electrical Wiring System Components
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PDF
Digital Logic Computer Design lecture notes
PPTX
Current and future trends in Computer Vision.pptx
PDF
Unit I ESSENTIAL OF DIGITAL MARKETING.pdf
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PPTX
bas. eng. economics group 4 presentation 1.pptx
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PPTX
Internet of Things (IOT) - A guide to understanding
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
CYBER-CRIMES AND SECURITY A guide to understanding
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
Operating System & Kernel Study Guide-1 - converted.pdf
UNIT 4 Total Quality Management .pptx
Artificial Intelligence
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
Lecture Notes Electrical Wiring System Components
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
Digital Logic Computer Design lecture notes
Current and future trends in Computer Vision.pptx
Unit I ESSENTIAL OF DIGITAL MARKETING.pdf
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
bas. eng. economics group 4 presentation 1.pptx
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
Internet of Things (IOT) - A guide to understanding
Model Code of Practice - Construction Work - 21102022 .pdf
Ad

Python Data Types with realistical approach.pptx

  • 1. PYTHON DATA TYPES MUHAMMAD NAEEM AKHTAR MS ENERGY SYSTEMS ENGINEERING DEPARTMENT OF MECHANICAL ENGINEERING INTERNATIONAL ISLAMIC UNIVERSITY, ISLAMABAD
  • 2. DATA TYPE Variables can store data of different types We will discuss following built-in data types str , int , float , bool , list , dict
  • 3. STRING DATA TYPE A string is simply a series of characters. Anything inside quotes is considered a string in Python, and you can use single or double quotes around your strings like the following code: x = "This is a string." y = 'This is also a string.'
  • 4. GET STRING INPUT FROM USER Input function is used to get input from user. print("Enter your name: ") name = input() # display value of name print(name)
  • 5. CHANGING STRING CASE One of the simplest tasks you can do with strings is change the case of the words in a string. Look at the following code, and try to determine what’s happening: message = “hello world“ print( message.title() ) Save this file as message.py, and then run it. You should see this output:
  • 6. message = “hello world“ print( message.title() ) output: Hello World The lowercase string " hello world " is stored in the variable message. The method title() appears after the variable in the print() statement. A method is an action that Python can perform on a piece of data. The dot . after message in message.title() tells Python to make the title() method act on the variable CHANGING STRING CASE
  • 7. you can change a string to all upper case letters like this: message = “hello world“ print( message.upper() ) output: HELLO WORLD CHANGING STRING CASE
  • 8. you can change a string to all lower case letters like this: message = “Hello World“ print( message.lower() ) output: hello world CHANGING STRING CASE
  • 9. COMBINING OR CONCATENATING STRINGS It’s often useful to combine / cancatenate strings. For example, you might want to store a first name and a last name in separate variables, and then combine them when you want to display someone’s full name: Python uses the plus symbol (+) to combine strings. first_name = “Muhammad” last_name = “Irfan" full_name = first_name + " " + last_name print(full_name) Output: Muhammad Irfan
  • 10. COMBINING OR CONCATENATING STRINGS It’s often useful to combine / cancatenate strings. For example, you might want to store a first name and a last name in separate variables, and then combine them when you want to display someone’s full name: Python uses the plus symbol (+) to combine strings. first_name = “Muhammad” last_name = “Irfan" full_name = first_name + " " + last_name print(full_name) Output: Muhammad Irfan
  • 11. ADDING WHITESPACE TO STRINGS WITH TABS OR NEWLINES In programming, whitespace refers to any nonprinting character, such as spaces, tabs, and end-of-line symbols. You can use whitespace to organize your output so it’s easier for users to read. To add a tab to your text, use the character combination t >>> print("Python") Python >>> print("tPython")
  • 12. ADDING WHITESPACE TO STRINGS WITH TABS OR NEWLINES To add a newline in a string, use the character combination n >>> print("Languages:nPythonnCnJava") Output: Languages: Python C Java
  • 13. NUMERIC DATA TYPES There are two numeric data types that are used in python  Integer  float
  • 14. INTEGER Integers are zero, positive or negative whole numbers without a fractional part >>> x = 2 >>> y = 3 You can add (+), subtract (-), multiply (*), and divide (/) integers in Python. Where + , - , * and / are called arithmetic operators >>> x + y 5 >>> y - x 1 >>> x * y 6 >>> y / x
  • 15. FLOAT Python calls any number with a decimal point a float. >>> x = 3.5 >>> y = 1.2 >>> x + y 4.7 >>> x - y 2.3 >>> x * y 4.2 >>> x / y
  • 16. BOOLEAN Booleans represent one of two values: True or False. you often need to know if an expression is True or False. You can evaluate any expression in Python, and get one of two answers, True or False. x = 10 y = 7 print("Is x Greater than y: ", x > y) print("Is x Less than y: ", x < y)
  • 17. BOOLEAN x = 10 y = 7 print("Is x Greater than y: ", x > y) print("Is x Less than y: ", x < y) print("Is x Equal to y: ", x == y) Output: Is x Greater than y: True Is x Less than y: False Is x Equal to y: False > , < , == are called Relational Operators
  • 18. FINDING TYPE OF VARIABLE USING TYPE() You can find the data type of a variable with the type() function. x = 3.5 y = 1 name =”Irfan” type(x) <class 'float'> type(y) <class 'int'> type(name) <class 'str'>
  • 19. FINDING TYPE OF VARIABLE USING TYPE() You can find the data type of a variable with the type() function. x = 3.5 y = 1 xGreaterthanY = x > y print(type( xGreaterthanY) ) Output: <class 'bool'>
  • 20. DYNAMIC TYPING In some programming languages such as C++, when declaring a variable, you need to specify a data type for it like the following code in which we specify type: int x = 5; float y = 3.5; but in case of python there is no need to specify the data type of variable , instead it automatically guess it from the value that is assigned to variable. Same variable is assigned values of different types. # x is variable having integer data type. x = 1 # x is variable having float data type. x = 3.5
  • 21. LIST List is used to store multiple items in a single variable. It is a collection of items students = ["ali", "imran", "irfan", "shoaib"] marks=[10,9,7,8] Here students and marks are two lists. First list stores names of students and second list store marks.
  • 22. LIST : ACCESSING ELEMENTS To access an element in a list, write the name of the list followed by the index of the item enclosed in square brackets. Index starts from 0 not from 1. the first element have index 0. students = ["ali", "imran", "irfan", "shoaib"] print( students[0] ) print( students[1] ) print( students[2] )
  • 23. LIST : MODIFYING ELEMENTS To change an element, use the name of the list followed by the index of the element you want to change, and then provide the new value you want that item to have. students = ["ali", "imran", "irfan", "shoaib"] students[0] = “haris” students[1] =“yousaf” print( students)
  • 24. LIST : ADDING ELEMENTS TO A LIST The simplest way to add a new element to a list is to append the item to the list. When you append an item to a list, the new element is added to the end of the list. students = ["ali", "imran", "irfan", "shoaib"] students.append("haris") print( students) output:
  • 25. LIST : INSERTING ELEMENTS INTO A LIST You can add a new element at any position in your list by using the insert() method. You do this by specifying the index of the new element and the value of the new item. students = ["ali", "imran", "irfan", "shoaib"] students.insert(0,"akhtar") print(students) output:
  • 26. LIST : REMOVING ELEMENTS FROM LIST You can remove element from your list by using the remove() method. students = ["ali", "imran", "irfan", "shoaib"] students.remove(”imran") print(students) output: ['ali', 'irfan', 'shoaib']
  • 27. LIST : SORTING The sort() method is used sorts the list students = [“hamza", “shoaib", “basit", “aadil"] students.sort() print(students) output: ['aadil', 'basit', 'hamza', 'shoaib'] The sort() method sorts the list