10 Lecture Data Types in Python SS
10 Lecture Data Types in Python SS
• 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.
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
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