Data Types & Variables

Python - A Quick Start for existing Programers

2 min read

Published Sep 16 2025, updated Sep 30 2025


21
0
0
0

Python

In Python, variables do not have their types declared and are automatically assigned types based on the contents at run time.


Variables should be declared using lower case, snake case, eg. my_variable. Variable names are case sensitive, so My_Variable is considered different to my_variable.


There is no concept of constants in Python. Standard practice is to use capitalised variables for constants as a naming convention, however, these are still variables so can be changed. eg. MY_CONSTANT.



Basic data types

NoneType : represents Nothing, or No Value:

x = None

int : integer values:

x = 4

No minimum or maximum number, only restricted by memory available.


float : floating point numbers (decimals):

x = 4.3

Smallest number 2.2250738585072014e-308, largest number 1.7976931348623157e+308, Anything beyond that becomes inf (infinity).


complex : complex numbers:

A complex number has two parts:

  • a real part (like an ordinary number)
  • an imaginary part (multiplied by j in Python, which represents √-1)
a + bj

where

  • a = real part (float or int)
  • b = imaginary part (float or int)
  • j = imaginary unit

You write j (not i, as in math) to denote the imaginary unit:

x = 3 + 5j
# Access the real number part -> 3.0
print(x.real)
# Access the imaginary number part -> 5.0
print(x.imag)

Minimum and maximums are the same as for float for each part.


bool : boolean (True or False):

is_valid = True



Text data type

str : string (sequence of Unicode characters), wrapped in single or double quotes:

name = "Pete"
animal = 'Frog'

No maximum length, only restricted by memory available.



Multi value data types

list : ordered, mutable collection, wrapped in square brackets:

fruits = ["apple", "banana", "cherry"]

tuple : ordered, immutable collection, wrapped in standard brackets:

point = (10, 20)

set : unordered collection of unique elements, wrapped in curly brackets:

colors = {"red", "green", "blue"}

dict : dictionary of key-value pairs:

person = {
    "name": "Ana",
    "age": 30,
    "country": "UK"
}

bytes : immutable sequence of bytes:

b = b"hello"

bytearray : mutable sequence of bytes:

ba = bytearray([65, 66, 67])



Casting one data type to another

Each data type has a function that can be called to cast data from one type to another:

# Casting to an integer
int(3.9)
# = 3
int("42")
# = 42

# Casting to a float
float(5)
# = 5.0
float("3.14")
# = 3.14

# Casting to a complex (real part = x, imag = 0)
complex(5)
# = (5+0j)
complex(2, 3)
# = (2+3j)

# Casting to a string
str(123)
# = "123"
str(3.14)
# = "3.14"

# Casting to a list
list("abc")
# = ['a', 'b', 'c']

# Casting to a tupal
tuple([1, 2])
# = (1, 2)

# Casting to a set
set([1, 2, 2, 3])
# = {1, 2, 3}

# Casting to a dictionary
dict([(1, "a"), (2, "b")])
# = {1: 'a', 2: 'b'}

# Casting to bytes
bytes("hi", "utf-8")
# = b'hi'

# Casting to a mutable version of bytes
bytearray([65, 66, 67])
# = bytearray(b'ABC')



Checking a variables type

The built-in type() function returns the type (class) of an object:

x = 42
print(type(x))
# <class 'int'>

y = "hello"
print(type(y))
# <class 'str'>

You can use the type() function for a direct comparison:

x = 3.14

if type(x) is float:
    print("x is a float")

Note: This checks for an exact match, not subclasses.



Using isinstance() is safer and more flexible, because it works with inheritance:

x = 3.14

if isinstance(x, float):
    print("x is a float")

# Multiple types
if isinstance(x, (int, float)):
    print("x is a number")

You can use issubclass() to check if a class is derived from another class:

print(issubclass(bool, int))
# True (in Python, bool is a subclass of int)

Products from our shop

Docker Cheat Sheet - Print at Home Designs

Docker Cheat Sheet - Print at Home Designs

Docker Cheat Sheet Mouse Mat

Docker Cheat Sheet Mouse Mat

Docker Cheat Sheet Travel Mug

Docker Cheat Sheet Travel Mug

Docker Cheat Sheet Mug

Docker Cheat Sheet Mug

Vim Cheat Sheet - Print at Home Designs

Vim Cheat Sheet - Print at Home Designs

Vim Cheat Sheet Mouse Mat

Vim Cheat Sheet Mouse Mat

Vim Cheat Sheet Travel Mug

Vim Cheat Sheet Travel Mug

Vim Cheat Sheet Mug

Vim Cheat Sheet Mug

SimpleSteps.guide branded Travel Mug

SimpleSteps.guide branded Travel Mug

Developer Excuse Javascript - Travel Mug

Developer Excuse Javascript - Travel Mug

Developer Excuse Javascript Embroidered T-Shirt - Dark

Developer Excuse Javascript Embroidered T-Shirt - Dark

Developer Excuse Javascript Embroidered T-Shirt - Light

Developer Excuse Javascript Embroidered T-Shirt - Light

Developer Excuse Javascript Mug - White

Developer Excuse Javascript Mug - White

Developer Excuse Javascript Mug - Black

Developer Excuse Javascript Mug - Black

SimpleSteps.guide branded stainless steel water bottle

SimpleSteps.guide branded stainless steel water bottle

Developer Excuse Javascript Hoodie - Light

Developer Excuse Javascript Hoodie - Light

Developer Excuse Javascript Hoodie - Dark

Developer Excuse Javascript Hoodie - Dark

© 2025 SimpleSteps.guide
AboutFAQPoliciesContact