Python is a versatile, open-source programming language that is commonly used for data science. It is flexible and has many applications due to its numerous packages: collections of functions and data that work together to achieve a specific task. Python is widely used for data science because there are several popular packages used for different aspects of data science. With Python, data scientists can connect to data from a variety of sources, transform the data, create machine learning algorithms, and visualize the results.

Since Python is an open-source technology, it’s free to use and expand upon. Its readability, ability to easily add functionality with libraries, and the fact that it’s a high-level language make it one of the easiest coding languages to learn.

Simply put, Python is simple yet powerful.

Key Highlights

Python is a versatile, open-source programming language that is commonly used for data science.

Python is a must use for data scientists and allows data analysts to take their capabilities to the next level.

Python syntax uses variables that store data and then let us reference that data throughout our code.

Who Should Use Python?

Python can be used by almost anyone, from web developers to data scientists to less technical professionals. When there is a need for automation and analysis, Python is a great tool to use.

Choosing to learn Python programming may be one of the smartest moves you can make in your finance career. It is the most popular language for machine learning applications, yet it can also be used for anything from web development to workflow automation. Financial professionals rely on Python to load and clean data sets in order to analyze and visualize that data. It doesn’t take much to learn how to optimize a financial portfolio in Python, and that’s only the beginning.

Python Syntax

Variables

Foundational for Python and coding in general, variables store data and then let us reference that data throughout our code. This makes our programs more flexible, gives us the ability to take input, and also makes it easier to read and reference.

Python has four basic data types.

Integer: profit = 5000000

Float: price = 5.99

Boolean: repeat_customer = True

String: customer_name = “Sam”

As you can see from the above, integers and floats both correspond to numeric values, with integers being whole numbers and floats being decimals. Booleans are either True or False, while a string simply refers to text.

It’s important to note that variables can be put together with different operators, but they need to be of the same variable type. So, for instance, if we want to print the statement “Sam spent 5.99” using our variables, our code will look like this:

print(customer_name + “ spent “ + str(price))

We use the print() function alongside str() to convert the price float into a string. We can use int() to transform variables into integers, while float() and bool() convert to floats and booleans, respectively.

Data structures

We can take manipulating data to the next level by leveraging Python’s built-in data structures. The main three are lists, tuples, and dictionaries.

A list is a group of data points collected in a sequence and denoted with [ ]. For instance:

We now have a list of strings, and we can access those items by leveraging the power of a list’s index, which starts at 0. Let’s access the third item of our list.

print(qualified_leads[2])
‘Karina’

We can also use negatives to start from the end of the list

print(qualified_leads[-3])
‘John’

Lists can contain a mix of any of the data types, and they can even contain other lists.

One of the main reasons to use lists is because they are mutable, meaning that we can manipulate them. Let’s start by sorting by ascending order. This works equally well for sorting numbers for low to high or sorting strings in alphabetical order.

We can also do reverse order, from largest to smallest or in reverse alphabetical order. Keep in mind, though, that we can only sort lists that contain all the same data type.

del qualified_leads[3]
[‘Yuting’, ‘Oscar’, “Katrina”, ‘Rosie’]

Another important data structure is the tuple, a collection of values separated by commas and enclosed in ( ). They can contain all the same items as a list, including other lists and tuples. For instance:

fixed_costs = (2000, 50, 320, 800)

Tuples are similar to lists, but they’re immutable, so they can’t be changed. This can be a benefit if we want to maintain the integrity of data and ensure that nothing can be altered or deleted.

The third data structure is the dictionary, which is a collection of unordered key value pairs denoted with { }. For example:

Now it’s time to build logic into our code. We’ll start with the standard operators for math, comparison, and logic.

The math operators are fairly straightforward: add, subtract, multiply, divide, exponent, and modulo, which means that we divide two expressions and return the remainder. For instance 4 % 3 returns 1.

Comparisons return a boolean, True or False. Larger than and less than can only be used for numbers, while equal (==) or not equal (!=) can be used for both numeric and string values.

Logical operators allow us to put operators and functions together, such as comparisons for a more complex result. When using the ‘and’ operator, both conditions must be satisfied to return True. We may want to ensure that a customer is qualified if they meet both revenue and order volume requirements, for example.

On the other hand ‘or’ will return true if either condition is satisfied. One example could be approving a customer for a credit card if they have an individual income above $60k or a household income above $100k.

Python also includes a suite of built-in functions for performing a wide range of tasks. Here are some basics.

max() returns the maximum value from a group of values.

max(4, 7, 23, 11) 23

min() returns the minimum.

min(4, 7, 23, 11) 4

abs() returns the absolute value.

abs(-9.4) 9.4

sum() returns the sum of the values.

sum([1, 2, 3]) 6

round() rounds to a specific decimal place. The first argument is the number we wish to round, and the second is the number of desired decimal places.

round(4.91278, 2) 4.19

pow() returns the first value to the power of the second value. It’s equivalent to the ** operator.

pow(2, 3) 8

Finally, len() returns the number of values within a list.

len([1, 3, 5, 7, 9]) 5

If statements and for loops

Once we’ve got the hang of variables, data structures, operators, and basic functions, we can begin incorporating them into even more complex logic with conditional if/then statements and “for loops”, which can iterate logic over a sequence. Both of these techniques open up an incredible number of possibilities, make our code more powerful, and unlock useful applications.

Python Packages for Data Analysis

It’s also easy to go beyond Python’s standard functionality by integrating pre-built packages. This can be as simple as using themath package to access the sqrt() function to find a number’s square root, or it can go as far as using advanced data science packages likepandas orNumPy.

Develop analytical superpowers by learning how to use programming and data analytics tools such as VBA, Python, Tableau, Power BI, Power Query, and more.