This is a glossary of terms and topics in Python as covered in Codecademy courses. It does not attempt to completely define and explain terms, but rather provides a general overview that is appropriate for someone new to the language. For a more comprehensive treatment of these topics, we recommend the Mozilla Developer Network Python documentation.
A Python data type that holds an ordered collection of values, which can be of any type. This is equivalant to an "array" in many other languages. Python lists are "mutable," implying that they can be changed once created.
>> x = [1, 2, 3, 4]
>> y = ['spam', 'eggs']
>> x
[1, 2, 3, 4]
>> y
['spam','eggs']
>> y.append('mash')
>> y
['spam', 'eggs', 'mash']
>> y += ['beans']
>> y
['spam', 'eggs', 'mash', 'beans']
A pythonic way of extracting "slices" of a list using a special bracket notation that specifies the start and end of the section of the list you wish to extract. Leaving the beginning value blank indicates you wish to start at the beginning of the list, leaving the ending value blank indicates you wish to go to the end of the list. Using a negative value references the end of the list (so that in a list of 4 elements, -1 means the 4th element). Slicing always yields another list, even when extracting a single value.
>> # Specifying a beginning and end:
>> x = [1, 2, 3, 4]
>> x[2:3]
[3]
>> # Specifying start at the beginning and end at the second element
>> x[:2]
[1, 2]
>> # Specifying start at the next to last element and go to the end
>> x[-2:]
[3, 4]
>> # Specifying start at the beginning and go to the next to last element
>> x[:-1]
[1, 2, 3]
>> # Specifying a step argument returns every n-th item
>> y = [1, 2, 3, 4, 5, 6, 7, 8]
>> y[::2]
[1, 3, 5, 7]
>> # Return a reversed version of the list ( or string )
>> x[::-1]
[4, 3, 2, 1]
>> # String reverse
>> my_string = "Aloha"
>> my_string[::-1]
"aholA"
Variables are assigned values using the '=' operator, which is not to be confused with the '==' sign used for testing equality. A variable can hold almost any type of value such as lists, dictionaries, functions.
>> x = 12
>> x
12
Python builds functions using the syntax: def function_name(variable): Functions can be stand-alone or can return values. Functions can also contain other functions.
def add_two(a, b):
c = a + b
return c
# or without the interim assignment to c
def add_two(a, b):
return a + b
A Python data type that holds an ordered collection of values, which can be of any type. Python tuples are "immutable," meaning that they cannot be changed once created.
>> x = (1, 2, 3, 4)
>> y = ('spam', 'eggs')
>> my_list = [1,2,3,4]
>> my_tuple = tuple(my_list)
>> my_tuple
(1, 2, 3, 4)
Convenient ways to generate or extract information from lists. List Comprehensions will take a general form such as: [item for item in List if Condition]
>> x_list = [1,2,3,4,5,6,7]
>> even_list = [num for num in x_list if (num % 2 == 0)]
>> even_list
[2,4,6]
>> m_list = ['AB', 'AC', 'DA', 'FG', 'LB']
>> A_list = [duo for duo in m_list if ('A' in duo)]
>> A_list
['AB', 'AC', 'DA']
Sets are collections of unique but unordered items. It is possible to convert certain iterables to a set. {"this", "is", "a", "set"}
>> new_set = {1, 2, 3, 4, 4, 4,'A', 'B', 'B', 'C'}
>> new_set
{'A', 1, 'C', 3, 4, 2, 'B'}
>> dup_list = [1,1,2,2,2,3,4,55,5,5,6,7,8,8]
>> set_from_list = set(dup_list)
>> set_from_list
{1, 2, 3, 4, 5, 6, 7, 8, 55}
Dictionaries, like sets, contain unique but unordered items. The big difference is the concept of "keys" to retrieve "values"; "keys" can be strings, integers or tuples (or anything else hashable), but the "values" that they map to can be any data type.
>> my_dict = {}
>> content_of_value1 = "abcd"
>> content_of_value2 = "wxyz"
>> my_dict.update({"key_name1":content_of_value1})
>> my_dict.update({"key_name2":content_of_value2})
>> my_dict
{'key_name1':"abcd", 'key_name2':"wxyz"}
>> my_dict.get("key_name2")
"wxyz"
Strings store characters and have many built-in convenience methods that let you modify their content.
>> my_string1 = "this is a valid string"
>> my_string2 = 'this is also a valid string'
>> my_string3 = 'this is' + ' ' + 'also' + ' ' + 'a string'
>> my_string3
"this is also a string"
Using len(some_object) returns the number of _top-level_ items contained in the object being queried.
>> my_list = [0,4,5,2,3,4,5]
>> len(my_list)
7
>> my_string = 'abcdef'
>> len(my_string)
6
Augmenting code with human readable descriptions can help document design decisions.
# this is a single line comment.
Some comments need to span several lines, use this if you have more than 4 single line comments in a row.
'''
this is
a multi-line
comment, i am handy for commenting out whole
chunks of code very fast
'''
A function to display the output of a program. Using the parenthesized version is arguably more consistent.
>> # this will work in all modern versions of Python
>> print("some text here")
"some text here"
>> # but this only works in Python versions lower than 3.x
>> print "some text here too"
"some text here too"
The range() function returns a list of integers, the sequence of which is defined by the arguments passed to it.
argument variations:
range(terminal)
range(start, terminal)
range(start, terminal, step_size)
>> [i for i in range(4)]
[0, 1, 2, 3]
>> [i for i in range(2, 8)]
[2, 3, 4, 5, 6, 7]
>> [i for i in range(2, 13, 3)]
[2, 5, 8, 11]
Python provides a clean iteration syntax. Note the colon and indentation.
>> for i in range(0, 3):
>> print(i*2)
0
2
4
>> m_list = ["Sir", "Lancelot", "Coconuts"]
>> for item in m_list:
>> print(item)
Sir
Lancelot
Coconuts
>> w_string = "Swift"
>> for letter in w_string:
>> print(letter)
S
w
i
f
t
A While loop permits code to execute repeatedly until a certain condition is met. This is useful if the number of iterations required to complete a task is unknown prior to flow entering the loop.
>> looping_needed = True
>>
>> while looping_needed:
>> # some operation on data
>> if condition:
>> looping_needed = False
Using the str() function allows you to represent the content of a variable as a string, provided that the data type of the variable provides a neat way to do so. str() does not change the variable in place, it returns a 'stringified' version of it.
>> # such features can be useful for concatenating strings
>> my_var = 123
>> my_var
123
>> str(my_var)
'123'
>> my_booking = "DB Airlines Flight " + str(my_var)
>> my_booking
'DB Airlines Flight 123'