Skip to content

Python Data Types

Python has several built-in types.

If you create the name variable assigning it the value “Roger”, automatically this variable is now representing a String data type.

name = "Roger"

You can check which type a variable is using the type() function, passing the variable as an argument, and then comparing the result to str:

name = "Roger"
type(name) == str #True

Or using isinstance():

name = "Roger"
isinstance(name, str) #True

Notice that to see the True value in Python, outside of a REPL, you need to wrap this code inside print(), but for clarity reasons I avoid using it

We used the str class here, but the same works for other data types.

First, we have numbers. Integer numbers are represented using the int class. Floating point numbers (fractions) are of type float:

age = 1
type(age) == int #True
fraction = 0.1
type(fraction) == float #True

You saw how to create a type from a value literal, like this:

name = "Flavio"
age = 20

Python automatically detects the type from the value type.

You can also create a variable of a specific type by using the class constructor, passing a value literal or a variable name:

name = str("Flavio")
anotherName = str(name)

You can also convert from one type to another by using the class constructor. Python will try to determine the correct value, for example extracting a number from a string:

age = int("20")
print(age) #20

fraction = 0.1
intFraction = int(fraction)
print(intFraction) #0

This is called casting. Of course this conversion might not always work depending on the value passed. If you write test instead of 20 in the above string, you’ll get a ValueError: invalid literal for int() with base 10: 'test' error.

Those are just the basics of types. We have a lot more types in Python:

and more!

We’ll explore them all soon.


→ Get my Python Handbook
→ Get my Python Handbook

download all my books for free

  • javascript handbook
  • typescript handbook
  • css handbook
  • node.js handbook
  • astro handbook
  • html handbook
  • next.js pages router handbook
  • alpine.js handbook
  • htmx handbook
  • react handbook
  • sql handbook
  • git cheat sheet
  • laravel handbook
  • express handbook
  • swift handbook
  • go handbook
  • php handbook
  • python handbook
  • cli handbook
  • c handbook

subscribe to my newsletter to get them

Terms: by subscribing to the newsletter you agree the following terms and conditions and privacy policy. The aim of the newsletter is to keep you up to date about new tutorials, new book releases or courses organized by Flavio. If you wish to unsubscribe from the newsletter, you can click the unsubscribe link that's present at the bottom of each email, anytime. I will not communicate/spread/publish or otherwise give away your address. Your email address is the only personal information collected, and it's only collected for the primary purpose of keeping you informed through the newsletter. It's stored in a secure server based in the EU. You can contact Flavio by emailing flavio@flaviocopes.com. These terms and conditions are governed by the laws in force in Italy and you unconditionally submit to the jurisdiction of the courts of Italy.

Related posts about python: