FREE INTERACTIVE ONLINE COURSE

Python 3

FOR BEGINNERS


"The first Python course that simply amazed me. Very well explained and easy to understand." (Alexandru Cosmin)

"The best Python course in Romania." (Iulian Geană)


ALL REVIEWS
LESSON 4
PAGE 2 / 4
Data Types
Home >>> Free Online Lessons, Python 3

Main Data Types

In Python, there are six main standard data types:

  • numerical
  • strings
  • lists
  • tuples
  • sets
  • dictionaries

The last four are data collections and will be studied separately in different sections. For now, we will focus on the first two.

Numerical Data Types

The numerical data types are int (signed integers), float (floating-point numbers), and complex (complex numbers). Examples:

int() float() complex()
11 0.0 2+3j
124 23.14 1-2j
-93 -173.955 3.14j
When defining a variable, we can use the respective function to impose the data type that will be retained.

Analyze the three examples below:
   
Editor - lesson_4_
1
.py
       
Console / Output done
OBSERVATIONS - Example 1

We used the function int() which took the text string input by the user as an argument. It converted it into a signed integer value that was stored in the variable x, and similarly for y. This time, the result is mathematically correct, which is what we wanted – the sum of the numbers.

Note. Since we imposed the int() data type for the two variables, x and y, if we try to input the text Star (or "Star" or 'Star') as the value for the first variable, we will now get an error:

ValueError: invalid literal for int() with base 10: 'Star' on line 1

x can only hold signed integer numbers because we used the int() function.

Note. In Python 3, the long type does not exist. Depending on the internal memory available, you can enter even a billion billion plus 1 😜:

1000000000000000001
Run all the programs and read the observations.
 home   list  CONTENTS   perm_identity   arrow_upward