0% found this document useful (0 votes)
2 views72 pages

Python Variables and Datatypes

The document provides an overview of Python variables, explaining their role as containers for data and how they are created dynamically without explicit declaration. It outlines the rules for naming variables and discusses the differences between Python and strongly-typed languages, as well as the various data types and structures available in Python. Additionally, it categorizes data types into primitive and non-primitive types, highlighting their significance in programming.

Uploaded by

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

Python Variables and Datatypes

The document provides an overview of Python variables, explaining their role as containers for data and how they are created dynamically without explicit declaration. It outlines the rules for naming variables and discusses the differences between Python and strongly-typed languages, as well as the various data types and structures available in Python. Additionally, it categorizes data types into primitive and non-primitive types, highlighting their significance in programming.

Uploaded by

Passy de great
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

PYTHON

VARIABLES - DATA TYPES / DATA STRUCTURES

IT TRAINING /EDUCATION | PUBLISHING IT SOLUTION DEVELOPMENT & SERVICES Email : enquiries@[Link]


NIGERIA’S No. 1 ONLINE Instructor-Led Training Institute Websites: [Link] | [Link]
IT TRAINING /EDUCATION | PUBLISHING IT SOLUTION DEVELOPMENT & SERVICES Email : enquiries@[Link]
NIGERIA’S No. 1 ONLINE Instructor-Led Training Institute Websites: [Link] | [Link]
IT TRAINING /EDUCATION | PUBLISHING IT SOLUTION DEVELOPMENT & SERVICES Email : enquiries@[Link]
NIGERIA’S No. 1 ONLINE Instructor-Led Training Institute Websites: [Link] | [Link]
Python variables are containers that store
data/ object value and reference to its
location.
A variable can be seen as a container (or
some say a pigeonhole) to store certain
values. While the program is running,
variables are accessed and sometimes
changed, i.e. a new value will be assigned to
the variable.
A Variable in python is created as soon as a
value is assigned to it. It does not need any
additional commands to declare a variable in
python

IT TRAINING /EDUCATION | PUBLISHING IT SOLUTION DEVELOPMENT & SERVICES Email : enquiries@[Link]


NIGERIA’S No. 1 ONLINE Instructor-Led Training Institute Websites: [Link] | [Link]
Creating a variable

IT TRAINING /EDUCATION | PUBLISHING IT SOLUTION DEVELOPMENT & SERVICES Email : enquiries@[Link]


NIGERIA’S No. 1 ONLINE Instructor-Led Training Institute Websites: [Link] | [Link]
As the name implies, a variable is something which
can change. A variable is a way of referring to a
memory location used by a computer program.

A variable is a symbolic name for this physical


location. This memory location contains values, like
numbers, text or more complicated types

IT TRAINING /EDUCATION | PUBLISHING IT SOLUTION DEVELOPMENT & SERVICES Email : enquiries@[Link]


NIGERIA’S No. 1 ONLINE Instructor-Led Training Institute Websites: [Link] | [Link]
Creating a variable
• In Python, a variable is a symbolic name or identifier
that represents a memory location containing data,
such as a numeric value, a string, or any other type
of information. Unlike some other programming
languages.

• Python is a dynamically typed language, meaning


you don't need to explicitly declare the type of a
variable. Instead, the type is inferred based on the
value assigned to it.

IT TRAINING /EDUCATION | PUBLISHING IT SOLUTION DEVELOPMENT & SERVICES Email : enquiries@[Link]


NIGERIA’S No. 1 ONLINE Instructor-Led Training Institute Websites: [Link] | [Link]
Creating a variable • Variables play a crucial role in programming by
allowing developers to store and manipulate data in
their programs.
• When you create a variable, you are essentially
reserving a space in the computer's memory to store
a particular piece of information.

• Technically variable is a reference to a memory


address that contains the value.
• Suppose, we have x=5. There are two ways of reading
this line.

• ‘x’ is a variable name that references to a memory


location in which 5 is stored.
• variable x equals 5

IT TRAINING /EDUCATION | PUBLISHING IT SOLUTION DEVELOPMENT & SERVICES Email : enquiries@[Link]


NIGERIA’S No. 1 ONLINE Instructor-Led Training Institute Websites: [Link] | [Link]
Creating a variable

• Variables play a crucial role in programming by


allowing developers to store and manipulate data
in their programs.
• When you create a variable, you are essentially
reserving a space in the computer's memory to
store a particular piece of information.
• Technically variable is a reference to a memory
address that contains the value.

IT TRAINING /EDUCATION | PUBLISHING IT SOLUTION DEVELOPMENT & SERVICES Email : enquiries@[Link]


NIGERIA’S No. 1 ONLINE Instructor-Led Training Institute Websites: [Link] | [Link]
Creating a variable
• If we use the definition x equals 5, we might think that
it is the same variable x with different values.
• But if we use the actual definition of the variable, we
can argue that there are 3 variables as they refer to
different memory locations and thus are different.

IT TRAINING /EDUCATION | PUBLISHING IT SOLUTION DEVELOPMENT & SERVICES Email : enquiries@[Link]


NIGERIA’S No. 1 ONLINE Instructor-Led Training Institute Websites: [Link] | [Link]
Creating a variable

• We can always check the identity of the location of


the object in memory using the python id()
function.
• To do this we Just write id(variable_name) which
outputs a number that refers to some memory
location(same number means the same variable).
• So, use the shortcut definition but always
remember the actual meaning.
• This means we can define a number of variables
with the same name(say x) in a program without
having a relation between them.

IT TRAINING /EDUCATION | PUBLISHING IT SOLUTION DEVELOPMENT & SERVICES Email : enquiries@[Link]


NIGERIA’S No. 1 ONLINE Instructor-Led Training Institute Websites: [Link] | [Link]
Creating a variable

IT TRAINING /EDUCATION | PUBLISHING IT SOLUTION DEVELOPMENT & SERVICES Email : enquiries@[Link]


NIGERIA’S No. 1 ONLINE Instructor-Led Training Institute Websites: [Link] | [Link]
Creating a variable - More Examples

a= 10
name=”bello”
todo = [“check email”,”call bob”]
greetin = “hello everyone ”
num=[1,2,3,4,5,6,7,8,9,0]
file= open('[Link]','a')

IT TRAINING /EDUCATION | PUBLISHING IT SOLUTION DEVELOPMENT & SERVICES Email : enquiries@[Link]


NIGERIA’S No. 1 ONLINE Instructor-Led Training Institute Websites: [Link] | [Link]
Declare a variable in Python

Declaring a variable means binding it to a data type.


Declaration of variables is not required in Python.
If there is need of a variable, you think of a name and start using it as a variable. A
variable has a name, in most cases , has a type, a scope, and above all a value.
A variable is a python [Link] soon as the value is assigned to it, the variable is
declared.

IT TRAINING /EDUCATION | PUBLISHING IT SOLUTION DEVELOPMENT & SERVICES Email : enquiries@[Link]


NIGERIA’S No. 1 ONLINE Instructor-Led Training Institute Websites: [Link] | [Link]
What are the differences between Python and strongly-typed languages ?
One of the main differences between Python and strongly-typed languages like C,
C++ or Java is the way it deals with types.
In strongly-typed languages every variable must have a unique data type. E.g. if a
variable is of type integer, solely integers can be saved in the variable.
In Java or C, every variable has to be declared before it can be used.

IT TRAINING /EDUCATION | PUBLISHING IT SOLUTION DEVELOPMENT & SERVICES Email : enquiries@[Link]


NIGERIA’S No. 1 ONLINE Instructor-Led Training Institute Websites: [Link] | [Link]
HOW TO NAME VARIABLES IN PYTHON

IT TRAINING /EDUCATION | PUBLISHING IT SOLUTION DEVELOPMENT & SERVICES Email : enquiries@[Link]


NIGERIA’S No. 1 ONLINE Instructor-Led Training Institute Websites: [Link] | [Link]
Rules for Naming Python Variables: Following are the rules for naming the Python variables:

A Python variable can only contain the set of characters i.e. (A-Z) and (a-z) and only one
special symbol i.e. underscore (_) in its name.
The name of a Python variable has to start with a character i.e. A-Z or a-z, or an underscore (_).
For instance, some of the valid variable names will be: val, Val, _val, etc.
So, it is obvious that anything that is not stated in the above rule cannot start our variable
name in Python.
For instance, the variable names in Python cannot start with a number like 2x, 345val, etc. Also,
we know from the first rule that the special symbols except for underscore () are not allowed in
the Python variable names.

IT TRAINING /EDUCATION | PUBLISHING IT SOLUTION DEVELOPMENT & SERVICES Email : enquiries@[Link]


NIGERIA’S No. 1 ONLINE Instructor-Led Training Institute Websites: [Link] | [Link]
IT TRAINING /EDUCATION | PUBLISHING IT SOLUTION DEVELOPMENT & SERVICES Email : enquiries@[Link]
NIGERIA’S No. 1 ONLINE Instructor-Led Training Institute Websites: [Link] | [Link]
Hence, they cannot start the variable names in Python. However, according to the second rule,
underscore () can start the variable names.
Python variables are case-sensitive in nature. This means that the variables x and X are not the
same. Also, the variable names like var and vAr, var and vaR, var and Var, and var and VAR are
not the same.
This means that even if one character’s case is different, the variables are not the same.
Like any other programming language, Python also has a bunch of keywords. These keywords
are reserved for Python and these cannot be used as variable names. So, variable names should
not be any of the Python keywords. For instance, def is a Python keyword, and hence def is not a
valid variable name. However, because Python is case-sensitive, Def can be used as a variable
name in Python.

IT TRAINING /EDUCATION | PUBLISHING IT SOLUTION DEVELOPMENT & SERVICES Email : enquiries@[Link]


NIGERIA’S No. 1 ONLINE Instructor-Led Training Institute Websites: [Link] | [Link]
IT TRAINING /EDUCATION | PUBLISHING IT SOLUTION DEVELOPMENT & SERVICES Email : enquiries@[Link]
NIGERIA’S No. 1 ONLINE Instructor-Led Training Institute Websites: [Link] | [Link]
DATA STRUCTURES / DATA TYPES IN PYTHON ARE OBJECTS THAT ARE USED TO DEFINE VARIABLES .

They can be further described as the charasteric type of a value that is assigned into a variable
In Python there are different data types, such as numbers, sequences, mappings etc. There may be a
situation where, you have the available data of one type but you want to use it in another form. For
example, the user has input a string but you want to use it as a number. Python's type casting
mechanism let you do that

In Python there are different data types, such as numbers, sequences, mappings etc. There may be a
situation where, you have the available data of one type but you want to use it in another form. For
example, the user has input a string but you want to use it as a number. Python's type casting
mechanism let you do that

IT TRAINING /EDUCATION | PUBLISHING IT SOLUTION DEVELOPMENT & SERVICES Email : enquiries@[Link]


NIGERIA’S No. 1 ONLINE Instructor-Led Training Institute Websites: [Link] | [Link]
DATA STRUCTURES / DATA TYPES IN PYTHON ARE OBJECTS THAT ARE USED TO DEFINE VARIABLES .

In Python there are different data types, such as numbers, sequences, mappings etc. There may be a
situation where, you have the available data of one type but you want to use it in another form. For
example, the user has input a string but you want to use it as a number. Python's type casting
mechanism let you do that

IT TRAINING /EDUCATION | PUBLISHING IT SOLUTION DEVELOPMENT & SERVICES Email : enquiries@[Link]


NIGERIA’S No. 1 ONLINE Instructor-Led Training Institute Websites: [Link] | [Link]
There are mainly six data types in python including data type range which is often used
while working with loops in python. Further more we can classify them into two
categories
• primitive (or basic) data structures such as floats, integers,
strings, and Booleans.
• non-primitive data structures such as lists, tuples,
dictionaries, and sets. Non-primitive data structures store a
collection of values in various formats rather than a single value.
Some can hold data structures within the data structure, creating
depth and complexity in data storage capabilities.

IT TRAINING /EDUCATION | PUBLISHING IT SOLUTION DEVELOPMENT & SERVICES Email : enquiries@[Link]


NIGERIA’S No. 1 ONLINE Instructor-Led Training Institute Websites: [Link] | [Link]
IT TRAINING /EDUCATION | PUBLISHING IT SOLUTION DEVELOPMENT & SERVICES Email : enquiries@[Link]
NIGERIA’S No. 1 ONLINE Instructor-Led Training Institute Websites: [Link] | [Link]
There are mainly six data types in python including data type range which is often used
while working with loops in python. Further more we can classify them into two
categories
• primitive (or basic) data structures
• Python, like other programming languages, provides fundamental
building blocks for storing and manipulating data, often referred
to as primitive data types. These types represent single, basic
values and are the foundation for more complex data
structures,such as floats, integers, strings, and Booleans

IT TRAINING /EDUCATION | PUBLISHING IT SOLUTION DEVELOPMENT & SERVICES Email : enquiries@[Link]


NIGERIA’S No. 1 ONLINE Instructor-Led Training Institute Websites: [Link] | [Link]
There are mainly six data types in python including data type range which is often used
while working with loops in python. Further more we can classify them into two
categories
• primitive (or basic) data structures
• Primitive data types in Python are the most basic types of data.
They are the building blocks for data manipulation in Python and
are typically immutable, meaning their value cannot change after
they are created.

IT TRAINING /EDUCATION | PUBLISHING IT SOLUTION DEVELOPMENT & SERVICES Email : enquiries@[Link]


NIGERIA’S No. 1 ONLINE Instructor-Led Training Institute Websites: [Link] | [Link]
Integers - We'll start with integers, which are whole numbers that can be positive, negative, or
zero.
Floats - Next, we'll dive into floats, which represent real numbers and include those with
decimal points.
Booleans - We'll also cover Booleans, a simple yet powerful type representing true or false
values. Booleans are fundamental in making decisions in your code, allowing you to execute
different actions based on certain conditions.
Strings - Finally, we'll explore strings, which are sequences of characters used to represent text
data. Strings are incredibly versatile and can be used for anything from displaying messages to
users, to processing text data in your applications. (As we've seen in the previous part of
printing text/string to the terminal)

IT TRAINING /EDUCATION | PUBLISHING IT SOLUTION DEVELOPMENT & SERVICES Email : enquiries@[Link]


NIGERIA’S No. 1 ONLINE Instructor-Led Training Institute Websites: [Link] | [Link]
IT TRAINING /EDUCATION | PUBLISHING IT SOLUTION DEVELOPMENT & SERVICES Email : enquiries@[Link]
NIGERIA’S No. 1 ONLINE Instructor-Led Training Institute Websites: [Link] | [Link]
primitive (or basic) data structures
• Python, like other programming languages, provides
fundamental building blocks for storing and manipulating
[Link] as floats, integers, strings, and Booleans
Integers (int):
Used to represent whole numbers (positive, negative, or zero) without a
fractional part. Python integers have arbitrary precision, meaning they
can represent numbers of any size, limited only by available memory.
You can use an integer to represent numeric data and, more specifically,
whole numbers from negative infinity to infinity, like 4, 5, or -1.

IT TRAINING /EDUCATION | PUBLISHING IT SOLUTION DEVELOPMENT & SERVICES Email : enquiries@[Link]


NIGERIA’S No. 1 ONLINE Instructor-Led Training Institute Websites: [Link] | [Link]
primitive (or basic) data structures
• Python, like other programming languages, provides
fundamental building blocks for storing and manipulating
[Link] as floats, integers, strings, and Booleans
Floats (float):
"Float" stands for 'floating point number'. You can use it for rational
numbers, usually ending with a decimal figure, such as 1.11 or
[Link] to represent real numbers with a decimal point or in
exponential form. They have a finite precision, which can lead to minor
rounding errors in certain calculations.

IT TRAINING /EDUCATION | PUBLISHING IT SOLUTION DEVELOPMENT & SERVICES Email : enquiries@[Link]


NIGERIA’S No. 1 ONLINE Instructor-Led Training Institute Websites: [Link] | [Link]
primitive (or basic) data structures
• Python, like other programming languages, provides
fundamental building blocks for storing and manipulating
[Link] as floats, integers, strings, and Booleans
Booleans (bool):
Represent logical values, with only two possible states: True or
False. These are essential for conditional statements and logical
operations.

IT TRAINING /EDUCATION | PUBLISHING IT SOLUTION DEVELOPMENT & SERVICES Email : enquiries@[Link]


NIGERIA’S No. 1 ONLINE Instructor-Led Training Institute Websites: [Link] | [Link]
primitive (or basic) data structures
• Python, like other programming languages, provides
fundamental building blocks for storing and manipulating
[Link] as floats, integers, strings, and Booleans
Strings (str):
Used to represent sequences of characters, enclosed within single
quotes ('...'), double quotes ("..."), or triple quotes ("""...""" or '''...''')
for multi-line strings.
Strings are collections of alphabets, words, or other characters. In
Python, you can create strings by enclosing a sequence of
characters within a pair of single or double quotes. For example:
'cake', "cookie", etc.

IT TRAINING /EDUCATION | PUBLISHING IT SOLUTION DEVELOPMENT & SERVICES Email : enquiries@[Link]


NIGERIA’S No. 1 ONLINE Instructor-Led Training Institute Websites: [Link] | [Link]
primitive (or basic) data structures
• Python, like other programming languages, provides
fundamental building blocks for storing and manipulating
[Link] as floats, integers, strings, and Booleans
NoneType (None):
A special data type that represents the absence of a value. It is
often used as a placeholder or to indicate that a variable has not
yet been assigned a meaningful value.

IT TRAINING /EDUCATION | PUBLISHING IT SOLUTION DEVELOPMENT & SERVICES Email : enquiries@[Link]


NIGERIA’S No. 1 ONLINE Instructor-Led Training Institute Websites: [Link] | [Link]
These primitive data types are immutable, meaning their values cannot be changed
after creation. Any operation that appears to modify a primitive data type actually
creates a new object with the updated value.
These are the most primitive or basic data structures. They are the building blocks for
data manipulation and contain pure, simple values of data.

IT TRAINING /EDUCATION | PUBLISHING IT SOLUTION DEVELOPMENT & SERVICES Email : enquiries@[Link]


NIGERIA’S No. 1 ONLINE Instructor-Led Training Institute Websites: [Link] | [Link]
IT TRAINING /EDUCATION | PUBLISHING IT SOLUTION DEVELOPMENT & SERVICES Email : enquiries@[Link]
NIGERIA’S No. 1 ONLINE Instructor-Led Training Institute Websites: [Link] | [Link]
IT TRAINING /EDUCATION | PUBLISHING IT SOLUTION DEVELOPMENT & SERVICES Email : enquiries@[Link]
NIGERIA’S No. 1 ONLINE Instructor-Led Training Institute Websites: [Link] | [Link]
IT TRAINING /EDUCATION | PUBLISHING IT SOLUTION DEVELOPMENT & SERVICES Email : enquiries@[Link]
NIGERIA’S No. 1 ONLINE Instructor-Led Training Institute Websites: [Link] | [Link]
WE CAN ALSO CLASSIFY THE DATA TYPES BASED ON
THEIR MUTABILITY

IT TRAINING /EDUCATION | PUBLISHING IT SOLUTION DEVELOPMENT & SERVICES Email : enquiries@[Link]


NIGERIA’S No. 1 ONLINE Instructor-Led Training Institute Websites: [Link] | [Link]
IT TRAINING /EDUCATION | PUBLISHING IT SOLUTION DEVELOPMENT & SERVICES Email : enquiries@[Link]
NIGERIA’S No. 1 ONLINE Instructor-Led Training Institute Websites: [Link] | [Link]
WHAT IS A MUTABLE OR IMMUTABLE OBJECT?

IT TRAINING /EDUCATION | PUBLISHING IT SOLUTION DEVELOPMENT & SERVICES Email : enquiries@[Link]


NIGERIA’S No. 1 ONLINE Instructor-Led Training Institute Websites: [Link] | [Link]
a mutable object can be changed after it is created, and an immutable object
can't.

Objects of built-in types like (int, float, bool, str, tuple, unicode) are
immutable.

Objects of built-in types like (list, set, dict) are mutable.

IT TRAINING /EDUCATION | PUBLISHING IT SOLUTION DEVELOPMENT & SERVICES Email : enquiries@[Link]


NIGERIA’S No. 1 ONLINE Instructor-Led Training Institute Websites: [Link] | [Link]
Python Data Types in Details

IT TRAINING /EDUCATION | PUBLISHING IT SOLUTION DEVELOPMENT & SERVICES Email : enquiries@[Link]


NIGERIA’S No. 1 ONLINE Instructor-Led Training Institute Websites: [Link] | [Link]
Numerical Data Types

More details

IT TRAINING /EDUCATION | PUBLISHING IT SOLUTION DEVELOPMENT & SERVICES Email : enquiries@[Link]


NIGERIA’S No. 1 ONLINE Instructor-Led Training Institute Websites: [Link] | [Link]
Numerical data type holds numerical value. In numerical data
there are 4 sub types as well.

Following are the sub-types of numerical data type:


Integers ,Float ,Complex Numbers ,Boolean

IT TRAINING /EDUCATION | PUBLISHING IT SOLUTION DEVELOPMENT & SERVICES Email : enquiries@[Link]


NIGERIA’S No. 1 ONLINE Instructor-Led Training Institute Websites: [Link] | [Link]
INTEGERS

Integers
are used to represent whole number values.
x = 100
y = 124
# it will be the integer as long as the value is a whole
number

IT TRAINING /EDUCATION | PUBLISHING IT SOLUTION DEVELOPMENT & SERVICES Email : enquiries@[Link]


NIGERIA’S No. 1 ONLINE Instructor-Led Training Institute Websites: [Link] | [Link]
The int() constructor

IT TRAINING /EDUCATION | PUBLISHING IT SOLUTION DEVELOPMENT & SERVICES Email : enquiries@[Link]


NIGERIA’S No. 1 ONLINE Instructor-Led Training Institute Websites: [Link] | [Link]
FLOATS

Float Number
data type is used to represent decimal point values.
x = 10.25
y = 12.30

IT TRAINING /EDUCATION | PUBLISHING IT SOLUTION DEVELOPMENT & SERVICES Email : enquiries@[Link]


NIGERIA’S No. 1 ONLINE Instructor-Led Training Institute Websites: [Link] | [Link]
The float() constructor

IT TRAINING /EDUCATION | PUBLISHING IT SOLUTION DEVELOPMENT & SERVICES Email : enquiries@[Link]


NIGERIA’S No. 1 ONLINE Instructor-Led Training Institute Websites: [Link] | [Link]
COMPLEX NUMBERS

Complex numbers
are used to represent imaginary values. Imaginary values are
denoted with ‘j’ at the end of the number.
x = 10 + 5j

IT TRAINING /EDUCATION | PUBLISHING IT SOLUTION DEVELOPMENT & SERVICES Email : enquiries@[Link]


NIGERIA’S No. 1 ONLINE Instructor-Led Training Institute Websites: [Link] | [Link]
TYPE CASTING

IT TRAINING /EDUCATION | PUBLISHING IT SOLUTION DEVELOPMENT & SERVICES Email : enquiries@[Link]


NIGERIA’S No. 1 ONLINE Instructor-Led Training Institute Websites: [Link] | [Link]
TYPE CASTING
implicit and explicit data conversion

Type Casting is the method to convert the Python variable datatype into a
certain data type in order to perform the required operation by users.
In Python, data type conversion refers to the process of transforming one data
type to another, which is useful in everyday programming.

IT TRAINING /EDUCATION | PUBLISHING IT SOLUTION DEVELOPMENT & SERVICES Email : enquiries@[Link]


NIGERIA’S No. 1 ONLINE Instructor-Led Training Institute Websites: [Link] | [Link]
TYPE CASTING
implicit and explicit data conversion

IT TRAINING /EDUCATION | PUBLISHING IT SOLUTION DEVELOPMENT & SERVICES Email : enquiries@[Link]


NIGERIA’S No. 1 ONLINE Instructor-Led Training Institute Websites: [Link] | [Link]
TYPE CASTING
implicit and explicit data conversion

Imagine you work for a financial institution that needs to


perform calculations on user input data. Your task is to create a
program that takes user input for a loan amount and interest
rate, calculates the total interest payable on the loan, and
displays it to the user.
The user inputs the loan amount and interest rate as strings, so
you must convert them to numeric data types to perform the
calculations. This is where implicit and explicit type conversion
comes in handy.

IT TRAINING /EDUCATION | PUBLISHING IT SOLUTION DEVELOPMENT & SERVICES Email : enquiries@[Link]


NIGERIA’S No. 1 ONLINE Instructor-Led Training Institute Websites: [Link] | [Link]
TYPE CASTING
implicit and explicit data conversion

Every value in Python has a data type. Data types are a classification of data that
tells the compiler or the interpreter how you want to use the data. The type
defines the operations that can be done on the data and the structure in which
you want the data to be stored. In data science, you will often need to change the
type of your data to make it easier to use and work with.

IT TRAINING /EDUCATION | PUBLISHING IT SOLUTION DEVELOPMENT & SERVICES Email : enquiries@[Link]


NIGERIA’S No. 1 ONLINE Instructor-Led Training Institute Websites: [Link] | [Link]
TYPE CASTING
implicit and explicit data conversion

Python has many data types. You must have already seen and worked with
some of them. You have integers and float to deal with numerical values,
boolean (bool) to deal with true/false values and strings to work with
alphanumeric characters. You can use lists, tuples, dictionaries, and sets, which
are data structures where you can store a collection of values.

IT TRAINING /EDUCATION | PUBLISHING IT SOLUTION DEVELOPMENT & SERVICES Email : enquiries@[Link]


NIGERIA’S No. 1 ONLINE Instructor-Led Training Institute Websites: [Link] | [Link]
TYPE CASTING
Checking Data Types in Python

Before converting data types, it’s useful to check them. Python


provides two main functions:

type():
Returns the type of a variable.
isinstance():
Checks if a variable belongs to a certain type.

IT TRAINING /EDUCATION | PUBLISHING IT SOLUTION DEVELOPMENT & SERVICES Email : enquiries@[Link]


NIGERIA’S No. 1 ONLINE Instructor-Led Training Institute Websites: [Link] | [Link]
TYPE CASTING
Checking Data Types in Python

a = 65.5
print(type(a)) # Output: <class 'float'>

print(isinstance(a, int)) # Output: False


print(isinstance(a, float)) # Output: True

IT TRAINING /EDUCATION | PUBLISHING IT SOLUTION DEVELOPMENT & SERVICES Email : enquiries@[Link]


NIGERIA’S No. 1 ONLINE Instructor-Led Training Institute Websites: [Link] | [Link]
TYPE CASTING
implicit and explicit data conversion

IT TRAINING /EDUCATION | PUBLISHING IT SOLUTION DEVELOPMENT & SERVICES Email : enquiries@[Link]


NIGERIA’S No. 1 ONLINE Instructor-Led Training Institute Websites: [Link] | [Link]
TYPE CASTING
implicit and explicit data conversion

There can be two types of Type Casting in


Python:
Python Implicit Type Conversion(automatic type
conversion)

Python Explicit Type Conversion(manual type


conversion)

IT TRAINING /EDUCATION | PUBLISHING IT SOLUTION DEVELOPMENT & SERVICES Email : enquiries@[Link]


NIGERIA’S No. 1 ONLINE Instructor-Led Training Institute Websites: [Link] | [Link]
TYPE CASTING
implicit type data conversion

Python Implicit Type Conversion(automatic type conversion)


when the data type conversion takes place during compilation or during the runtime, it’s called
an implicit data type conversion.
Python handles the implicit data type conversion, so we don’t have to explicitly convert the data
type into another data type.

IT TRAINING /EDUCATION | PUBLISHING IT SOLUTION DEVELOPMENT & SERVICES Email : enquiries@[Link]


NIGERIA’S No. 1 ONLINE Instructor-Led Training Institute Websites: [Link] | [Link]
TYPE CASTING
implicit data conversion

a=5
b = 5.5
sum = a + b
print (sum)
print (type (sum)) #type() is used to display the datatype of a variable

IT TRAINING /EDUCATION | PUBLISHING IT SOLUTION DEVELOPMENT & SERVICES Email : enquiries@[Link]


NIGERIA’S No. 1 ONLINE Instructor-Led Training Institute Websites: [Link] | [Link]
TYPE CASTING
implicit data conversion

In the above example, we have taken two variables of integer, and float data types and added
them.
Further, we have declared another variable named ‘sum’ and stored the result of the addition in
it. When we checked the data type of the sum variable, we could see that the data type of the
sum variable had been automatically converted into the float data type by the Python compiler.
This is called implicit type conversion

IT TRAINING /EDUCATION | PUBLISHING IT SOLUTION DEVELOPMENT & SERVICES Email : enquiries@[Link]


NIGERIA’S No. 1 ONLINE Instructor-Led Training Institute Websites: [Link] | [Link]
TYPE CASTING
implicit data conversion

The reason that the sum variable was converted into the float data
type and not the integer data type is that if the compiler had
converted it into the integer data type, then it would’ve had to
remove the fractional part, which would have resulted in data loss.
So, Python always converts smaller data types into larger data
types to prevent the loss of data.

IT TRAINING /EDUCATION | PUBLISHING IT SOLUTION DEVELOPMENT & SERVICES Email : enquiries@[Link]


NIGERIA’S No. 1 ONLINE Instructor-Led Training Institute Websites: [Link] | [Link]
TYPE CASTING
Explicit type data conversion

Python explicit Type Conversion(manual type conversion)


Explicit type conversion is also known as typecasting. Explicit type
conversion takes place when the programmer clearly and explicitly defines
the same in the program. For explicit type conversion, there are some in-built
Python functions.

IT TRAINING /EDUCATION | PUBLISHING IT SOLUTION DEVELOPMENT & SERVICES Email : enquiries@[Link]


NIGERIA’S No. 1 ONLINE Instructor-Led Training Institute Websites: [Link] | [Link]
BOOLEAN

Boolean
theses are used for categorical output, since the output of
boolean is either true or false.
num = 5 > 4
#num is the boolean variable

IT TRAINING /EDUCATION | PUBLISHING IT SOLUTION DEVELOPMENT & SERVICES Email : enquiries@[Link]


NIGERIA’S No. 1 ONLINE Instructor-Led Training Institute Websites: [Link] | [Link]
The bool()

IT TRAINING /EDUCATION | PUBLISHING IT SOLUTION DEVELOPMENT & SERVICES Email : enquiries@[Link]


NIGERIA’S No. 1 ONLINE Instructor-Led Training Institute Websites: [Link] | [Link]
• The bool() function allows you to evaluate any value, and
give you True or False in return,It's not mandatory to pass a
value to bool(). If you do not pass a value, bool()
returns,false.
• False if the value is omitted or false
• True if the value is true

IT TRAINING /EDUCATION | PUBLISHING IT SOLUTION DEVELOPMENT & SERVICES Email : enquiries@[Link]


NIGERIA’S No. 1 ONLINE Instructor-Led Training Institute Websites: [Link] | [Link]
• Return Value from bool()
• The bool() returns:
• False if the value is omitted or false
• True if the value is true

IT TRAINING /EDUCATION | PUBLISHING IT SOLUTION DEVELOPMENT & SERVICES Email : enquiries@[Link]


NIGERIA’S No. 1 ONLINE Instructor-Led Training Institute Websites: [Link] | [Link]
•The following values are
considered false in Python:

IT TRAINING /EDUCATION | PUBLISHING IT SOLUTION DEVELOPMENT & SERVICES Email : enquiries@[Link]


NIGERIA’S No. 1 ONLINE Instructor-Led Training Institute Websites: [Link] | [Link]
• None
• False
• Zero of any numeric type. For example, 0, 0.0, 0j
• Empty sequence. For example, (), [], ''.
• Empty mapping. For example, {}
• objects of Classes which has __bool__() or __len()__ method
which returns 0 or False
• All other values except these values are considered true.

IT TRAINING /EDUCATION | PUBLISHING IT SOLUTION DEVELOPMENT & SERVICES Email : enquiries@[Link]


NIGERIA’S No. 1 ONLINE Instructor-Led Training Institute Websites: [Link] | [Link]
Next we will look at String data types

IT TRAINING /EDUCATION | PUBLISHING IT SOLUTION DEVELOPMENT & SERVICES Email : enquiries@[Link]


NIGERIA’S No. 1 ONLINE Instructor-Led Training Institute Websites: [Link] | [Link]
IT TRAINING /EDUCATION | PUBLISHING IT SOLUTION DEVELOPMENT & SERVICES Email : enquiries@[Link]
NIGERIA’S No. 1 ONLINE Instructor-Led Training Institute Websites: [Link] | [Link]

You might also like