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

Similar to Python Data Types: Strings, Numbers, Booleans, Lists (38

Python programming workshop
Python programming workshopPython programming workshop
Python programming workshopBAINIDA
 
Beginners python cheat sheet - Basic knowledge
Beginners python cheat sheet - Basic knowledge Beginners python cheat sheet - Basic knowledge
Beginners python cheat sheet - Basic knowledge O T
 
Data Type In Python.pptx
Data Type In Python.pptxData Type In Python.pptx
Data Type In Python.pptxhothyfa
 
Chap 2 Arrays and Structures.ppt
Chap 2  Arrays and Structures.pptChap 2  Arrays and Structures.ppt
Chap 2 Arrays and Structures.pptshashankbhadouria4
 
Chap 2 Arrays and Structures.pptx
Chap 2  Arrays and Structures.pptxChap 2  Arrays and Structures.pptx
Chap 2 Arrays and Structures.pptxshashankbhadouria4
 
Basics of Python programming (part 2)
Basics of Python programming (part 2)Basics of Python programming (part 2)
Basics of Python programming (part 2)Pedro Rodrigues
 
python programming for beginners and advanced
python programming for beginners and advancedpython programming for beginners and advanced
python programming for beginners and advancedgranjith6
 
CS 360 LAB 3 STRINGS, FUNCTIONS, AND METHODSObjective The purpos.docx
CS 360 LAB 3 STRINGS, FUNCTIONS, AND METHODSObjective The purpos.docxCS 360 LAB 3 STRINGS, FUNCTIONS, AND METHODSObjective The purpos.docx
CS 360 LAB 3 STRINGS, FUNCTIONS, AND METHODSObjective The purpos.docxfaithxdunce63732
 
File handling in pythan.pptx
File handling in pythan.pptxFile handling in pythan.pptx
File handling in pythan.pptxNawalKishore38
 
Python Workshop - Learn Python the Hard Way
Python Workshop - Learn Python the Hard WayPython Workshop - Learn Python the Hard Way
Python Workshop - Learn Python the Hard WayUtkarsh Sengar
 
009 Data Handling .pptx
009 Data Handling .pptx009 Data Handling .pptx
009 Data Handling .pptxssuser6c66f3
 
Array assignment
Array assignmentArray assignment
Array assignmentAhmad Kamal
 

Similar to Python Data Types: Strings, Numbers, Booleans, Lists (38 (20)

Python programming workshop
Python programming workshopPython programming workshop
Python programming workshop
 
Beginners python cheat sheet - Basic knowledge
Beginners python cheat sheet - Basic knowledge Beginners python cheat sheet - Basic knowledge
Beginners python cheat sheet - Basic knowledge
 
1. python
1. python1. python
1. python
 
Python cheatsheet for beginners
Python cheatsheet for beginnersPython cheatsheet for beginners
Python cheatsheet for beginners
 
Data Type In Python.pptx
Data Type In Python.pptxData Type In Python.pptx
Data Type In Python.pptx
 
Chap 2 Arrays and Structures.ppt
Chap 2  Arrays and Structures.pptChap 2  Arrays and Structures.ppt
Chap 2 Arrays and Structures.ppt
 
Chap 2 Arrays and Structures.pptx
Chap 2  Arrays and Structures.pptxChap 2  Arrays and Structures.pptx
Chap 2 Arrays and Structures.pptx
 
Basics of Python programming (part 2)
Basics of Python programming (part 2)Basics of Python programming (part 2)
Basics of Python programming (part 2)
 
python programming for beginners and advanced
python programming for beginners and advancedpython programming for beginners and advanced
python programming for beginners and advanced
 
CS 360 LAB 3 STRINGS, FUNCTIONS, AND METHODSObjective The purpos.docx
CS 360 LAB 3 STRINGS, FUNCTIONS, AND METHODSObjective The purpos.docxCS 360 LAB 3 STRINGS, FUNCTIONS, AND METHODSObjective The purpos.docx
CS 360 LAB 3 STRINGS, FUNCTIONS, AND METHODSObjective The purpos.docx
 
File handling in pythan.pptx
File handling in pythan.pptxFile handling in pythan.pptx
File handling in pythan.pptx
 
Python basics
Python basicsPython basics
Python basics
 
Python Workshop - Learn Python the Hard Way
Python Workshop - Learn Python the Hard WayPython Workshop - Learn Python the Hard Way
Python Workshop - Learn Python the Hard Way
 
Dictionary
DictionaryDictionary
Dictionary
 
Python-Cheat-Sheet.pdf
Python-Cheat-Sheet.pdfPython-Cheat-Sheet.pdf
Python-Cheat-Sheet.pdf
 
Python
PythonPython
Python
 
Python - variable types
Python - variable typesPython - variable types
Python - variable types
 
009 Data Handling .pptx
009 Data Handling .pptx009 Data Handling .pptx
009 Data Handling .pptx
 
Arrays in c
Arrays in cArrays in c
Arrays in c
 
Array assignment
Array assignmentArray assignment
Array assignment
 

Recently uploaded

High Profile Call Girls Nashik Megha 7001305949 Independent Escort Service Na...
High Profile Call Girls Nashik Megha 7001305949 Independent Escort Service Na...High Profile Call Girls Nashik Megha 7001305949 Independent Escort Service Na...
High Profile Call Girls Nashik Megha 7001305949 Independent Escort Service Na...Call Girls in Nagpur High Profile
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxAsutosh Ranjan
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxDeepakSakkari2
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineeringmalavadedarshan25
 
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZTE
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Analog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAnalog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAbhinavSharma374939
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxupamatechverse
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile servicerehmti665
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSRajkumarAkumalla
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )Tsuyoshi Horigome
 

Recently uploaded (20)

High Profile Call Girls Nashik Megha 7001305949 Independent Escort Service Na...
High Profile Call Girls Nashik Megha 7001305949 Independent Escort Service Na...High Profile Call Girls Nashik Megha 7001305949 Independent Escort Service Na...
High Profile Call Girls Nashik Megha 7001305949 Independent Escort Service Na...
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptx
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineering
 
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Analog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAnalog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog Converter
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptx
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile service
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )
 

Python Data Types: Strings, Numbers, Booleans, Lists (38

  • 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