Python
Error types in Python
Two common errors that we encounter while writing Python are SyntaxError and NameError.
Syntax Error
It means there is something wrong with the way your program is written. For example, punctuation that does not belong, a command where it is not expected, or a missing parenthesis can all trigger a SyntaxError.
Name Error
A NameError occurs when the Python interpreter sees a word it does not recognize. Code that contains something that looks like a variable but was never defined will throw a NameError.
Loops
Programming languages like Python implement two types of iteration:
Indefinite iteration, where the number of times the loop is executed depends on how many times a condition is met.
Definite iteration, where the number of times the loop will be executed is defined in advance (usually based on the collection size).
The general structure of a loop is like below:
for <temporary variable> in <collection>:
<action>
#example
for elements in list_name:
print(element)Let’s break down each of these components:
A
forkeyword indicates the start of aforloop.A
<temporary variable>that is used to represent the value of the element in the collection the loop is currently on.An
inkeyword separates the temporary variable from the collection used for iteration.A
<collection>to loop over. In our examples, we will be using a list.An
<action>to do anything on each iteration of the loop.
Temporary Variables:
A temporary variable’s name is arbitrary and does not need to be defined beforehand. Both of the following code snippets do the exact same thing as our above example:
Programming best practices suggest we make our temporary variables as descriptive as possible. Since each iteration (step) of our loop is accessing an ingredient it makes more sense to call our temporary variable element rather than i or item.
Often we won’t be iterating through a specific list (or any collection), but rather only want to perform a certain action multiple times. In this case, we would follow this structure:
for a 2D nested loop:
Continue and Break
List Comprehension
In python, one elegant way to write loops is using List Comprehensions:
Strings
Strings are Immutable.
There’s an even easier way than iterating through the entire string to determine if a character is in a string. We can do this type of check more efficiently using in. in checks if one string is part of another string.
Here is what the syntax of in looks like:
A string is a list of characters.
A character can be selected from a string using its index
string_name[index]. These indices start at 0.A ‘slice’ can be selected from a string. These can be between two indices or can be open-ended, selecting all of the string from a point.
Strings can be concatenated to make larger strings.
len()can be used to determine the number of characters in a string.Strings can be iterated through using
forloops.Iterating through strings opens up a huge potential for applications, especially when combined with conditional statements.
String Methods

Last updated