Introduction
This tutorial provides a basic introduction to the Python programming language. If you are new to Python, you should run the code snippets while reading this tutorial. If you are an advanced Python user, please feel free to skip this chapter.
Basic Variable Types
The basic types of variables in Python are: strings, integers, floating point numbers and booleans.
Strings in python are identified as a contiguous set of characters represented in either single quotes (' ') or double quotes (" ").
my_string1 = 'Welcome to'
my_string2 = "QuantConnect"
print(my_string1 + ' ' + my_string2)
[out]: Welcome to QuantConnect
An integer is a round number with no values after the decimal point.
my_int = 10
print(my_int)
[out]: 10
print(type(my_int))
[out]: type 'int'
The built-in function int()
can convert a string into an integer.
my_string = "100"
print(type(my_string))
[out]: type 'str'
my_int = int(my_string)
print(type(my_int))
[out]: type 'int'
A floating point number, or a float, is a real number in mathematics. In Python we need to include a value after a decimal point to define it as a float.
my_float = 1.0
print(type(my_float))
[out]: type 'float'
my_int = 1
print(type(my_int))
[out]: type 'int'
As you can see above, if we don't include a decimal value, the variable would be defined as an integer. The built-in function float()
can convert a string or an integer into a float.
my_string = "100"
my_float = float(my_string)
print(type(my_float))
[out]: type 'float'
A boolean, or bool, is a binary variable. Its value can only be True
or False
. It is useful when we do some logic operations, which would be covered in our next chapter.
my_bool = False
print(my_bool)
[out]: False
print(type(my_bool))
[out]: type 'bool'
Basic Math Operations
The basic math operators in python are demonstrated below:
print(f"Addition {1+1}")
print(f"Subtraction {5-2}")
print(f"Multiplication {2*3}")
print(f"Division {10/2}")
print(f"Exponent {2**3}")
[out]:
Addition 2
Subtraction 3
Multiplication 6
Division 5
Exponent 8
Data Collections
List
A list is an ordered collection of values. A list is mutable, which means you can change a list's value without changing the list itself. Creating a list is simply putting different comma-separated values between square brackets.
my_list = ['Quant', 'Connect', 1,2,3]
print(my_list)
[out]: ['Quant', 'Connect', 1, 2, 3]
The values in a list are called "elements". We can access list elements by indexing. Python index starts from 0. So if you have a list of length n
, the index of the first element will be 0, and that of the last element will be n
− 1. By the way, the length of a list can be obtained by the built-in function len()
.
my_list = ['Quant', 'Connect', 1,2,3]
print(len(my_list))
[out]: 5
print(my_list[0])
[out]: Quant
print(my_list[len(my_list) - 1])
[out]: 3
You can also change the elements in the list by accessing an index and assigning a new value.
my_list = ['Quant','Connect',1,2,3]
my_list[2] = 'go'
print(my_list)
[out]: ['Quant', 'Connect', 'go', 2, 3]
A list can also be sliced with a colon:
my_list = ['Quant','Connect',1,2,3]
print(my_list[1:3])
[out]: ['Connect', 1]
The slice starts from the first element indicated, but excludes the last element indicated. Here we select all elements starting from index 1, which refers to the second element:
print(my_list[1:])
[out]: ['Connect', 1, 2, 3]
And all elements up to but excluding index 3:
print(my_list[:3])
[out]: ['Quant', 'Connect', 1]
If you wish to add or remove an element from a list, you can use the append()
and remove()
methods for lists as follows:
my_list = ['Hello', 'Quant']
my_list.append('Hello')
print(my_list)
[out]: ['Hello', 'Quant', 'Hello']
my_list.remove('Hello')
print(my_list)
[out]: ['Quant', 'Hello']
When there are repeated instances of "Hello", the first one is removed.
Tuple
A tuple is a data structure type similar to a list. The difference is that a tuple is immutable, which means you can't change the elements in it once it's defined. We create a tuple by putting comma-separated values between parentheses.
my_tuple = ('Welcome','to','QuantConnect')
Just like a list, a tuple can be sliced by using index.
my_tuple = ('Welcome','to','QuantConnect')
print(my_tuple[1:])
[out]: ('to', 'QuantConnect')
Set
A set is an unordered collection with no duplicate elements. The built-in function set()
can be used to create sets.
stock_list = ['AAPL','GOOG','IBM','AAPL','IBM','FB','F','GOOG']
stock_set = set(stock_list)
print(stock_set)
[out]: set(['GOOG', 'FB', 'AAPL', 'IBM', 'F'])
Set is an easy way to remove duplicate elements from a list.
Dictionary
A dictionary is one of the most important data structures in Python. Unlike sequences which are indexed by integers, dictionaries are indexed by keys which can be either strings or floats.
A dictionary is an unordered collection of key : value pairs, with the requirement that the keys are unique. We create a dictionary by placing a comma-separated list of key : value pairs within the braces.
my_dic = {'AAPL': 'Apple', 'FB': 'FaceBook', 'GOOG': 'Alphabet'}
After defining a dictionary, we can access any value by indicating its key in brackets.
print(my_dic['GOOG'])
[out]: Alphabet
We can also change the value associated with a specified key:
my_dic['GOOG'] = 'Alphabet Company'
print(my_dic['GOOG'])
[out]: Alphabet Company
The built-in method of the dictionary object dict.keys()
returns a list of all the keys used in the dictionary.
print(my_dic.keys())
[out]: ['GOOG', 'AAPL', 'FB']
Common String Operations
A string is an immutable sequence of characters. It can be sliced by index just like a tuple:
my_str = 'Welcome to QuantConnect'
print(my_str[8:])
[out]: to QuantConnect
There are many methods associated with strings. We can use string.count()
to count the occurrences of a character in a string, use string.find()
to return the index of a specific character, and use string.replace()
to replace characters.
print("Counting the number of e's in this sentence".count('e'))
[out]: 6
print('The first time e appears in this sentence'.find('e'))
[out]: 2
print('all the a in this sentence now becomes e'.replace('a','e'))
[out]: ell the e in this sentence now becomes e
The most commonly used method for strings is string.split()
. This method will split the string by the indicated character and return a list:
Time = '2016-04-01 09:43:00'
splited_list = Time.split(' ')
date = splited_list[0]
time = splited_list[1]
print(f'{date} {time}')
[out]: 2016-04-01 09:43:00
hour = time.split(':')[0]
print(hour)
[out]: 09
We can replace parts of a string by our variable. This is called string formatting.
my_time = 'Hour: {}, Minute: {}'.format(9, 43)
print(my_time)
[out]: Hour: 9, Minute: 43
Another way to format a string is to use the % symbol.
print('pi is %f' % 3.14)
[out]: pi is 3.140000
print('%s to %s' % ('Welcome', 'QuantConnect'))
[out]: Welcome to QuantConnect
%s is a placeholder that takes in a string. Similarly %f takes a float and %d takes an integer.
Finally we can format a string using the f-string feature.
import numpy as np
print(f'pi is {np.pi}')
[out]: pi is 3.141592653589793
Summary
We have seen the basic data types and data structures in Python. It's important to keep practicing to become familiar with these data structures. In the next tutorial, we will cover for and while loops and logical operations in Python.