In this 6-hour workshop, students will be introduced to a variety of topics in a rather fast-paced manner. This guide is supposed to help students prep to get the most out of this 3-hour course. This guide will cover Python’s definitions, uses, and applications, as well as Python’s syntax and programming principles within in it.
What is Python?
Python is a programming language much like Java & C++ in both use and functionality. So why is everyone talking about it? Python is considered the new and improved coding language because of its English like, minimal syntax style paired with its versatility. To break down this sentence.
Why is Python easy to learn?
Python writes differently than other preferred computer languages. For example, Java needs a semi-colon after each line of code, while Python only needs a colon after conditional statements (you’ll learn about those later). Another example that depicts this stark syntactical difference is the print function in Java versus Python. In Java to print a piece of your code, programmers must write System.out.println(), but in Python it is simply the word print. This is just a few examples that exemplify the simplicity of Python’s language.
Why is Python so powerful?
Python simplicity does not compromise its power and versatility. Python is extremely fast and can be used for data science, web development, and software. The frameworks, libraries, and packages in Python allow it to be the preferred language for everything data. This means that Python has built-in features that allow you to perform lots of actions on your code without writing your own code for it. An example is the Sum function in Python, which works very similar to the Excel sum function. Python has a built-in function that knows what Sum does when its written, so there is no need to write your own code to perform addition.
Python is also an amazing data aggregator because of the Beautiful Soup Library. This allows Python to be used to pull data from websites, the verb of pulling data is called ‘scraping’. After scraping data, students can input the data in Jupyter Notebook. The Jupyter Notebook is an open-source web application that allows programmers to create and share live code documents. This notebook also supports different package imports such as Pandas, NumPy, and Matplotlib that allow for data science. If you’re having trouble understanding what Jupyter Notebook is - think of it as Google Docs for Python.
What is the purpose of writing Python code?
Before going into the nitty-gritty syntax students must know that Python is way of commanding your computer to do certain task, these tasks are your code and within your code you have statements such as print.
Launching our Python Package
We are going to use Anaconda in the Python workshop. So, let’s learn what it is and how to open it so we can practice a bit throughout this article.
If you do not have Anaconda use this link to download it for Mac & this for PC. After downloading, type python -- into your terminal (use Finder and type terminal to find it) and press enter or command line if you are using a PC. The output should be Python 3.7, which shows you that you are running the most updated version of Python. So now what is Anaconda?
- Anaconda is an open-source distribution that simplifies package management for both Python and R. In simpler terms, Anaconda is a one-stop shop for software that runs Python. In this class, we will be starting using the QT console which allows us to code in Python in its most primitive environment.
Work on Math using Python
To get a feeling of typing in Python, let’s start with some basic calculations. If you type any two numbers with a +, -, /, *, **, % Python will boil it down to a value, just as a calculator would. The mathematical operators of * represents multiplication, / is division, ** is exponents, and lastly % is remainder division. So, try this on your own now and type some Math problems using these operators.
What are variables?
The first topic to discuss when going over Python is to understand variables, how to assign them, and how to work with them. Let’s go over how to create a variable first. The most important and really only rule of creating a variable is that it must start with a letter and have no spacing.
So, let’s go over how to do this, let’s create a variable called movie and assign it to the value “Pulp Fiction”. I will be explaining in Python programming verbiage so you can get used to it and feel comfortable in the workshop. Your current code should look exactly like this, movie = ‘Pulp Fiction’. It is truly important to put the quotations around Pulp Fiction (I will explain later in the article why). Now, every time a movie is referred to in the code it truly represents the value ‘Pulp Fiction’. You can see this by typing movie and pressing Enter to run your code and the output should be “Pulp Fiction”.
Is the equal sign in assigning variables act the same as the mathematical equal sign?
Another topic to discuss here is the presence of the = as the assignment operators. The equal sign in programming could be confusing because it is very different than a mathematical equal sign. It is really important to understand that one equal sign is to set a variable, and a double equal sign is used to test equality similar to the mathematical operator you are used to using. So, to go over some terminology the variable refers to the movie, and pulp fiction is the value or output of the variable. A good exercise to make sure that you understand this concept is to set two variables, one equal to 5 and one equal to 2, then multiply these two variables and make sure the output equals 10. (Hint: w = 5, q = 2, w*q).
- Data type: The data type of a value (or variable in some contexts) is an attribute that tells what kind of data that value can have such as Integer, Float, String, List, Boolean, and more!
- Integer: The first data type that will be introduced is an Integer, think of an integer as any whole number. To assign an integer to the variable name noble, your code should look like noble = 7.
- Float: Any number with a decimal point, for example4, 2.1, 4.3221 are all Floats. To assign a Float to the variable name nyc, your code should look like nyc = 3.11.
- String: A String is one of the most complex data type and is not considered a primitive data much like Integer and Float. Strings are anything that is surrounded by a double quotation or apostrophe, just as Pulp Fiction was at the beginning of the variable example. Strings are usually associated with words, but I like to think about them as a collection of characters in between single or double quotes, does not matter which you use but you must be consistent with respect to single or double quotes. The reason I like to think like that is because ‘7’ is considered a string and not considered a number.
- So, if you perform ‘7’ * 2, the computer will output ‘77’, because it is just duplicating the string, but if you do 7*2, the computer will treat it similar to a math problem and output it to 14. Strings are complicated but have many great functions and methods (will talk later on in the article) that make them a great data type. To practice create a variable named barack and set it to the string ‘Obama’, your code should look like barack = ‘Obama’.
- Boolean: Boolean is a piece of code that either evaluates to be true or false, for example, 5 > 7 or ‘house’ == ‘house’ – the former output is false and the latter is True. Type these into your computer and see what happens!
- Lists: A list is an ordered collection of data types and is denoted by its bracket syntax, think of lists as a grocery list. An empty list is created when you set a variable name = , you can add items to the list by using the. append This is a good way to start talking about methods & functions, which virtually have the same definition but different names because their syntaxes are different.
What are Functions?
Methods & Functions are used to perform tasks on data types, some methods and functions are built-in such as the sum function or the append method we saw above. Additionally, you can create your functions and methods and the motivation behind this is repeatability. For example, if you have a list of numbers and you want to test if a number is divisible by 2, instead of writing x / 2 == 0, you can create a function called div2, and then if you want to check if its divisible by 2 now you just called the function div2(number). This allows for efficiency, readability, and limits extra lines of code.
This guide is meant to give you a brief idea of what you'll learn in our Python workshop in New York City. During the class, we will review these concepts and take a step deeper by practicing with real-world examples. You'll write the code and get guidance from a top Python programmer.