Python can seem intimidating at first. A bunch of new words that look foreign to a pair of fresh eyes.
Let’s demystify that…
See a short definition below, click through for a longer post, code sample, and video explaining each word.
Python Glossary:
- Anaconda
- Abs
- Append
- Argument
- Assert
- Assigning A Variable
- Block
- Callable
- Class
- Conditional
- Continue
- Def
- Dictionary
- Double Underscore (Dunder)
- Exploratory Data Analysis (EDA)
- Exception
- Float
- For (Loop)
- Generator
- Immutable
- Import
- Indent
- Int
- IPython
- Iterable
- Jupyter
Anaconda
Anaconda is a free and open-source (you can view the source code) package of the Python and R programming languages for scientific computing. It is the starting point for data analysts + scientists around the world.
If you’re just starting out on your data journey, we highly recommend starting with Anaconda.
Abs (Absolute Value)
Abs stands for “absolute value.” This means the distance a number or vector is away from the origin or 0. The result is always positive.
Ex: abs(4) = 4
, abs(-3) = 3
Append
Append is used when you need to add items to a list in python. This is a very common operation, not just in Python, but also in every other programming language
my_list.append(new_item)
View Append Code Sample + Video
Argument
Arguments are the information that is passed into a function. Arguments and the word ‘parameter’ can be used interchangeably.
def my_function(argument1, argument2): # Do soething
View Argument Code Sample + Video
Assert
Assert in real life means “to state or declare.” In Python, assert
is used when debugging code. You present a statement to assert
, and if it is true
, then your code will pass. If not, then a AssertionError
will be raised.
x = "DataIndy" # If the condition you pass returns True, then nothing happens: assert x == "DataIndy" # If the condition you pass condition returns False # AssertionError is raised: assert x == "Lake Tahoe Trees"
View Assert Code Sample + Video
Assigning A Variable
Assigning a variable means to either 1) create a variable and give it a value or 2) update a variables value. Either way, you’re setting the value of a variable.
In python this is done via a single equals (“=”) sign.
# Assign the value 3 to variable x x = 3
View Assigning A Variable Code Sample + Video
Block
In Python, a block refers to a “block of code.” This is a piece of code that is generally part of a function, class, or loop.
An easy way to tell which blocks of code exist are through the code indents. Lines that are on the same indent (and not interrupted by other lines not on the same indent) are part of the same code block.
for i in range(5): print (i) # This is a single line and code block def my_func(): print (i) print (y) # Above we have a code block with two lines
View Block A Variable Code Sample + Video
Callable
Simply, a ‘callable’ is something that can be called. This is either a class with a __call__ method, or else a function. Most commonly you’ll be able to tell which items are callables by looking at the open/closed parentheses “()”
It is also a python keyword callable(your_item)
will return True
if your_item
is able to be called
my_func() # Callable class Counter: def __init__(self, value = 0): self.n = 0 def __call__(self): # Makes a class callable self.n += 1 return self.n
View Callable A Variable Code Sample + Video
Class
Python is an Object Oriented Programing (OOP) Language. This means that a large amount of code is done using classes. Classes are uses to keep related items together.
Say you had 5 students, and they each had 5 properties (age, grade, hometown, test score 1, test score 2). The class-less way to do this would be by creating 25 variables (5 students * 5 properties).
However with classes, you’re able to create a “Student Class” which has 5 properties. Think of this as a template that is able to hold information.
class Student: def __init__(self, age, grade, hometown, test1, test2): self.age = age self.grade = grade self.hometown = hometown self.test1 = test1 self.test2 = test2
View Class A Variable Code Sample + Video
Conditional
Python is able to check a condition and change the behavior of a program depending on certain outcomes.
For example, if you wanted to create a program that printed “mow the lawn” IF it was sunny, then you would be running that code based off of a condition.
The main conditional in python is the if...else
statement.
if is_sunny: # Conditional print ("Mow the lawn") # Run if True else: print ("Wait till the sun comes out") # Run if False
View Conditional A Variable Code Sample + Video
Continue
The python keyword continue
will skip to the next item in your loop. This is used when working with for
and while
loops.
For example, if you wanted to print the numbers 1 through 5, but skip 3, then you could execute continue
if the number is three.
for i in range(5): if i == 3: continue else: print (i)
Continue is cousins with break
and pass
View Continue Code Sample + Video
Def
def
is the command that starts a function or method in python. It is short for define.
def my_function(): print ("This is my function")
Dictionary
A dictionary is a type of data in python. Its identifying characteristic is the key:value
data structure and it’s curly brackets.
Dictionaries are especially useful when you want to quickly look up an item, and return another. Similar to a vlookup in Excel
my_dict = { 'key1' : 'value1', 'key2' : 'value2', 'key3' : 'value3', }
View Dictionary Code Sample + Video
Double Underscore (Dunder)
Dunders or Double Unders (Underscores) or Magic Methods are special methods within python. They are denoted by the preceeding and trailing double underscores “__”
Common examples include __init__
, __len__
, __name__
. They are mainly used within classes.
if __name__ = '__main__': my_program()
View Dunder Code Sample + Video
Exploratory Data Analysis (EDA)
EDA is the process of getting intimate with your data. In all seriousness, it’s the act of getting to know your data from a foundational level.
How many rows are there? Columns? What are the value distributions like? How many unqiue values are there?
EDA is as much of an art as it is a science. You’ll find out paths you’d like to explore later on!
Exception
Python throws errors. This means it will return an error if it doesn’t understand something. Exceptions are expected errors.
These are not the worse thing in the world because you can account for them without breaking your program. In order to do this, you’ll need to do exception handling or simply, making sure you account for errors.
To do exception handling, you need to use the try
…except
statements.
try: x = int(input("Please enter a number: ")) break except ValueError: print("Oops! That was no valid number. Try again...")
View Exception Code Sample + Video
Float
Python has many datatypes. The float
datatype represents a number with decimal points.
The close, more well-rounded, sister of the float
is the int
. You won’t think about floats too often…until they cause you data type errors and you’ll need to debug.
x = 5.6 print (type(x)) >>> <class 'float'>
View Float Code Sample + Video
For Loop
Python’s For
Loop will iterate through an iterable and process each item. The command reads almost like a regular sentence.
“For
each item in the whole. Do something with each item”
for name in ["Bob", "Sally", "Frank"]: print (name) >>> "Bob" >>> "Sally" >>> "Frank"
View For Loop Code Sample + Video
Generator
Generators are a special type of memory efficient function. They are especially useful when working with large data sets. They are an advanced Python topic, so use at your own caution!
# Generator function for an infinite sequence def my_generator(): x = 0 while True: yield x x += 1 next(g) >>> 0 next(g) >>> 1
View Generator Code Sample + Video
Immutable
Immutable means that your data object can not be edited after you create it. Common immutable objects are int, string, and tuples.
View Immutable Code Sample + Video
Import
In order to get other packages and libraries in your script, you’ll need to import
them. Import does what it sounds like, imports other things into your code. You can use a ‘nickname’ for you package if you specify an ‘as’ + name afterward.
import pandas as pd pd.do_something()
View Import Code Sample + Video
Indent
An indent in python is like an indent within general writing. You use an indent when you want to group code together. For example, everything below a for
loop that is indented once will be executed with the loop.
for i in range (5): print (i) # This line is indented once print ("This is my loop") # This line is also indented once print () # Finally, this line is also indented print ("Ended Loop") # This line is back to 0 indents
View Indent Code Sample + Video
Int
In python, an int
is a datatype, or a specific type of data. An int is a number without a decimal point. It’s close cousin the float is also a data type, but it has decimal point specification.
x = 5 print (type(x)) >>> <class 'int'>
IPython
An old word for the program that provided data analysis notebooks (Ex: IPython Notebooks). This has since been more generally rebranded as “Juptyer”
View IPython Code Sample + Video
Iterable
An iterable is anything you’re able to ‘iterate’ through. Or simply, you can traverse the items in an object one by one. The most common example of an iterator in Python is the list
.
# This list is an iterable my_list = [1,2,3,4,6] for i in my_list: print (i) >>> 1 >>> 2 >>> 3 >>> 4 >>> 6
View Iterable Code Sample + Video
Jupyter
Project Jupyter creates Data Analysis and Data Science notebooks. The project is an evolution of the previously (and still currently) very popular. Ipython Notebooks.