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

10 Lecture Data Types in Python SS

Uploaded by

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

10 Lecture Data Types in Python SS

Uploaded by

ajm8982
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

PYTHON Programming (CST310)

Lecture 10: Standard Data-types supported by Python

Department of Computer Science and Engineering


National Institute of Technology, Srinagar, Jammu and Kashmir
September 10, 2024
Data Types Supported by Python

• Variables can hold some value, and every value has a data-type.
• Python is dynamically typed language (We don’t need to define the
data-type for the variable, Interpreter implicitly gives the datatype to
the variable based on the value).
Age=18
Python interpreter will automatically interpret variables age as an
integer type.
Standard built-in Data-types in Python
Following are the standard datatypes in Python language:
1. Numeric
a. int:
b. Float:
c. Complex:
2. Boolean
a) bool (values are true or false)
3. Sequence Type:
a. String (Immutable): Collection/Sequence of Characters
b. List (Mutable): Collection of elements (same or different)
c. Tuple (Immutable) (Collection of elements
4. Set: (unordered collection of unique elements, Cannot be accessed via indexes)
5. Dictionary (collection of elements stored in key-value pairs)
Standard built-in Data-types in Python
Following are the standard datatypes in Python language:
1. Numeric
a. int: (Integer) for storing the integer values.
It contains positive or negative whole numbers (without fraction or decimal).
Standard built-in Data-types in Python
int: (Integer) for storing the integer values.
a. Arithmetic Operations: You can perform various arithmetic operations on integers,
including addition, subtraction, multiplication, division, floor division (//), and
modulo (%).

b. Unlimited Precision: Python's int type supports unlimited precision arithmetic. This
means that integer values can be as large as the available memory allows.
In Python there is no limit to how long an integer value can be.
Standard built-in Data-types in Python
In Python there is no limit to how long an integer value can be.
Standard built-in Data-types in Python
int: (Integer) for storing the integer values.
• Immutable: Integers are immutable, which means their values cannot be changed
after they are created. When you perform operations on integers, new integer objects
are created.
• Binary, Octal, and Hexadecimal Literals: Integers can be written in various number
bases, such as binary (base 2), octal (base 8), and hexadecimal (base 16), using
prefixes like 0b, 0o, and 0x, respectively.
Standard built-in Data-types in Python
• Type Conversion: Integers can be converted to other data types using built-in
functions like str(), float(), and bool().
• Memory Efficiency: In Python 3, integers are dynamically allocated and managed by
the interpreter, which allows for efficient memory usage.
Finding Size of int object in python: sys.getsizeof() function allows you to find the
memory size of an object in bytes.

• Math Module: Python's built-in math module provides various mathematical


functions that work with integers, such as finding absolute value, factorial, and more.
Standard built-in Data-types in Python
Numeric
b. float:
For storing the decimal numeric values
The float data type is used to represent floating-point numbers, which are numbers
with decimal points or fractional parts
Standard built-in Data-types in Python
float:
• Floating-Point Arithmetic:
• Type Conversion: Floats can be converted to other data types using built-in functions like int()
and str().
• Math Module: Python's built-in math module provides various mathematical functions that
work with floating-point numbers, such as trigonometric functions, logarithms.
• Finite Precision: Floating-point numbers in Python have Finite precision due to the way they
are stored in memory. Floating-point numbers are stored in computers using their binary
representation, specifically the IEEE 754 standard for floating-point arithmetic.
• This means that some calculations might result in small rounding errors.
a=1.1
b=2.2
c=a+b #Output? 3.3?
Floating-point numbers in computers are stored using a finite number of binary digits, which
can lead to small rounding errors.
When you assign a floating-point number like 1.1 to a variable in Python, the value is stored in
its binary representation using the chosen format (typically double precision). The binary
representation is used for calculations and storage internally, and when you access the value,
it's converted back to its decimal representation for display
Standard built-in Data-types in Python

b. Complex:
For storing the complex numbers
Complex number is represented by complex class.
It is specified as (real part) + (imaginary part)j.
For example: 2+3j
Standard built-in Data-types in Python
Following are the standard datatypes in Python language:
2. Boolean
Boolean datatypes take one of the two values: True or False.
Standard built-in Data-types in Python
3. Sequence data-types
In Python, sequence is the ordered collection of values of similar or different
data types.
Sequences allows to store multiple values in an organized and efficient
fashion.
a. String:
A string is a collection of one or more characters put in a single quote, double-quote or
triple quote.
In python, there is no character data type, a character is a string of length one. It is
represented by str class.
Standard built-in Data-types in Python
How to store the following strings in a variable

My name is ‘Prince' and I love Programming.

My name is 'Vivek' and I love “Programming”.

My name is 'Vivek' and I love ''' "Programming"


Standard built-in Data-types in Python
Accessing the elements of a String:

city=“NIT Srinagar”

print(city[0])
print(city[30])
Standard built-in Data-types in Python
3. Sequence data-types
b. List:
Lists are just like the arrays, declared in other languages which is a ordered collection of
data.
It is very flexible as the items in a list do not need to be of the same type.
Standard built-in Data-types in Python
4. SET
Set is also the collection of values but has no duplicate elements

S= { 1, 2, 3}
S2={ 1, 2.3, ‘cse’ }

S3= { 1, 1, 2 }
print(S3) //What will be the output

You might also like