Python Types


Every variable you make has a specific data type. Python is dynamically typed. What does that mean? The same variable can change type later in the program but it can only have one type at any one time. The types are integer, floating point, complex, string, list tuple, boolean and dictionary. As always, as very good website for learning technical things is w3schools.com. Check out their Python Tutorial. There is a lot of information there, much more than is here.

There are three types of numbers in Python: integers, floating points, and complex. If you want to get the type of a variable, say x, you can use print(type(x)).

Integer

An integer is a whole number, positive or negative, without decimals, of unlimited length.

Floating Point

A floating point number is a number, positive or negative, containing one or more decimals.

Complex

A complex number is a number with one defined part and one undefined part.

String

Strings in python are surrounded by either single quotation marks, or double quotation marks. Assigning a string to a variable is done with the variable name followed by an equal sign and the string: fname = “Bob”.

Like many other popular programming languages, strings in Python are arrays of bytes representing unicode characters. However, Python does not have a character data type, a single character is simply a string with a length of 1. Square brackets can be used to access elements of the string. greeting = “Hello, World!” print(greeting[1])

Since strings are arrays, we can loop through the characters in a string, with a for loop. The len() function returns the length of a string. The first character has index 0.

Boolean

The two boolean values are written as True and False.

There are 4 built-in data types in Python used to store collections of data. They are List, Tuple, Set, and Dictionary, all with different qualities and usage.

Lists

A list is a mutable series of items. Lists are created using square brackets:

myfruits = [“apple”, “plum”, “banana”]

You can remove duplicates from a list. Python has a set of built-in methods that you can use on lists/arrays.

Tuple

A tuple is an immutable series of items. Immutable means unchangeable.

Set

Dictionary

Python has a set of built-in methods that you can use on dictionaries.

DateTime Module

We don’t have a datetime type in Python. Instead, we can import a module.

Arrays

Python does not have built-in support for arrays.

Leave a Reply