0% found this document useful (0 votes)
27 views12 pages

Data Types

Uploaded by

shantanu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views12 pages

Data Types

Uploaded by

shantanu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Part-II

Data Handling

Prof. K. Adisesha
Data Types
• Data Type specifies which type of value a variable can store.
type() function is used to determine a variable 's type in
Python.
• Various data types supported by Python programs are:

Data Types In Python


1. Number
2. String
3. Boolean
4. List
5. Tuple
6. Dictionary

2
Data Types
Number In Python
• It is used to store numeric values
• Python has three numeric types:
1. Integers
 Example: a = 10
2. Floating point numbers
 Example: b = -101.4
3. Complex numbers
 Example: b=complex(101,23)
 Output :- (101+23j)

3
Data Types
String In Python
• A string is a sequence of characters. In python we can create string
using single(‘ ')or double quotes (“ "). Both are same in python.
• Example
str='computer science'
print('str-',str) #print string
print('str[0]-',str[0]) #print first char
print('str[1:3]-,str[1:3]) #print string from position 1 to 3

Boolean In Python
It is used to store two possible values either true or false
Example
str="compsc"
b=str.isupper() #test if string contain uppercase
print(b)
Output
False
4
Data Types
List In Python
• List are collections of items and each item has its own index value.
Tuple In Python
• List and tuple, both are same except, a list is mutable python objects
and tuple is immutable Python objects.
• Immutable Python objects mean you can not modify the content so a
tuple once it is assigned.
Example of List:
list=[6,9] Example of tuple
list[0]=55 tup=(66,99)
print(list[0]) Tup[0]=3 # error message will be displayed
print(list[1]) print(tup[0])
OUTPUT print(tup[1])
55, 9

5
Data Types
• Dictionary In Python
• It is an unordered collection of items and each item consist of a key
and a value.
• Example:
dict= {'Subject': 'comp sc', 'class': '11'}
print(dict)
print ("Subject : ", dict['Subject'])
print ("class : ", dict.get('class'))
• Output
{'Subject': 'comp sc', 'class': '11'}
Subject : comp sc
class : 11

6
if Statement
• if statement: Executes a group of statements only if a certain
condition is true. Otherwise, the statements are skipped.
 Syntax:
if condition:
statements

• Example:
gpa = 3.4
if gpa > 2.0:
print "Your application is accepted."

7
if/else
• if/else statement: Executes one block of statements if a certain condition is
True, and a second block of statements if it is False.
 Syntax:
if condition:
statements
else:
statements
• Example:
gpa = 1.4
if gpa > 2.0:
print "Welcome to University!"
else:
print "Your application is denied."

• Multiple conditions can be chained with elif ("else if"):


if condition:
statements
elif condition:
statements
else:
statements

8
While Loop
• while loop: Executes a group of statements as long as a condition is True.
 good for indefinite loops (repeat an unknown number of
times)
• Syntax:
while condition:
statements
• Example:
number = 1
while number < 200:
print number,
number = number * 2
 Output:
1 2 4 8 16 32 64 128

9
The for loop
• for loop: A for loop is used for iterating over a sequence (that is either a list, a
tuple, a dictionary, a set, or a string).
• Repeats a set of statements over a group of values.
 Syntax:
for variableName in groupOfValues:
statements
• We indent the statements to be repeated with tabs or spaces.
• variableName gives a name to each value, so you can refer to it in the statements.
• groupOfValues can be a range of integers, specified with the range function.

 Example:
for x in range(1, 6):
print x, "squared is", x * x
Output:
1 squared is 1
2 squared is 4
3 squared is 9
4 squared is 16
5 squared is 25
10
range
• The range function specifies a range of integers:
• range(start, stop) - the integers between start (inclusive)
and stop (exclusive)
 It can also accept a third value specifying the change between
values.
• range(start, stop, step) - the integers between start (inclusive)
and stop (exclusive) by step
 Example:
for x in range(1, 10, 2):
print x
print "Blastoff!"
Output:
1 3 5 7 9
Blastoff!

11
The break & continue Statement
• With the break statement we can stop the loop before it has looped
through all the items Example: Do not print banana
Example: Exit loop when x is “banana”
fruits = ["apple", "banana", "cherry"] fruits = ["apple", "banana", "cherry"]
for x in fruits: for x in fruits:
print(x) if x == "banana":
if x == "banana": break
break print(x)
Output:
apple Output: apple
banana

• With the continue statement we can stop the current iteration of the
loop, and continue with the next
Example: Do not print banana
fruits = ["apple", "banana", "cherry"]
for x in fruits:
if x == "banana":
continue
print(x)

Output: apple
cherry
12

You might also like