This document provides an overview of Python data types including strings, integers, floats, Booleans, lists and dictionaries. It discusses how to define and manipulate variables of each data type in Python. For strings, it describes how to get input from the user, change case, concatenate and add whitespace. For numbers, it covers arithmetic operations on integers and floats. It also discusses determining a variable's type, dynamic typing in Python, accessing, modifying and adding/removing elements from lists.
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
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