Python Glossary - Codecademy
Python Glossary - Codecademy
Python Glossary
Programming reference for Python
Class
Python is an Language that supports the Object Oriented Programming
paradigm. Like other OOP languages, Python has classes which are de ned
wireframes of objects. Python supports class inheritance. A class may have
many subclasses but may only inherit directly from one superclass.
Syntax
class ClassName(object):
"""This is a class"""
class_variable
def __init__(self,*args):
https://www.codecademy.com/articles/glossary-python 1/20
9/15/2020 Python Glossary | Codecademy
self.args = args
def __repr__(self):
return "Something to represent the object as a string"
def other_method(self,*args):
# do something else
Example
class Horse(object):
"""Horse represents a Horse"""
species = "Equus ferus caballus"
def __init__(self,color,weight,wild=False):
self.color = color
self.weight = weight
self.wild = wild
def __repr__(self):
return "%s horse weighing %f and wild status is %b" %
(self.color,self.weight,self.wild)
def make_sound(self):
print "neighhhh"
def movement(self):
return "walk"
Syntax
class ClassName(SuperClass):
# same as above
# use 'super' keyword to get from above
Example
https://www.codecademy.com/articles/glossary-python 2/20
9/15/2020 Python Glossary | Codecademy
class RaceHorse(Horse):
"""A faster horse that inherits from Horse"""
def movement(self):
return "run"
def movement_slow(self):
return super(Horse,self).movement()
def __repr__(self):
return "%s race horse weighing %f and wild status is %b"
(self.color,self.weight,self.wild)
Comments
SINGLE-LINE COMMENTS
Example
https://www.codecademy.com/articles/glossary-python 3/20
9/15/2020 Python Glossary | Codecademy
MULTI-LINE COMMENTS
Some comments need to span several lines, use this if you have more than
4 single line comments in a row.
Example
'''
this is
a multi-line
comment, i am handy for commenting out whole
chunks of code very fast
'''
Dictionaries
Dictionaries are Python’s built-in associative data type. A dictionary is
made of key-value pairs where each key corresponds to a value. Like sets,
dictionaries are unordered. A few notes about keys and values: * The key
must be immutable and hashable while the value can be of any type.
Common examples of keys are tuples, strings and numbers. * A single
dictionary can contain keys of varying types and values of varying types.
Syntax
https://www.codecademy.com/articles/glossary-python 4/20
9/15/2020 Python Glossary | Codecademy
Example
>> 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"
Syntax
{key1:value1,key2:value2}
Example
https://www.codecademy.com/articles/glossary-python 5/20
9/15/2020 Python Glossary | Codecademy
Functions
Python functions can be used to abstract pieces of code to use elsewhere.
Syntax
def function_name(parameters):
# Some code here
Example
Syntax
Example
https://www.codecademy.com/articles/glossary-python 6/20
9/15/2020 Python Glossary | Codecademy
def shout(exclamation="Hey!"):
print exclamation
FUNCTION OBJECTS
Python functions are rst-class objects, which means that they can be
stored in variables and lists and can even be returned by other functions.
Example
def say_hello(name):
return "Hello, " + name
foo = say_hello("Alice")
# Now the value of 'foo' is "Hello, Alice"
fun = say_hello
# Now the value of 'fun' is a function object we can use like the
original function:
bar = fun("Bob")
# Now the value of 'bar' is "Hello, Bob"
Example
https://www.codecademy.com/articles/glossary-python 7/20
9/15/2020 Python Glossary | Codecademy
# A simple function
def say_hello(greeter, greeted):
return "Hello, " + greeted + ", I'm " + greeter + "."
https://www.codecademy.com/articles/glossary-python 8/20
9/15/2020 Python Glossary | Codecademy
Example
if current_action == 'PAUSE':
pause()
elif current_action == 'RESTART':
restart()
elif current_action == 'RESUME':
resume()
# This can get long and complicated if there are many values.
# Instead, we can use a dictionary:
response_dict = {
'PAUSE': pause,
'RESTART': restart,
'RESUME': resume
}
len()
https://www.codecademy.com/articles/glossary-python 9/20
9/15/2020 Python Glossary | Codecademy
Syntax
len(iterable)
Example
List
Comprehensions
Convenient ways to generate or extract information from lists.
Syntax
https://www.codecademy.com/articles/glossary-python 10/20
9/15/2020 Python Glossary | Codecademy
Example
Lists
A Python data type that holds an ordered collection of values, which can
be of any type. Lists are Python’s ordered mutable data type. Unlike tuples,
lists can be modi ed in-place.
Example
>> x = [1, 2, 3, 4]
>> y = ['spam', 'eggs']
>> x
https://www.codecademy.com/articles/glossary-python 11/20
9/15/2020 Python Glossary | Codecademy
[1, 2, 3, 4]
>> y
['spam','eggs']
>> y.append('mash')
>> y
['spam', 'eggs', 'mash']
>> y += ['beans']
>> y
['spam', 'eggs', 'mash', 'beans']
Loops
FOR LOOPS
Python provides a clean iteration syntax. Note the colon and indentation.
Example
https://www.codecademy.com/articles/glossary-python 12/20
9/15/2020 Python Glossary | Codecademy
Sir
Lancelot
Coconuts
WHILE LOOPS
Syntax
while condition:
//do something
Example
https://www.codecademy.com/articles/glossary-python 13/20
9/15/2020 Python Glossary | Codecademy
print()
A function to display the output of a program. Using the parenthesized
version is arguably more consistent.
Example
>> # but this only works in Python versions lower than 3.x
>> print "some text here too"
"some text here too"
range()
The range() function returns a list of integers, the sequence of which is
de ned by the arguments passed to it.
Syntax
https://www.codecademy.com/articles/glossary-python 14/20
9/15/2020 Python Glossary | Codecademy
argument variations:
range(terminal)
range(start, terminal)
range(start, terminal, step_size)
Example
>> range(4)
[0, 1, 2, 3]
>> range(2, 8)
[2, 3, 4, 5, 6, 7]
Sets
Sets are collections of unique but unordered items. It is possible to
convert certain iterables to a set.
Example
https://www.codecademy.com/articles/glossary-python 15/20
9/15/2020 Python Glossary | Codecademy
Slice
A Pythonic way of extracting “slices” of a list using a special bracket
notation that speci es 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.
Example
>> # 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
https://www.codecademy.com/articles/glossary-python 16/20
9/15/2020 Python Glossary | Codecademy
>> x[-2:]
[3, 4]
str()
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
‘stringi ed’ version of it. On a more technical note, str() calls the special
__str__ method of the object passed to it.
https://www.codecademy.com/articles/glossary-python 17/20
9/15/2020 Python Glossary | Codecademy
Syntax
str(object)
Example
>> str(my_var)
'123'
Strings
Strings store characters and have many built-in convenience methods that
let you modify their content. Strings are immutable, meaning they cannot
be changed in place.
Example
https://www.codecademy.com/articles/glossary-python 18/20
9/15/2020 Python Glossary | Codecademy
Tuples
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.
Example
>> x = (1, 2, 3, 4)
>> y = ('spam', 'eggs')
TUPLE ASSIGNMENT
https://www.codecademy.com/articles/glossary-python 19/20
9/15/2020 Python Glossary | Codecademy
Example
Variables
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.
Example
>> x = 12
>> x
12
https://www.codecademy.com/articles/glossary-python 20/20