top of page

Python Datatypes

  • Writer: SimpleSyntax
    SimpleSyntax
  • Feb 17, 2023
  • 3 min read

Before making any sort of program, you should understand the basic datatypes in Python. Although there are others, here we will be showing the basic and most used datatypes. Others such as dictionaries and tuples will be more easily understood later on by understanding these datatypes first.


A datatype is what it sounds like, a type of data. Such as numbers, words, characters, lists and so on. When writing code however, you need to be aware of using the correct datatype or else your program may not function correctly. For instance, you may try to use a multiplication function but use the wrong datatype such as a string (meaning letters or words), which will not work correctly and cause an error.


The next blog post will cover variables, but just know that the to the left of the = sign is the variable, and to the right is the value.


int

An int is a datatypes that in simple terms, is a whole number, meaning no decimal points. The numbers 5, 10, 27 are valid int (standing for integer), while numbers 1.5, 7.2 are not as they contain decimals.

Syntax: declare a variable name like below, then an = with the int value

Example: int_num_1 = 20 is a variable containing the int value of 20.


float

A float is much like an int, however in this case, you are allowed to use decimal points. Such as 5.8, 10.9, 5.3. A float can contain a maximum of 15 decimal places.

Syntax: Just the same as int, except you use a value with a decimal

Example: float_num_1 = 20.386 is a variable containing the int value of 20.386


string

A string is essentially characters (letters) or words. Such as in the first blog post that contained the message "Hello World!". Note that while you can use a number in a string, it is still considered a string datatype, not an int or float, so trying to use a math function on a number in a string still will not work.

Syntax: Declare a variable name like the previous examples, however, when you type in your characters or words, they must be surrounded by either a double quote ( "" ) or single quote

( ' ' ). It is often good practice to type the quotes first, then write your string inside. Depending on your version of Sublime text, often typing in a " or ' will have it automatically fill in the other quote for you to save time.

Example: string_word = "Hello World!"


list

A list is also exactly as it sounds, a list of data. Shown above we see the variable string_word containing the value "Hello World!". In a list, you can contain multiple values within the one list.

Syntax: When making a list, the values must be surrounded by square brackets [ ]. Also, if your values are string, then they must also use the quotation marks previously covered. If they are int or floats, you do not need the quotation marks. It is important that you separate each value with a comma ( , ). This is so Python knows where to separate the values.

You can also mix string with int or float within the same list as shown below with the int 25 at the end.

Example: list_words = ["name","date","age","phone_number",25]


When trying these, remember that when you create a new file in Sublime text, save it right away as a python file or else your code will not work. Sublime text must know that it is a Python file. Saving it with a .py at the end tells Sublime text this is a Python file so will run it as such. E.g., you can first create then save your file as datatypes.py


Github link: If you have trouble, you can download a Python file from the SimpleSyntax Github resources here https://github.com/SimpleSyntax/SimpleSyntax . Just click the green button labelled <> Code and then click "download zip"


Examples in Sublime text editor




 
 
 

Comments


Subscribe to Simple Syntax

Thanks for subscribing!

bottom of page