Basics of programming
Computer programming varies from language to language in terms of implementation, but there are remarkable similarities among these languages in how their internal logic works. These programming basics are applicable for all programming languages with specific code implementations shown in Python.
Key Concepts
Variables |
Names assigned to Python objects of any data type. Variables must start with a letter. Underscores are encouraged. |
x=0
y=1
xy = x+y
xy_str = str(xy)
|
Iteration |
For loops are used to iterate through an iterable data object (e.g. a list). While loops are used to loop until a condition has been met |
for item in datalist:
print(item)
x=0
while x < 1:
x+=1
|
Conditionals |
If/Elif/Else statements that interpret if an object meets a condition. |
list_var = [1,’1’,1.0]
for item in list_var:
if type(item) == type(0):
print(‘Integer’)
elif type(item) == type(‘a’):
print(‘String’)
else:
print(‘Float’)
|
Zero-based indexing |
Data containers are accessed using indexes that start with 0. The indexes are passed to the list or tuple using square brackets []. String characters can be access using the same pattern. |
list_var = [‘s’,’m’,’t’]
m_var = list_var[0]
name_var = “logan”
g_var = name_var[0]
|
Data Types |
Strings are for text. Integers are for whole numbers. Floats are for floating point numbers. Data containers such as lists and tuples and dictionaries are used extensively to organized data. |
Str_var = “string”
int_var = 4
float_var = 5.7
list_var = [45,43,24]
tuple_var = (87.’a’,34)
dict_var = {‘key’:’value’}
|
Code Comments |
Comments in code are encouraged. They help explain your thinking to both other readers and yourself. Comments are created by using the “#” symbol. Comments can be on a line by themselves or can be added to the end of a statement as anything after the # symbol will be ignored. |
# This is a comment
x = 0 #also a comment
|
Errors |
Error messages of many types are built into Python. The error traceback show the affected lines of code and the type of error. It’s not perfect. |
>>> str_var = 'red"
File "<stdin>", line 1
str_var = 'red"
^
SyntaxError: EOL while scanning string literal
|
Counters/Enumerators |
Using a variable to keep track of the number of loops performed by a for loop or while loop is a good idea. Some languages (including Python) have some built-in enumeration functionality. Counters are reassigned to themselves after being increased.
In Python the shortcut “x += y” is the same as “x = x +y” |
counter = 0
list_var = [34,54,23,54]
for item in list_var:
print(item, counter)
counter += 1
|
Variables
Variables are used to assign objects to labels or identifiers. They are used to keep track of pieces of data, to organize the flow of the data through the script, and to help programmers read the script.
variable = 1 # a variable assignment
It is recommended (by me) to use descriptive variables that are neither too long nor too short. When variables are too short, they can become confusing to read. When they are too long, they can be confusing to write. Using underscores to separate words in variables is a common practice.
Assigned to vs is equal to (value comparison)
In Python, variables are assigned to an object using the equals sign “=”. This means that there is another way to check if a value is equal to another value: using a double equals sign “==”.
variable = 1 # a variable assignment
variable == 1 # a comparison (that is True)
Iteration
The core of computer programming is iteration
: recursively performing the same action or analysis or function call or whatever your script is built to process. Computers excel at this type of task: they can quickly iterate through a dataset to perform whatever action you deem necessary, on each data item in the set.
For loops
A “for loop” is an iteration implementation that, when presented with a data list, will perform an operation on each member of the list.
In this example, a list of integers are assigned to the variable name data_list
. The list is then used to construct a for loop using the format “for {var} in {iterable}
” where {var} is a variable name that is assigned to each object in the list, one at a time as the loop progresses. One convention is to use “item
” but it can be any valid variable:
data_list = [45,56,34,12,2]
for item in data_list:
print (item * 2)
90
112
68
24
4
While loops
A “while loop” is an iteration implementation that will loop until a specific threshold is met. While loops can be dangerous as they can cause an infinite loop in a script if the threshold is never met.
In this example, the while loop will run (doing nothing but adding 1 to x until it reaches 100, upon which the threshold is met and the while loop will end
x = 0
while x < 100:
x = x + 1 #same as x += 1
Read more about loops here: https://www.geeksforgeeks.org/loops-in-python/
Counter and enumerators
Iteration in for loops or while loops often requires the use of counters (also called enumerators) to track loops in an iteration.
For loops have the option to use the enumerate function by passing the iterator to the function and using a count variable (can be any valid variable name but count is logical) in front of the item variable. The count variable will keep track of the loops, starting at index zero:
>>> data_list = ['a','b','c','d','e']
>>> for count,item in enumerate(data_list):
... print(count, item)
...
0 a
1 b
2 c
3 d
4 e
In Python the shortcut “x += y” is used to increase the value of x while keeping the same variable name, which is the same as “x = x +y”:
>>> x = 0
>>> while x <100:
... x = x + 1
>>> x
100
>>> x = 0
>>> while x <100:
... x += 1
>>> x
100
Conditionals
If statements and Elif statements (short for else if) and Else statements are used to create conditions that will be used to evaluate data objects. If statements can be by themselves (elif
and else
are optional) and is used by declaring the keyword if and then the condition the data must meet:
list_var = [1,’1’,1.0]
for item in list_var:
if type(item) == type(0):
print(‘Integer’)
elif type(item) == type(‘a’):
print(‘String’)
else:
print(‘Float’)
Read more about conditionals here: https://realpython.com/python-conditional-statements/
If vs Else
If statements are usually specific to one condition, while else statements are used as catch-alls to ensure that any data that goes through the if statement will have some way of being dealt with, even if it doesn’t meet the condition of the if statement. Elif statements, which are dependent on the if statement existing and are also condition specific, are not catch-all statements.
List Position (or why programmers count from 0)
Iteration occurs over lists that contain data. Within the list, these data are differentiated by list order or position. Items in a list are retrieved by item index, the (current) position of the data in the list.
Zero-based indexing
In Python, like most computer programming languages, the first item in a list is at index 0, not index 1.
This is a bit confusing to beginners but is a programming standard. It is slightly more computationally efficient to retrieve an item in a list that starts with 0 than a list that starts with 1, and this became the standard in C and its precursors, which meant that Python (written in C) uses zero-based indexing.
Data Types
The data type of a variable determines its behavior. For instance, the character 5 could be an integer type (5) or a float (5.0) or a string (“5”). Each version of 5 will have different available tools, such as the replace
method for strings which can replace characters in the string with other characters.
Key Data Types
Data Type |
Python Data Type Object |
Text data is stored as a String data type |
str |
Numeric data is stored as an Integer or Float or Complex type |
int , float , complex |
Sequence data (lists or arrays) can be stored as a list or tuple. Range is a special generator |
list , tuple , range |
Mapping or key/value pair data types are also known as dictionaries in Python |
dict |
A Set is a data type that contains distinct, immutable objects |
set , frozenset |
Boolean is either True or False, 1 or 0 |
bool |
Binary data types are used to access data files in binary mode. |
bytes , bytearray , memoryview |
To check the data type of a Python variable, use the type()
function:
>>> x = 0
>>> type(x)
<class ‘int’>
Strings
All text data is represented as the String data type in Python. These are known as strings. Common data stored as strings includes names, addresses, or even whole blog posts.
Strings can be also templated in code to allow for “fill-in-the-blank” strings that are not set until the script is run. Strings are technically immutable but can be manipulated using built-in Python string tools and the separate String module.
Key Concepts
Quotation Marks |
Single or double quotation marks can be used to designate a string, as long as it is the same at the beginning and end. Triple quotation marks are used for strings with multiple lines. Quotes within a string can be indicated using the opposite mark as the one opening and closing the string. |
String addition |
Strings can be “added” together to form a larger string. Strings can also be “multiplied” by an integer to repeat the string X times. |
String formatting |
String templates or placeholders can be used in code and filled in at run-time with the data required. |
String manipulation |
Strings can be manipulated using built-in functionality. Characters can be replaced or located. Strings can be split or joined. |
Quotation marks
Strings must be surrounded by quotation marks. In Python, these can be either single or double quotes, but they must be consistent. If a single quote is used to start the string, a single quote must be used to stop it:
>>> string_var = 'the red fox"
File "<stdin>", line 1
string_var = 'the red fox"
^
SyntaxError: EOL while scanning string literal
>>> string_var = 'the red fox'
>>> string_var
'the red fox'
Multiple line strings
Multiple line strings are created by pair three single quotes or double quotes at the beginning of the string, and three at the end.
In this example the variable string_var
is a multiple line string (“\n
” is a Python character representing a new line):
>>> string_var = """the red fox chased the
... dog across the yard"""
>>> string_var
'the red fox chased the\ndog across the yard'
String addition (and more)
Strings can be “added” together to create a new string. This process allows you to build strings from smaller strings, which can be useful for populating new fields composed of other fields in a data file and other tasks.
In this example the string “forest” is assigned to string_var
. Another string is then added to string_var to create a longer string.
>>> string_var = "forest"
>>> string_var += " path" #same as string_var = string_var+ “ path”
>>> string_var
'forest path'
Key Concepts
Format function |
All strings have a built-in function called format that allows the string to have arguments passed. It will accept all data types and format the string from a template. |
String literals |
For Python 3.6+, there is a new tool called string literals, which allow you to insert variables into strings directly. An “f” is placed in front of the string. |
Data type string operators |
An older but still useful tool are the string operators, which are used in strings as placeholders for specific data types (either strings or floats or integers). |
String format function
This method of formatting is the preferred form for Python 3 (it is also available in Python 2.7). It allows you to pass the variables to the format
function (which is built into all strings) and to have them fill up placeholders within the string. Any data type can be passed to the format function.
In this example, the string template is filled with details contained in other variables using the format
string function. The placeholders are filled in the order that the variables are listed, so they must be in correct order. The curly brackets are the placeholders, and the format function will accept arguments and fill in the string:
>>> year = 1980
>>> day = "Monday"
>>> month = "Feb"
>>> template = "It was a cold {} in {} {}"
>>> template.format(day, month, year)
'It was a cold Monday in Feb 1980'
In this example, the placeholders are named, and are passed to keyword arguments in the format function. The arguments are named and do not need to be in order in the format function:
>>> template = 'It was a cold {day} in {month} {year}'
>>> template.format(month=month,year=year,day=day)
'It was a cold Monday in Feb 1980'
In this example, the placeholders are numbered, which makes it much easier to repeat a string:
>>> template = "{0},{0} oh no,{1} gotta go"
>>> template.format("Louie", "Me")
'Louie,Louie oh no,Me gotta go'
String literals
There is a new (as of Python 3.6) method of formatting strings known as formatted string literals. By adding an “f
” before strings, placeholder variables can become populated by variables without using the format function.
In this example, the variables are formatted directly into the string literal, which has an “f
” before the string to indicate that it is a string literal:
>>> year = 1980
>>> day = "Monday"
>>> month = "Feb"
>>> str_lit = f"It was a cold {day} in {month} {year}"
>>> str_lit
'It was a cold Monday in Feb 1980'
Read more about string formatting here: https://realpython.com/python-string-formatting/
String manipulation
String manipulation is common and lots of tools are built into the String data type. These allow you to replace characters in a string or find their index location in the string.
Find and index are similar methods but find is able to be used in conditional statements. If the character is not found in the string, find will return -1, while index will return an error.
The join method is used to join together a list of string data. The split method is the opposite: it splits a string into a list based on a supplied character or the default empty space.
Method |
Example |
join |
string_list = [‘101 N Main St’,’Eureka’,’Illinois 60133’]
address = ‘, ’.join(string_list)
|
replace |
address = ‘101 N Main St’.replace(“St”,”Street”)
|
find, rfind |
str_var = ‘rare’
str_index = str_var.find(‘a’) #index 1
str_index = str_var.find(‘r’) #index 0
str_index = str_var.rfind(‘r’) #index 2
|
upper, lower, title |
name = “Laura”
name_upper = name.upper()
name_lower = name.lower()
name_title = name_lower.title()
|
index, rindex |
str_var = ‘rare’
str_index = str_var.index(‘a’) #index 1
str_index = str_var.index(‘r’) #index 0
str_index = str_var.rindex(‘r’) #index 2
str_var.index(‘t’) #this will cause an error
|
split |
latitude,longitude = “45.123,-95.321”.split(“,”)
address_split = ‘101 N Main St’.split()
|
String indexing
String indexing is similar to list indexing, as explained above. Individual characters, or groups of characters, can be selected from a string by passing the index of the character needed to the string in square brackets.
In this example, the “d
” from “readiness
” is accessed by passing the index [3] to square brackets next to the string:
>>> str_var = "readiness"
>>> d_var = str_var[3]
>>> d_var
'd'
Groups of characters are selected by passing a start and end index, where the end index is the index of the first character you do not want to include:
>>> str_var = "readiness"
>>> din_var = str_var[3:6]. #index 6 is e
>>> din_var
'din'
>>> dine_var = str_var[3:7]. #index 7 is s
>>> dine_var
'dine'
Integers
The Integer data type represents whole numbers. It can be used to perform addition, subtraction, multiplication, and division (with one caveat as noted below).
>>> int_var = 50
>>> int_var * 5
250
>>> int_var / 5
10.0
>>> int_var ** 2
2500
Convert a string to an integer
To convert a string (or a float) to an integer, use the int
function:
>>> x = '0'
>>> y = int(x)
>>> y
0
>>> type(y)
<type 'int'>
>>> type(x)
<type 'str'>
Integer math issue in Python 2
A well-known and well-intentioned design issue in Python 2 is the integer division issue. It means that performing division math with integers will result in a (usually) unwanted result where no remainder is returned. It is encouraged to convert integers into floats before dividing.
Here is an example of the issue:
Python 2.7.16 (default, Dec 21 2020, 23:00:36)
>>> 5/3
1
This issue has been fixed in Python 3:
Python 3.8.2 (default, Apr 8 2021, 23:19:18)
>>> 5/3
1.6666666666666667
Read more about integers in Python here: https://realpython.com/python-numbers/
Floating Numbers
Floating point numbers in Python are used to represent real numbers as 64-bit double-precision values. Sometimes using binary systems to represent decimal based numbers can be a bit odd, so keep an eye out, but in general these will work as expected.
>>> x = 5.0
>>> x * 5
25.0
>>> x ** 5
3125.0
>>> x/2.3
2.173913043478261
Conversion between data types
Conversion between data types is possible in Python using built-in functions that are part of the standard library. To start, the type function is useful to find the data type of an object. Once identified, the data object can be converted from Integer (int
function) to String (str
function) to Float (float
function), as long as the character would be valid in that data type.
In these examples, a character is converted from String to Integer to Float to String using the int
and str
and float
functions:
>>> str_var = "5"
>>> int_var = int(str_var)
>>> int_var
5
>>> float_var = float(int_var)
>>> float_var
5.0
>>> str_var = str(float_var)
>>> type(str_var)
'<class 'str'>'
Data Structures or Containers
Data structures, also called data containers and data collections, are special data types that can hold, in a retrievable order, any data item of any data types (including other data containers). Data containers are used to organized data items by index in tuples or lists, or by key:value pair in dictonaries.
lData retrieval from data containers
To get data out of data containers, square brackets are used to pass either indexes (lists and tuples) or keys (dictionaries). If there is more than one level of data container (i.e. one container contains another), first the data container inside is referenced using an index or key inside a first square bracket, and then the data inside the container is accessed using a second.
Data Container |
Example |
Tuple |
tuple_var = (“blue”, 32,[5,7,2],’plod’,{‘name’:’magnus’})
plod_var = tuple_var[-2]
magnus_var = tuple_var[-1][‘name’]
|
List |
list_var = [‘fast’,’times’,89,4.5,(3,8),{‘we’:’believe’}]
times_var = list_var[1]
|
Dictionary |
dict_var = list_var[-1]
believe_var = list_var[-1][‘we’]
|
Tuples
Tuples are ordered lists that can hold any data type, even in the same tuple. They are immutable, meaning they cannot be altered, and data cannot be added to or removed from the tuple once it has been created. They have length and the built-in len
function can be used to get the length of the tuple.
In Python they are declared by using round brackets () or the tuple
function. Data is accessed using zero-based indexing by passing the index to square brackets next to the tuple.
In this example, a tuple is assigned to the variable name tuple_var
, and data is accessed using indexing:
>>> tuple_var = ("red",45,"left")
>>> type(tuple_var)
<class 'tuple'>
>>> ("red",45,"left")[0]
'red'
>>> tuple_var[0]
'red'
Read more about tuples in Python here: https://www.geeksforgeeks.org/python-tuples/
Lists
Lists (often called Arrays in other programming languages) are data containers that can hold any other type of data type, even in the same list, meaning they do not have to be only one data type. Lists can be altered after they are created. In Python they are declared by using square brackets [] or the list
function. Data is accessed using zero-based indexing by passing the index to square brackets next to the list.
In this example, a list is assigned to the variable name list_var
, and data is accessed using indexing:
>>> list_var = ["blue",42,"right"]
>>> type(list_var)
<class 'list'>
>>> ["blue",42,"right"][0]
'blue'
>>> list_var[0]
'blue'
Read more about lists in Python here: https://www.geeksforgeeks.org/python-list/
Convert between lists and tuples
Lists can be copied into a new tuple object using the tuple
function. Conversely, Tuples can be copied into a list data type using the list
function. Technically this does not convert the original data item, but instead creates a copy of the data item in the new data type.
In this example, the list is copied into a tuple data type, and then the tuple is copied into a list data type. Note that the brackets change with each new data type created:
>>> tuple_copy = tuple(list_var)
>>> tuple_copy
('blue', 42, 'right', 'ankle')
>>> list_copy = list(tuple_copy)
>>> list_copy
['blue', 42, 'right', 'ankle']
List operations for both tuples and lists
Lists and tuples can be iterated using for loops. They can both be “sliced” as well, creating a subset of the list or tuple that will be operated on for the for loop or other operation.
Slicing
Slicing a list or tuple will create a new list or tuple. The slice is created by passing indexes to the list or tuple in square brackets, separated by a colon. The first index is the start index, and it can be ignored if it is index 0 (i.e. the beginning of the original list). The second index is the index of the first value that you do NOT want to include (it can be blank if it the rest of the original list).
In this example we see a tuple with three data items sliced to only include the first two items. The string “left” is at index 2 in the tuple, meaning that the last index in the slice will be 2. The slice is assigned to variable name tuple_slice:
>>> tuple_var = ("red",45,"left")
>>> tuple_slice = tuple_var[:2]
>>> tuple_slice
('red', 45)
In this example we see a list with four data items sliced to only include the last two items. The first index is the index of the first data item we want (the string “right”
). The last index is blank:
>>> list_var = ["blue",42,"right","ankle"]
>>> list_slice = list_var[2:]
>>> list_slice
['right', 'ankle']
List operations for only lists
A list can be appended (one data item added) or extended (a list or tuple of data items are all added to the main list). The list order can be reversed or sorted. Built-in functions allow for the calculation of the maximum or minimum value of a list or even the sum of a list (given the data type of the items in the list is correct).
Sets
Sets represent a collection of distinct objects. In Python, sets are unordered, no duplicates are allowed, and all data items inside a set must be immutable.
Set operations
Sets are especially useful for getting all distinct members of a list. They cannot be accessed using indexing (they are unordered) but they can be iterated:
>>> orig_list = ["blue","pink","yellow","red","blue","yellow" ]
>>> set_var = set(orig_list)
>>> set_var
{'pink', 'yellow', 'blue', 'red'}
>>> set_var[0]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'set' object is not subscriptable
>>> for item in set_var:
... print(item)
...
pink
yellow
blue
red
Dictionaries
Dictionaries are key:value stores, mean they are data containers that use unordered key and value pairs to organize data. Keys are used as reference points for organization and retrieval. When a key is supplied to a dictionary in square brackets, the value is returned.
>>> dict_var = {"key":"value"}
>>> dict_var['key']
'value'
>>> dict_var = {"address":"123 Main St", "color":"blue"}
>>> dict_var["address"]
'123 Main St'
>>> dict_var["color"]
'blue'
Read more about dictionaries in Python here: https://www.geeksforgeeks.org/python-dictionary/
Keys and values
Keys can be any immutable data type (meaning lists cannot be used as keys, but strings and integers and floats and tuples can be used as keys. Values can be any type of data, including other dictionaries.
All keys in a dictionary can be accessed as a list using the dictionary keys
function. In Python 2.x this is a list. In Python 3.x it is a generator.
All values in a dictionary can be accessed as a list using the dictionary values function. In Python 2.x this is a list. In Python 3.x it is a generator.
Functions
Functions are sub routines defined by code. When “called” or run, functions will do something (or nothing if written that way). Functions often accept parameters, and these can be required or optional.
Functions make it easy to perform the same action over and over without writing the same code over and over. This makes code cleaner, shorter and smarter. They are a good idea and should be used often.
Read more about functions here: https://realpython.com/defining-your-own-python-function/
Def keyword
Functions are defined using the “def
” keyword, which is short for “define function”. The keyword is written, and then the name of the function and round brackets (), into which expected parameters can be defined.
Return statement
Functions allow for data to be returned from the subroutine to the main loop using return
statements. These allow the user to calculate a value or perform some action in the function, and then return back a value to the main loop.
Parameters
Parameters or arguments are values expected by functions and supplied by the code at runtime.
Namespaces
In Python, there is a concept called namespaces. These are refined into two types of namespaces: global and local.
All variables defined in the main part of a script (outside of any functions) are considered to be in the global namespace. Within the function, variables have a different “namespace”, meaning that variables inside a function are in a local
namespace
and are not the same as variables in the main script, which are in the global
namespace
. If a variable name inside a function is the same as one outside of the function, changing values inside the function (in the local namespace) will not affect the variable outside the function (in the global namespace)
Function Examples
In this example, a function is defined and written to return “hello world” every time it is called. There are no parameters, but the return
keyword is used:
def new_function():
return "hello world"
In this example, an expected parameter is defined in the brackets. When called, this value is supplied and the function then returns the value from the local namespace back to the global namespace in the main loop:
def accept_param(value):
return value
In this example an expected parameter has a default value assigned, meaning it only has to be supplied if the function uses a non-default parameter:
def accept_param(value=12):
return value
Doc strings
Functions allow for a string after the definition line that is used to declare the purpose of the function for documentation purposes.
def accept_param(value=12):
'this function accepts a parameter if different from default'
return value
Classes
Classes are special blocks of code that organize multiple variables and functions into an object with its own methods and functions. Classes make it easy to create code tools that can reference the same internal data lists and functions. The internal functions and variables are able to communicate across the class, so that variables defined in one part of the class are available in another.
Classes use the idea of “self” to allow for the different parts of the class to communicate. By introducing self as a parameter into each function inside a class, the data can be called.
Classes are called or “instantiated” to create a class object. This means the class definition is kind of like a factory for that class, and when you want one of those class objects, you call the class type and pass the correct parameters if required.
class Object():
def __init__(self, name):
'accepts a string'
self.name = name
def get_name(self):
'return the name'
return self.get_name
Read more about classes here: https://www.geeksforgeeks.org/python-classes-and-objects/