Learn typecasting in Python

Learn typecasting in Python

A crash course on Typecasting and Type conversion in Python in a very non-verbose manner


The process of converting one data type to another data type is called Typecasting or Type Coercion or Type Conversion.

The topics that I’ll be focusing on in this article are:

  1. Implicit Type Conversion
  2. Explicit Type Conversion
  3. Advantages
  4. Disadvantages

Implicit Type Conversion

When the type conversion is performed automatically by the interpreter without the programmer’s intervention, that type of conversion is referred to as implicit type conversion.

Example Program:

myInt = 143     # Integer value.
myFloat = 1.43  # Float value.

myResult = myInt + myFloat # Sum result

print("datatype of myInt:",type(myInt))
print("datatype of myFloat:",type(myFloat))

print("Value of myResult:",myResult)
print("datatype of myResult:",type(myResult))


The output for the above program will be:

datatype of myInt: <class 'int'>
datatype of myFloat: <class 'float'>
Value of myResult: 144.43
datatype of myResult: <class 'float'>

In the above program,

  • We add two variables myInt and myFloat, storing the value in myResult.
  • We will look at the data type of all three objects respectively.
  • In the output, we can see the datatype of myInt is an integer, the datatype of myFloat is a float.
  • Also, we can see the myFloat has float data type because Python converts smaller data type to larger data type to avoid the loss of data.

This type of conversion is called Implicit Type conversion (or) UpCasting.

Explicit Type Conversion

In Explicit Type Conversion, users convert the data type of an object to the required data type. We use predefined in-built functions like:

  1. int()
  2. float()
  3. complex()
  4. bool()
  5. str()

The syntax for explicit type conversion is:


This type of conversion is called Explicit Type conversion (or) DownCasting.

Int Conversion

We can use this function to convert values from other types to int.

For example:

>>> int(123.654)


>>> int("10")

>>> int("10.5")
ValueError: invalid literal for int() with base 10: '10.5'

>>> int("ten")
ValueError: invalid literal for int() with base 10: 'ten'

>>> int("0B1111")
ValueError: invalid literal for int() with base 10: '0B1111'

>>> int(10+3j)
TypeError: can't convert complex to int


  1. You can’t convert complex datatype to int
  2. If you want to convert string type to int type, the string literal must contain the value in Base-10

Float Conversion

This function is used to convert any data type to a floating point number.

For example:

>>> float(10)

>>> float(True)

>>> float(False)

>>> float("10")

>>> float("10.5")

>>> float("ten")
ValueError: could not convert string to float: 'ten'

>>> float(10+5j)
TypeError: can't convert complex to float

>>> float("0B1111")
ValueError: could not convert string to float: '0B1111'


  1. You can convert complex type to float type value.
  2. If you want to convert string type to float type, the string literal must contain the value in base-10.

Complex Conversion

This functionis used to convert real numbers to a complex (real, imaginary) number.

Form 1: complex (x)

You can use this function to convert a single value to a complex number with real part x and imaginary part 0.

For example:

>>> complex(10)

>>> complex(10.5)

>>> complex(True)

>>> complex(False)

>>> complex("10")

>>> complex("10.5")

>>> complex("ten")
ValueError: complex() arg is a malformed string

Form 2: complex (x, y)

If you want to convert X and Y into complex number such that X will be real part and Y will be imaginary part.

For example:

>>> complex(10,-2)

>>> complex(True, False)

Boolean Conversion

This function is used to convert any data type to boolean data type easily. It is the most flexible data type in Python.

For example:

>>> bool(0)

>>> bool(1)

>>> bool(10)

>>> bool(0.13332)

>>> bool(0.0)

>>> bool(10+6j)

>>> bool(0+15j)

>>> bool(0+0j)

>>> bool("Apple")

>>> bool("")

Note: With the help of bool function, you can convert any type of datatype into boolean and the output will be - For all values it will produce True except 0, 0+0j and for an Empty String.

String Conversion

This function is used to convert any type into a string type.

For example:

>>> str(10)

>>> str(10.5)

>>> str(True)

>>> str(False)

>>> str(10+5j)

>>> str(False)

Example Program:

integer_number = 123  # Int
string_number = "456" # String

print("Data type of integer_number:",type(integer_number))
print("Data type of num_str before Type Casting:",type(num_str))

string_number = int(string_number)
print("Data type of string_number after Type Casting:",type(string_number))

number_sum = integer_number + string_number

print("Sum of integer_number and num_str:",number_sum)
print("Data type of the sum:",type(number_sum))


When we run the above program the output will be:

Data type of integer_number: <class 'int'>
Data type of num_str before Type Casting: <class 'str'>
Data type of string_number after Type Casting: <class 'int'>
Sum of integer_number and num_str: 579
Data type of the sum: <class 'int'>

In the above program,

  • We add string_number and integer_number variable.
  • We converted string_number from string(higher) to integer(lower) type using int() function to perform addition.
  • After converting string_number to an integer value Python adds these two variables.
  • We got the number_sum value and data type to be an integer.

Advantages Of Typecasting

  1. More convenient to use

Disadvantages Of Typecasting

  1. More complex type system
  2. Source of bugs due to unexpected casts

I covered pretty much everything that is required to perform any type of typecasting operation in Python3.

Hope this helped you learn about Python Typecasting in a quick and easy way.

If you liked this article please click on the clap and leave me your valuable feedback.

Learn More

Complete Python: Go from zero to hero in Python

An A-Z of useful Python tricks

A Complete Machine Learning Project Walk-Through in Python

A Feature Selection Tool for Machine Learning in Python

Learning Python: From Zero to Hero

MongoDB with Python Crash Course - Tutorial for Beginners

Introduction to PyTorch and Machine Learning

Python Tutorial for Beginners (2019) - Learn Python for Machine Learning and Web Development

Build Your First Open Source Python Project

Complete Python Bootcamp: Go from zero to hero in Python 3

Python and Django Full Stack Web Developer Bootcamp

The Python Bible™ | Everything You Need to Program in Python

Originally published by PALAKOLLU SRI MANIKANTA at https://medium.freecodecamp.org

Python GUI Programming Projects using Tkinter and Python 3

Python GUI Programming Projects using Tkinter and Python 3

Python GUI Programming Projects using Tkinter and Python 3

Learn Hands-On Python Programming By Creating Projects, GUIs and Graphics

Python is a dynamic modern object -oriented programming language
It is easy to learn and can be used to do a lot of things both big and small
Python is what is referred to as a high level language
Python is used in the industry for things like embedded software, web development, desktop applications, and even mobile apps!
SQL-Lite allows your applications to become even more powerful by storing, retrieving, and filtering through large data sets easily
If you want to learn to code, Python GUIs are the best way to start!

I designed this programming course to be easily understood by absolute beginners and young people. We start with basic Python programming concepts. Reinforce the same by developing Project and GUIs.

Why Python?

The Python coding language integrates well with other platforms – and runs on virtually all modern devices. If you’re new to coding, you can easily learn the basics in this fast and powerful coding environment. If you have experience with other computer languages, you’ll find Python simple and straightforward. This OSI-approved open-source language allows free use and distribution – even commercial distribution.

When and how do I start a career as a Python programmer?

In an independent third party survey, it has been revealed that the Python programming language is currently the most popular language for data scientists worldwide. This claim is substantiated by the Institute of Electrical and Electronic Engineers, which tracks programming languages by popularity. According to them, Python is the second most popular programming language this year for development on the web after Java.

Python Job Profiles
Software Engineer
Research Analyst
Data Analyst
Data Scientist
Software Developer
Python Salary

The median total pay for Python jobs in California, United States is $74,410, for a professional with one year of experience
Below are graphs depicting average Python salary by city
The first chart depicts average salary for a Python professional with one year of experience and the second chart depicts the average salaries by years of experience
Who Uses Python?

This course gives you a solid set of skills in one of today’s top programming languages. Today’s biggest companies (and smartest startups) use Python, including Google, Facebook, Instagram, Amazon, IBM, and NASA. Python is increasingly being used for scientific computations and data analysis
Take this course today and learn the skills you need to rub shoulders with today’s tech industry giants. Have fun, create and control intriguing and interactive Python GUIs, and enjoy a bright future! Best of Luck
Who is the target audience?

Anyone who wants to learn to code
For Complete Programming Beginners
For People New to Python
This course was designed for students with little to no programming experience
People interested in building Projects
Anyone looking to start with Python GUI development
Basic knowledge
Access to a computer
Download Python (FREE)
Should have an interest in programming
Interest in learning Python programming
Install Python 3.6 on your computer
What will you learn
Build Python Graphical User Interfaces(GUI) with Tkinter
Be able to use the in-built Python modules for their own projects
Use programming fundamentals to build a calculator
Use advanced Python concepts to code
Build Your GUI in Python programming
Use programming fundamentals to build a Project
Signup Login & Registration Programs
Job Interview Preparation Questions
& Much More

Guide to Python Programming Language

Guide to Python Programming Language

Guide to Python Programming Language

The course will lead you from beginning level to advance in Python Programming Language. You do not need any prior knowledge on Python or any programming language or even programming to join the course and become an expert on the topic.

The course is begin continuously developing by adding lectures regularly.

Please see the Promo and free sample video to get to know more.

Hope you will enjoy it.

Basic knowledge
An Enthusiast Mind
A Computer
Basic Knowledge To Use Computer
Internet Connection
What will you learn
Will Be Expert On Python Programming Language
Build Application On Python Programming Language

Python Programming Tutorials For Beginners

Python Programming Tutorials For Beginners

Python Programming Tutorials For Beginners

Hello and welcome to brand new series of wiredwiki. In this series i will teach you guys all you need to know about python. This series is designed for beginners but that doesn't means that i will not talk about the advanced stuff as well.

As you may all know by now that my approach of teaching is very simple and straightforward.In this series i will be talking about the all the things you need to know to jump start you python programming skills. This series is designed for noobs who are totally new to programming, so if you don't know any thing about

programming than this is the way to go guys Here is the links to all the videos that i will upload in this whole series.

In this video i will talk about all the basic introduction you need to know about python, which python version to choose, how to install python, how to get around with the interface, how to code your first program. Than we will talk about operators, expressions, numbers, strings, boo leans, lists, dictionaries, tuples and than inputs in python. With

Lots of exercises and more fun stuff, let's get started.

Download free Exercise files.

Dropbox: https://bit.ly/2AW7FYF

Who is the target audience?

First time Python programmers
Students and Teachers
IT pros who want to learn to code
Aspiring data scientists who want to add Python to their tool arsenal
Basic knowledge
Students should be comfortable working in the PC or Mac operating system
What will you learn
know basic programming concept and skill
build 6 text-based application using python
be able to learn other programming languages
be able to build sophisticated system using python in the future

To know more: