Values and Data types in python

2,743 views 17 slides Aug 15, 2019
Slide 1
Slide 1 of 17
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17

About This Presentation

GE8151 - Problem Solving and Python Programming (Values and Data types in python)


Slide Content

Values and Data types in Python Prepared By, P.Jothi Thilaga , AP/CSE Ramco Institute of Technology Rajapalayam

Value one of the fundamental things, like a number, a character or a string that program manipulates. Example: a = 25, b= “Hi” 25, Hi represents values Data type refers to the type and size of data. Variables can hold values of different data types. Python is a purely object oriented language. It refers to everything as an object, including numbers and strings .

The five standard data types supported by python includes: Number String List Tuple Dictionary

Numbers Number refers to a numeric value. It includes integers long integers floating point complex numbers

Integers are whole numbers with no fractional parts. They can be either positive, negative or zero value . Long integers in python is displayed with an uppercase L (56788333354533L is a long integer). - supported only in python 2 version not in python 3. Floating point numbers are numbers with fractions or decimal points. Floating point values can be expressed in scientific notation using the letter ‘e’ or ‘E ’. C omplex number   is a number that can be expressed in the form a + bi, where a and b are real numbers, and i is the imaginary unit .

Boolean is another data type in Python. A variable of Boolean type can have one of the two values - True or False. The type() function can be used to know which data type a variable or a value belongs to . The isinstance () function to check if an object belongs to a particular class .

Examples : a = 5 print(a, "is of type", type(a)) a = 2.0 print(a, "is of type", type(a)) a = 1+2j print(a, "is complex no.?", isinstance (1+2j,complex)) Output : 5 is of type <class ' int '> 2.0 is of type <class 'float'> (1+2j) is complex no.? True

List List is an ordered sequence of items . It is one of the most used data type in Python and is very flexible. All the items in a list do not need to be of the same type. Lists are mutable. The elements in the list can be modified. To declare a list in python, separate the items using commas and enclose them within square brackets [ ].

>>> a = [1, 2.2, 'python'] The slicing operator [ ] is used to extract an item or a range of items from a list. Index starts from 0 in Python. Examples: >>> a = [5,10,15,20,25,30,35,40] >>> a[2] 15 >>> print("a[0:3] = ", a[0:3]) a[0:3] = [5, 10, 15]

Tuple Tuple is an ordered sequence of items same as list. The only difference is that tuples are immutable. Tuples once created cannot be modified . It is defined within parentheses ( ) where items are separated by commas .

Example: >>> t = (5 , 'program ', 1+3j, 4, 11.5) print("t[1] = ", t[1] ) print("t[0:3] = ", t[0:3] ) Output: t[1 ] = program t[0:3] = (5, 'program', (1+3j ))

Strings A String in python can be a series or a sequence of alphabets, numerals and special characters. Single quotes or double quotes are used to represent strings. >>> s = "This is a string" Strings are immutable.

Examples: s = 'Hello world!' print("s[4] = ", s[4]) print ("s[6:11] = ", s[6:11]) Output: s[4] = o s[6:11] = world

Dictionary A python dictionary is an unordered collection of key-value pairs. Keys and values can be of any type in a dictionary. Items in dictionary are separated by comma and enclosed with the curly-braces { }. Example: >>> dict ={1 :“hi”,2 : “hello”}

Exercises --------- are the reserved memory locations that stores values. What data type is used to store values in terms of key and value? (a ) list (b ) tuple (c ) class (d ) dictionary What data type is the object below ? L = [1.0, 15, ‘ hi’, 27] (a) list (b) dictionary (c) array (d) tuple

Answers Variables (d) dictionary (a) list

Thank You
Tags