0% found this document useful (0 votes)
1 views

07_BasicDataTypes

Uploaded by

Darshanjr Dachhu
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

07_BasicDataTypes

Uploaded by

Darshanjr Dachhu
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 19

Lecture 07: Basic Data Types

18ESC108A Elements of Computer Science and Engineering


B. Tech. 2018

Course Leader(s):
Prabhakar A.
Roopa G.
Jishmi Jos Choondal
Pujitha V.
Department of Computer Science and Engineering
Faculty of Engineering and Technology
Ramaiah University of Applied Sciences

1
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Objectives

• At the end of this lecture, student will be able to


– explain the basic data types of Python

2
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Topics

• int type
• float type
• complex type
• str type
• bool type

3
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Data Types
• In the real world, we use data all the time without bothering to consider what kind
of data we are using

• For example, consider this sentence:


“In 2007, Micaela paid $120,000 for her house at 24 East Maple Street.”

• This sentence includes at least four pieces of data


– Name
– Date
– Price
– Address

4
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Data Types in Python
• In programming,
– A data type defines a set of values and a set of operations that can be performed on
those values
– A literal is the way a value of a data type looks to a programmer

• The programmer can use a literal in a program to mention a data value


• When Python interpreter evaluates a literal, the value it returns is simply that literal
• Table shows sample literals of several Python data types

5
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Python’s Core Data Types
• The Table previews Python’s built-in types and some of the syntax used to code their
literals

6
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
The int Type
• The integers include 0, the positive whole numbers, and the negative whole
numbers
• The int class is designed to represent integer values
• Object of this type represent integers (both positive and negative)
• Cannot store a fractional part

• The call int(), can be used to construct an object of type integer based on an existing
value of another type
• It is helpful for converting other types like float and string to the type int
 int(3.14) produce the value 3
 int(3.99) produce the value 3
 int(-3.9) produces the value -3
 int(‘137’) produces the value 137
7
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
The int Type - Examples
>>>15 #positive integer
15

>>> -15 #negative integer


-15

>>> -0o15 #negative octal integer; octal constant is represented with leading 0o and
consists of numbers from 0 to 7
-13

>>>0x12abc4 #hexadecimal integer; hexadecimal constant is represented with leading 0X or


0x and consists of digits and alphabets (a-f) in upper case or lower case
1223620

8
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Long Integers

• Usually, in various programming languages such as C, the notion of int type and long
int type exists based on the range of integers that they can represent
– for example, int type may represent -2**31 to 2**31 - 1 and long int may represent
-2**63 to 2**63 -1

• In older versions of Python (namely Python 2.X too), the above distinction is
prevelant

• In Python 3.X, there is only int type irrespective of the number of bits required to
represent it
– For example, whatever may the number of bytes (or words) required to represent an
integer, the necessary number of bytes is allocated by Python
9
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Built in Functions on Type int
• The following mathematical functions are available on type int

Expression Meaning Example Result


pow(x,y) returns (x ** y) pow (2,3) 8
divmod(x,y) returns (x //y, x%y) divmod(10,3) (3,1)
abs(x) absolute value abs(-10) 10
math.factorial(x factorial math.factorial(5) 120
)

10
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Built in Functions on Type int contd.
• Objects of type int can be converted to other bases using the below built in
functions

Expression Result Meaning


bin(12) ‘0b1100’ Converts the integer to binary
oct(12) ‘0o14’ Converts the integer to octal

hex(12) ‘0xc’ Converts the integer to hexadecimal

11
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
The float Type
• The float data type allows to store numbers that have a decimal part
• Float constant
– Any literal constant made up only digits(with an optional leading sign) with either
a decimal point (.) or an exponentiation (e/E)
• Examples
>>>123.5
123.5
>>> 123.0
123.0
>>>12e2
1200.0
>>>123.5e2
12350.0 12
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
The float Type contd.
• The float( ) is helpful for converting other types like int and string to the type float

 float(2) produces the floating point value 2.0


 float(‘3.14’) produces the floating point value 3.14

13
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
The complex Type
• The complex Type allows us to represent complex numbers
• The notation to represent complex numbers with real part x and imaginary part y
x+yj or x+yJ

• Examples
>>> 2+3J
(2+3j)
>>> (2+3j).real
2.0
>>> (2+3j).imag
3.0

14
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
The complex Type contd.

• Objects of type complex can be constructed using the constructor call complex()
• The following statements are identical in function
x= 2+3j
x= complex(2,3)

• complex() is helpful for converting other types like int and string to the type float
>>> complex()
0j
>>> complex(2)
(2+0j)
>>> complex(“2+3j”)
(2+3j) 15
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
The str Type
• The string type (str) allows to store sequence of characters in addition to numeric
values

• The string literals can be enclosed in single, double or triple quotes


• The following statements display the same output Hello
>>>print('Hello')
>>>print("Hello")
>>>print('''Hello''')

• Empty string
>>> ''
''
>>> ""
''
16
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
The bool Type
• The bool class is used to manipulate logical (Boolean) values

• The only two instances of that class are expressed as the literals
1. True
2. False

• Numbers evaluate to False if zero, and True if nonzero


• Sequences and other container types, such as strings and lists, evaluate to False if
empty and True if nonempty

17
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
The bool Type contd.
• The bool() is helpful for converting other types like int and string to the type bool

• Examples
>>>bool(0)
False
>>>bool(10)
True
>>>bool(-12.5)
True
>>>bool(“hello”)
True
>>>bool(“”)
False
>>>bool()
False 18
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Summary
• The basic data types of Python are int, float, complex, str and bool
• The int class is designed to represent integer values
• The float data type allows to store numbers that have a decimal part
• The complex Type allows to represent complex numbers
• The str type allow to represent a sequence of characters
• The bool class is used to manipulate logical (Boolean) values

19
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences

You might also like