Steve Iannaccone, Author at General Assembly Blog

Beginner’s Python Cheat Sheet

By

Do you want to be a data scientist? Data Science and machine learning are rapidly becoming a vital discipline for all types of businesses. An ability to extract insight and meaning from a large pile of data is a skill set worth its weight in gold. Due to its versatility and ease of use, Python programming has become the programming language of choice for data scientists.

In this Python crash course, we will walk you through a couple of examples using two of the most-used data types: the list and the Pandas DataFrame. The list is self-explanatory; it’s a collection of values set in a one-dimensional array. A Pandas DataFrame is just like a tabular spreadsheet, it has data laid out in columns and rows.

Let’s take a look at a few neat things we can do with lists and DataFrames in Python!
Get the PDF here.

BEGINNER’S Python Cheat Sheet

Lists

Creating Lists

Let’s start this Python tutorial by creating lists. Create an empty list and use a for loop to append new values. What you need to do is:

#add two to each value
my_list = []
for x in range(1,11):
my_list.append(x+2)

We can also do this in one step using list comprehension:

my_list = [x + 2 for x in range(1,11)]

Creating Lists with Conditionals

As above, we will create a list, but now we will only add 2 to the value if it is even.

#add two, but only if x is even
my_list = []
for x in range(1,11):
if x % 2 == 0:
my_list.append(x+2)
else:
my_list.append(x)

Using a list comp:

my_list = [x+2 if x % 2 == 0 else x \
for x in range(1,11)]

Selecting Elements and Basic Stats

Select elements by index.

#get the first/last element
first_ele = my_list[0]
last_ele = my_list[-1]

Some basic stats on lists:

#get max/min/mean value
biggest_val = max(my_list)
smallest_val = min(my_list)avg_val = sum(my_list) / len(my_list)

DataFrames

Reading in Data to a DataFrame

We first need to import the pandas module.

import pandas as pd

Then we can read in data from csv or xlsx files:

df_from_csv = pd.read_csv(‘path/to/my_file.csv’,
sep=’,’,
nrows=10)
xlsx = pd.ExcelFile(‘path/to/excel_file.xlsx’)
df_from_xlsx = pd.read_excel(xlsx, ‘Sheet1’)

Slicing DataFrames

We can slice our DataFrame using conditionals.

df_filter = df[df[‘population’] > 1000000]
df_france = df[df[‘country’] == ‘France’]

Sorting values by a column:

df.sort_values(by=’population’,
ascending=False)

Filling Missing Values

Let’s fill in any missing values with that column’s average value.

df[‘population’] = df[‘population’].fillna(
value=df[‘population’].mean()
)

Applying Functions to Columns

Apply a custom function to every value in one of the DataFrame’s columns.

def fix_zipcode(x):
”’
make sure that zipcodes all have leading zeros
”’
return str(x).zfill(5)
df[‘clean_zip’] = df[‘zip code’].apply(fix_zipcode)

Ready to take on the world of machine learning and data science? Now that you know what you can do with lists and DataFrames using Python language, check out our other Python beginner tutorials and learn about other important concepts of the Python programming language.

3 Reasons Python Programming is So Popular

By

Since its introduction in the ’90s, Python has rapidly become one of the world’s most popular programming languages. Most recently, we have seen Python even surpass other languages like Java. How has a humble language like Python managed to gain so much attention? Why is Python so popular?

Some estimates claim there were over 8 million active users of Python by the end of 2018. What has created the demand for this programming language compared to Java with 7.6 million, C# with 6.7 million, and JavaScript with 11.7 million active users at the end of 2018? One way to think about using a programming language is to think about its primary use case. In the case of JavaScript, the primary function is building software for the web or the cloud. Cloud infrastructure and web development are still very common business needs. For C# and Java, these use cases are more driven to desktop application development, which has started to fall off with the rise of the mobile-first mentality of end-users.

1. The rise of analytics and Python.

With Python, the use cases are shifting to data analysis and machine learning. As Clive Humby stated back in 2006, “Data is the new oil.” The bottom line is that data science has a high value. Companies have made data analytics and data science a priority due to their abilities to maximize profits and gain better insights on business. Because of well-developed resources like the data science workhorses of Pandas and Scikit-learn, Python easily does the heavy-lifting of machine learning algorithms.

Along with ready-made tools to do the work, Python is also an incredibly readable programming language. Its syntax was explicitly designed to remove a lot of unnecessary code and emphasize making it human-readable. Python makes the development of complex programs easier to write and easier to manage, which translates directly to the bottom line of the company.

2. Why is Python so popular? One word of many: Free.

The facts that drive Python’s booming popularity: it is an open source and free to use. Developers all over the world are writing and distributing software packages in Python that small companies or individual developers can use in their projects for free. Who wouldn’t want to be able to plug into a sophisticated image segmentation library developed by Google? At no cost! Just a few years ago, similar image analysis software cost thousands of dollars and was not nearly as user-friendly.

3. It takes a village.

Python programming is easy to learn, easy to write, cheap to build with, and massive followings of programmers worldwide. It’s no wonder Python is rapidly gaining in popularity. One of the worst feelings for new developers is not understanding why their program isn’t working, but with Python, the programming and data science communities are very active. Blog posts, answer sites like StackOverflow, and groups on LinkedIn have made getting feedback and solutions to your issues easier than ever. Getting hands-on help with issues quickly, learning, and picking up better development practices are no longer a daunting task.

The best way to learn any new language is to immerse yourself. Popular programming languages like Python are no different. The more time you interact with solving real-world problems with a new language, the faster you can become fluent. There are tons of resources like YouTube videos and blog posts, but I find that there really isn’t a better-suited way to learn than hands-on teaching. You need to raise your hand and ask an instructor attuned to the Python language, programming languages, Python code, data science, python developers, artificial intelligence, programming, and machine learning, and more.

General Assembly: the bridge to machine learning.

The immense rise of use cases and companies hiring developers, allows an increase in places to learn these new skills. General Assembly has a multitude of ways to get you started on the path to learning Python and becoming a Python developer. Informal and free introduction sessions at General Assembly aim to get you running code in just a couple of hours. Part-time classes take things up a notch by giving you focused hands-on lessons twice a week, over 10 weeks — artificial intelligence will have nothing on you. For those future Python developers that are ready to take the plunge, and want a deep-dive into all things machine learning, General Assembly also offers full-time Data Science Immersive programs every quarter to learn Python code, programming, nuances of artificial intelligence — and more.

Why is Python so popular? These reasons are a very good place to start!

How Long It Takes to Learn Python

By

Python, an essential programming language, has taken the programming world by storm. Much of this attention has followed from the interest in machine learning and AI. Python has become the default programming language of Data Scientists and Machine Learning Engineers all over the world. Python’s versatility has also gained a loyal following not just in computer science but also amongst diverse fields like Bioinformatics, Astronomy, Gaming, and of course, Data Science.

But utility alone doesn’t explain why so many developers love using Python. From its humble beginnings in 1991, Python was designed by Guido van Rossum to be a programming language that emphasized code readability. Or in Guido’s words, “Computer Programming for Everybody.” This ease of human interpretability pairs with an open source ethos that makes it available to developers everywhere for free! So with a few short lines of code you can import packages and libraries that professional developers from companies like Facebook, Google, or AirBnB have spent thousands of hours building _(for free)_.

It’s low-entry cost and ease of reading programs has rightfully garnered Python an immense and passionate following.

1. Where there is talent, there is an opportunity, especially, in Python programming.

Python has rapidly become a deep learning skill that is in high demand within the job market. Jobs sites like Dice and Glassdoor have seen near-exponential growth in postings looking for candidates with Python skills over the last few years because making pivot tables and wrangling data in spreadsheets is no longer enough to get you noticed for data analyst positions. As the variety, velocity, and volume of data has exploded, developers have had to scale their analysis pipelines to match — this means that the people pouring over those numbers must develop a deeper skill set to deal with the enormous amounts of data piling up in their databases.

2. Speed and flexibility are the names of the game!

Python is ideal for handling the heavy-lifting required for today’s computationally intense data analyses used by most businesses today.

OK, so now that you’re sold on its value, how long does it take to learn Python? Like any language, practice and muscle memory are the name of the programming language game. The more time you can immerse yourself, the quicker you will see gains.

It also depends on how much you intend to learn during this process. You can figure out elementary Python and have a simple “Hello World” program running in a matter of minutes, i.e., _Seriously; it is only one line of code!_, etc. To get an understanding of deep learning, a subset of machine learning, or data scientist techniques may take months of focused study, pushing past basic concepts. But, to get your foot in the door as a Data Analyst, it takes about 40–50 hours of studying and practicing — in my computer programming experience.

Some of the rudimentary skills from loading required packages, the underlying data structures, and some simple data manipulation take some effort to put into practice. Remember that learning anything takes motivation and attention, especially when learning a new programming language. With our focus being pulled in many directions at once, sometimes having some guided learning  as a programmer can be a huge help — especially with data analysis and data analysts.

How often have you had a problem you spent hours trying to solve by Googling every corner of the internet, only to have the solution explained to you in three seconds by an expert? You can have industry professionals help guide you through this exciting learning adventure to help make sure you are spending your effort in the right places rather than sift through all the YouTube videos, blogs, or StackOverflow posts.

3. General Assembly Python programming FTW!

Often you get back what you put in. So if you are thinking about getting started on your programming language journey of learning Python, General Assembly has several great ways to get you started.

Free Introduction to Python workshops are held regularly. The aim here is to get you set up to start learning and developing in a couple of hours.

There is a 10-week part-time Python course that give you all the programming language skills you need to start a new career as a Data Analyst or Python Developer for those that are ready for more structured and in-depth learning. These classes are held for two hours, twice a week, over 10 weeks.

For those who like to jump in and learn as much as possible in concentrated, full-time sessions every day, General Assembly offers a 13-week Data Science Immersive as well, which covers all the essentials of putting Python programming into good use for Machine Learning and Data Science.

4. Dive into Python programming + a Python course.

If you are on the fence about learning the programming language Python, I strongly suggest you dive in and don’t look back! I have found the transition from being a Data Analyst in a cancer research lab to becoming a Data Scientist at an InsureTech company, one of the best experiences of my life. All the nerdy things I loved, i.e.,  _(computers, stats, data visualization)_, all banded together in an amazing career path

How long does it take to learn the Python programming language? The answer is your learning path up to YOU. 

Are you ready to start your next chapter and boost your coding skills as a python programmer?