DEV Community

loading...

Getting to Grips with Python: Data Types & Variables

rvtheverett profile image Ruth Everett Updated on ・4 min read

When I started learning Python a couple of months ago I was fairly overwhelmed with the amount of information out there and struggled at first to find a learning style that suited me. After realising that I learn best by reading and writing and video tutorials were not going to work for me, I started to read blog posts and guides to help me learn.

I thought it might be cool to digitise the notes that I have been taking, in a bid to help other people who are just getting started and might feel a little overwhelmed, as well as to have a reference guide for myself in the future. No harm in giving it a go, right? So here is the first part of the series, all about data types and variables.

What are data types?
'The data type of an item defines the type and range of values that the item can have'

Python's Data Types

There are three main data types in Python:

  1. Numbers
  2. Booleans
  3. Strings

Numbers

Again, there are three main types of numbers in Python:

  1. Integers
  2. Floating-Point
  3. Complex

Integers

An integer is comprised of all the whole numbers, both positive and negative. For example, both 27 and -27 would be classed as integers.

The amount of memory an integer occupies will depend on its value, for example 0 would take up 24 bytes, while 1 would occupy 28 bytes.

print(27) # A positive integer 
print(-27) # A negative integer 

num = 1234567 # Assigning an integer to a variable 
print(num) # This would print 1234567

Floating-Point Numbers

Floating-point numbers, or floats refer to all the positive and negative decimal numbers. For example, 7.5 and -7.5 are both floating-point numbers. With the ability to create decimals up to a very high decimal point, Python allows for accurate computations and precise values.

A float occupies 24 bytes of memory.

print(7.000002) # A positive float
print(-7.255) # A negative float

flt_pt = 27.333
print(flt_pt) # Prints 27.333

Complex Numbers

Complex numbers in Python are made up of a real and imaginary part and just like the print() statement, complex() us used to create complex numbers.

It is made up of two values; the real part followed by the imaginary part. complex(real, imaginary) Python has built-in support for complex numbers and the imaginary part is written with a j suffix e.g 7j.

print(complex(30+40)) # The output would be (70+0j)

complex_1 = complex(0,7)
complex_2 = complex(7,0)
print (complex(complex_1 + complex_2)) # Prints (7+7j)

Booleans

Booleans are used to determine whether the logic of an expression or comparison is correct. Also known as a bool, this data type allows us to choose between two values: true and false.

The syntax is bool([value]) and will return either false if the value is omitted or false, or true if the value is true.

my_list = []
print(my_list, 'is', bool(my_list)) # Prints False

my_list = [7]
print(my_list, 'is', bool(my_list)) # Prints True

Logical Operators

Logical operators can be used to manipulate the logic of the boolean expression.

Operator Purpose Notation
and And Prefix
not Not Prefix
or Or Prefix

Strings

'A string is defined as a collection of characters closed within single or double quote marks'

Strings may contain a full sentence, a single character or be completely empty.

print("Harry Potter") 

hp = "Harry Potter"
print(hp) # Will print Harry Potter

print("a") # A single character 

empty = "" # This is an example of a empty string

A blank space inside of a string is considered to be a character.

Finding the length of a string

You can identify the number of characters within a string by using the len statement.

my_string = "Hello World" # This string is 11 characters, including the space
print(len(my_string)) # The output will be 11

String indexing

Every character in a string is given a numerical index based on its position. This starts from 0, so the first character in a string will be 0.

Index - 0 1 2 3 4
E m b e r

The length of this string is 5, as there are 5 characters, starting from 0.

String slicing

String slicing is the process of obtaining a portion of a string, otherwise known as substring, by using its indices.

It is achieved using the following syntax string [start:end].

  • start is the character where the substring will start
  • end is where the substring will end

Using this method, the character at the end of the substring will not be included.

my_string = "Ember is the best"
print(my_string [0:7]) # This will print Ember i
print(my_string [1:9]) # This will print mber is 

Variables

A variable is simply what we would name our values, they allow us to give meaningful names to the data we are exploring. The simplest way to assign a value to a variable is through the = operator. For example, variable = value.

Variables also allow us to store data to be used later on when performing operations in the code. They are also mutable, so it is possible to update or replace them.

There are certain requirements that we must follow when naming variables, including:

  • The name can start with either an uppercase or lowercase letter
  • Numbers can appear anywhere in the name, except for at the start
  • The _ character can also appear anywhere in the name
  • It is not possible to use spaces, instead we have to use snake_case to create the variable name
  • Ensure the name of the variable is something meaningful and describes the value that it holds.

Please let me know if I have missed anything or if there is any other tips you would recommend for newbies like myself :)

Discussion

pic
Editor guide