Programming in Python (Best Tutorial 2019)

Programming in Python

What is Programming in Python

This tutorial explains Python Programming with the best examples. Python is the best open-source and easiest to learn programming languages in the world. It runs on all the major operating systems and computers and is used in everything from building web servers to creating desktop applications. 

 

Keywords

The Python language has a list of words with special meaning. These are called keywords. for is a keyword we’ve already seen that is used to execute code multiple times. We will learn more keywords throughout this blog.

 

Spacing

Let’s take another look at our program that prints Hello, World! a hundred times:

# calthoff/tstp

for i in range ( 100 ):

print ( "Hello, World!" )

As I noted earlier, the print is indented by four spaces. We will cover why shortly, but it has to do with letting Python know when blocks of code begin and end.

 

In the meantime, please be aware that whenever you see an indent in an example it is an indent of four spaces. Without proper spacing, your program will not work.

 

Spacing is not used like this in other programming languages. Other approaches include using keywords or brackets instead of spacing.

 

Python proponents believe the required use of proper spacing makes Python less tedious to read and write than other languages, but the best approach to this problem is frequently debated amongst programmers.

 

Data Types

Different kinds of data in Python are grouped into different categories or data types. In Python, each data value, like 2 or "Hello, World!" , is called an object. We will learn more about objects in Part II, but for now think of an object as a data value in Python with three properties: an identity, a data type and a value.

 

The identity of an object is where it is stored in memory, which never changes (and which we will be ignored for now). The data type of an object is the category of data the object belongs to and determines the properties the object has. This also never changes.

The value of an object is the data it represents—the number 2, for example, has a value of 2.

 

"Hello, World!" is an object with a data type called str, short for string, and the value "Hello, World!" . When we refer to an object with an str data type, we call it a string.

 

A string is a sequence of one or more characters surrounded by quotes. You can use single quotes or double quotes, but the quotes at the beginning and end of a given string must match:

“Hello, World!”

>> Hello, World!

‘Hello, World!’

>> Hello, World!

A character can be any symbol found in a Unicode character table like the interactive one found at Unicode® character table. Strings are used to represent text, and they have unique properties.

 

The numbers we used to do math in the previous section are also objects—but they are not strings, they have a different data type. Whole numbers have the data type int, short for integer.

 

Numbers like 2, 3, 4 and 10 all have the data type int. Another way of saying this is they are all integers. Like strings, integers have their own properties. For example, you can divide two integers, but you cannot divide two strings.

 

Although whole numbers are integers, fractional numbers (numbers with a decimal point) have a different data type called float. 2.1, 8.2 and 9.9999 are all examples of objects with the float data type.

 

They are called floats. Like all data types, floats have their own properties and behave in a certain way. Floats behave similarly to integers:

2.2 + 2.2

>> 4.4

 

But, there are some important differences between floats and integers you will learn about later. Objects with a bool data type have a value of either True or False and are called booleans.

 

Objects with a data type NoneType always have the value None. Objects with a NoneType data type is used to represent the absence of a value.

 

Comments

A comment is a line (or part of a line) of code written in English (or another language) preceded by a special symbol that tells the programming language you are using to ignore that line (or part of a line) of code. In Python, the pound symbol is used to create comments.

 

A comment explains what a line of code does. Programmers use comments to clarify why they did something to make the line of code easier to understand for whoever reads it.

 

You can write whatever you want in a comment, as long as it is only one line long. Here is an example of a comment taking up an entire line of code, followed by an example of a comment taking up part of a line of code:

# This is a comment

print ( "The comment does not affect this code" )

>> The comment does not affect this code

print ( "The comment does not affect this code" ) # This is a comment >> The comment does not affect this code

 

You should only write a comment if you are doing something unusual in your code, or to explain something that is not obvious in the code itself. Use comments sparingly —do not comment on every line of code you write—save them for special situations. Here is an example of an unnecessary comment:

print ( "Hello, World!" ) # this line of code prints Hello, World!

It is unnecessary because it is very clear what this line of code does. Here is an example of a good comment:

d = math.sqrt(l**2 + w**2) # length of the diagonal

 

Even if you understood exactly how this code works, you still might not know how to calculate the length of a diagonal of a rectangle, which is why this comment is useful.

 

Printing

We are not limited to printing “Hello, World!” in our programs. We can print whatever we’d like as long as we surround it with quotes:

print ( "Python" )

>> Python

print ( "Hola!" )

>> Hola!

Lines

Python programs are divided into lines of code. Take a look at this program:

# line1

# line2

# line3

 

There are three lines of code. It is useful to refer to each piece of code by the line it is on. In IDLE you can go to “Edit” and select “Go to line” to jump to a specific line in your program.

 

Sometimes a piece of code is long and takes up more than one line. When this happens, it is ok for the code to extend to the next line (when extended according to Python's rules) :

 

Error and Exceptions

If you write a Python program and disregard Python’s syntax you will get one or more errors when you run your program.

 

When this happens, the Python shell will inform you your code did not work and gives you information about the error that occurred. Look at what happens if you try to define a string in Python with a quote on only one side:

my_string = “Hello World.

>> File "/Users/coryalthoff/PycharmProjects/se.py", line 1 my_string = 'd

^

 

SyntaxError: EOL while scanning string literal

This is called a syntax error. Syntax errors are fatal. A program cannot run with a syntax error. When you run a program with a syntax error, Python lets you know about it in the shell.

 

The message will tell you what file the error was in, what line it occurred on and what kind of error it was. Although this error may look intimidating, errors like this happen all the time.

 

When there is an error in your code, you should go to the line number the problem occurred on, and try to figure out what you did wrong. In this example, you would go to line 1 of your code.

 

After staring at it for a while, you would eventually notice there is only one quote. To fix it, add a quote at the end of the string and rerun the program. From this point forward, I will represent the output of an error like this:

 

>> SyntaxError: EOL while scanning string literal

 

For easier reading, I will only show the last line of the error.

Python has two kinds of errors: syntax errors and exceptions. Any error that is not a syntax error is an exception. A ZeroDivisionError is an example of an exception that occurs if you try dividing by zero.

 

The difference between a syntax error and an exception is exceptions are not necessarily fatal (there is a way to make a program run even if there is an exception which you will learn about in the next blog). When an exception occurs, Python programmers say “ Python (or your program) raised an exception”. Here is an example of an exception:

10 / 0

>> ZeroDivisionError: integer division or modulo by zero If you incorrectly indent your code, you get an IndentationError :

y = 2

x = 1

 

>> IndentationError: unexpected indent

As you are learning to program, you will frequently get syntax errors and exceptions, but they will decrease over time. Remember, when you run into a syntax error or exception, go to the line where the problem occurred and stare at it until you figure out the solution (or search the internet for the error or exception if you are stumped).

 

Functions

A function is a compound statement that can take input, execute instructions, and optionally return an output. Calling a function means giving the function the input it needs too it can execute its instructions and optionally return an output.

 

Functions in Python are similar to functions in math. If you don’t remember functions from algebra, here is an example:

 

# the following is algebra, not Python code f(x) = x * 2

The left half of the equation defines a function f —that takes one parameter— x. A parameter is data passed you pass into a function when its called. A function can have one parameter, multiple parameters or no parameters. A function’s parameters are its input.

 

The right half of the equation is the definition of the function (the instructions it executes when called). The right-hand side of the equation user is able to use the parameter that was passed in ( x ) to make a calculation and return the result (the output). In this case, our function takes a parameter ( x);, multiplies it by 2; and returns the result.

 

In both Python and algebra, you call a function with the following syntax: [function_name]([parameters_seperated_by_commas] ) . You call a function by putting parentheses after the function name.

 

TYou put the parameters go inside the parenthesis with each parameter separated by a comma. If we call the function we just defined above (?) and pass in the parameter 4, we get the following result:

# the following is algebra, not Python code f(4)

>> 8

If we pass in 10 as a parameter we get 20:

f(10)

>> 20

In our first example, we called our function with 4 as a parameter ( x ). Our function executed its code, x * 2, and returned the result — 8. In our second example, we called our function with 10 as a parameter ( x ). Our function executed its code, x * 2, and returned the result — 20.

 

Representing Concepts

This is abrupt, maybe something like “As you may have figured out by now…”We are not limited to printing “Hello, World!” in our programs. We can print whatever we’d like:

print(“Python!”)

>> Python!

 

From here on out, I will use the following convention to explain concepts like the idea that you can print anything in Python: print(“[what_you_want_to_print]”). The brackets and the text inside of them represent that you need to be replaced by a piece of code substitute a piece of code in place of them.

 

When you are trying to follow an example written in this format like this, do not type the brackets. The words inside of the brackets are a hint for the code you need to replace the brackets with. Everything outside of the not in brackets represents actual code that you should type. This format is a way of expressing that you can type

print(“[what_you_want_to_print]”) into Python, replace [what_you_want_to_print] with

whatever you want to print, and Python will print it:

print(“I do not like green eggs and ham.”)

print(“The Cat in the Hat”)

>> I do not like green eggs and ham

>> The Cat in the Hat

Programming is full of conventions: agreed upon ways of doing things. This format is an example of a convention that is used in the programming world and will be used throughout the blog.

 

Defining Functions

To create a function in Python we choose a function name, define its parameters, define what the function will do, and we can choose to optionally return value (if we don’t return a value the function will return None ). We use the following syntax to define a function:

def [function_name]([parameters_seperated_by_commas]):

[function_definition]

Our mathematical function f(x) = x * 2 looks like this in Python:

# calthoff/tstp

def f(x):

return x * 2

>> 

The keyword def tells Python you are about to define a function. is a keyword used to define a function? When you use it, Python knows you are about to define one. After def, you can name your function anything whatever you’d like.

 

By convention, you should never use capital letters in your function name, and if there are two words in your function name you should separate them with an underscore—like_this. Once you’ve named your function, put parentheses after it.

 

Inside the parenthesis, you put your parameter(s). In this (the previous?) example, our function only has one parameter ( x ), but if you want your function to accept more than one parameter, you must separate each parameter inside the parenthesis with a comma. After the parenthesis, you put a colon and indent by four spaces (like any other compound statement).

 

Any code indented four spaces after the colon is the function’s definition. In this case, our function’s definition is only one line— return x * 2 . return is another keyword. It is used to define the value a function outputs when you call it, referred to as the value the function returns.

To call a function in Python, we use the syntax we learned earlier: [function_name]().

Here is an example of calling our function with 2 as a parameter:

f(2)

>> 

You will notice the console didn’t print anything. Our function worked, but it just didn’t print the result because we didn’t tell Python too. If we want to print the value our function returned, we can save our function’s output in a variable:

# calthoff/tstp result = f(2)

print(result)

>> 4

 

You can save the result your function returns in a variable whenever you need to use the value later in your program. Functions are not only just used to return values.

 

Returning a value is optional, as is including a return statement in your function. Aside from returning values, functions also encapsulate functionality you want to reuse. For example:

#add GitHub

def even_odd(x):

if x % 2 == 0:

print(“even”)

else:

print(“odd”)

even_odd(2)

even_odd(3)

>> even

>> odd

We didn’t define a value for our function to return, but our function is still useful. ItOur function tests if x % 2 == 0 and prints whether x is even or odd depending on the result.

 

Remember, modulo returns the remainder when you divide two numbers. If there is no remainder when you divide a number by 2 (modulo returns 0 ) the number is by definition even.

 

If there is a remainder, the number is odd. You may need to use this functionality in several different places in your program. It would be poor programming to type the code we used in our function’s definition every time you want to use this functionality.

 

That’s what functions are for. You put functionality in a function, and it lets you easily reuse that functionality throughout your program, without having to do any extra work. This is a little confusing - are you going to explain later on? 

 

Exception Handling

When you rely on user input from the input function, you do not control the input to your program—the user does, and that input could cause an error. For example, say we write a program to collect two numbers from a user and print out the result of the first number divided by the second number:

a = input(“type a number”)

b = input(“type another number”)

a = int(a)

b = int(b)

print(a / b)

>> type a number

>> 10

>> type another number

>> 5

>> 2

Our program appears to work. However, we will run into a problem if the user inputs 0 as the second number:

a = input(“type a number”)

b = input(“type another number”)

a = int(a)

b = int(b)

print(a / b)

>> type a number

>> 10

>> type another number

>> 0

>> ZeroDivisionError: integer division or modulo by zero

Our program works—until the user decides to enter 0 as the second number, in which case our program raises an exception. We cannot allow people to use this program and hope they will not enter 0 as the second number.

 

One way to solve this is to use exception handling, which allows you to “catch” exceptions if they occur and decide what to do.

 

The keywords try and except are used for exception handling. We can change our program to use exception handling so if a user enters 0 as the second number, our program prints a message telling them not to enter 0 instead of raising an exception.

 

In Python, exceptions are objects—which allows us to use to them in our programs. Each exception in Python is an object. You can see the full list of built-in exceptions here: Built-in Exceptions.

 

Whenever you are in a situation where you think your code may raise an exception, you can use a compound statement with the keywords try and expect to catch the exception.

 

The try clause defines the error that could occur. The except clause defines code that will only execute if the exception defined in your try clause occurs. Here is an example of how we can use exception handling in our program so if a user enters 0 as the second number our program doesn’t break:

a = input(“type a number”)

b = input(“type another number”)

try:

print(a / b)

except ZeroDivisionError:

print(“b cannot be zero. Try again.”)

>> type a number

>> 10

>> type another number

>> 0

>> “b cannot be zero. Try again.”

If the user enters anything other than 0, the code in our try block is executed and our except block doesn’t do anything. But if the user enters 0, instead of raising an exception, the code in our except block is executed and our program prints “b cannot be zero. Try again.”.

 

Docstrings

Docstrings are comments at the top of a function or method that explain what the function or method does, and documents what types of parameters should be passed to it. Here is an example:

def add (x , y):

"""

Returns x + y.

:param x: int first integer to be added.

:param y: int second integer to be added.

:return : int sum of x and y.

"""

return x + y

The first line of the docstring clearly explains what our function does. When other developers reuse your function or method, they do not want to have to read through all of your code to figure out what it does.

 

The rest of the lines of the docstring lists the function’s parameters, its return value, and some additional information, including the type for all of the parameters and the return value.

 

Docstrings will help you program faster because if you forget what a piece of code does, you can quickly figure it out by reading the docstring instead of all of the code in a function, class or method. It will also make it much easier for other programmers to use your code.

 

In some cases, I’ve omitted docstrings I normally would have included them to make my code as concise as possible for easy reading—but whenever I am writing code for production (code that is actually used by other people)— I use doc strings.

 

Lists

A list is a mutable container that stores objects in a specific order. When a container is mutable it means the objects in the container can change—objects can be added and removed from the container.

 

[image]

Lists are represented in Python with brackets. There are two syntaxes to create a list. We can create an empty list with the list function:

new_list = list()

new_list

>> []

Or we can create an empty list with brackets:

new_list = []

new_list

>> []

 

Both syntaxes create a new empty list. When you create a new list with the list function you can also pass in objects you want to add to your list as parameters:

my_list = list (“Apple”, “Orange”, “Pear”)

my_list

>> ['Apple', 'Orange', 'Pear'] Or like this using the second syntax:

my_list = [“Apple”, “Orange”, “Pear”]

my_list

>> ['Apple', 'Orange', 'Pear']

Each object in a list is called an item in the list. In this example, there are three items in our list: ‘Apple’ , ‘Orange’ and ‘Pear’ . Lists keep their items in order—the order the items entered the list.

 

Unless we change the order of our list, ‘Apple’ will always be the first item, ‘Orange’ the second item and ‘Pear’ the third item. ‘ Apple’ is at the beginning of the list, and ‘Pear’ is at the end. We can add a new item to the end of our list using the append function:

my_list.append(“Banana”)

my_list.append(“Peach”)

my_list

>> ['Apple', 'Orange', 'Pear', ‘Banana’,’Peach’]

Lists are not limited to storing strings—they can store any data type:

new_list = []

new_list.append(True)

new_list.append(100)

new_list.append(1.1)

new_list.append(‘Hello’)

>> [True, 100, 1.1, ‘Hello’]

Every item in a list has a position in the list—called its index. You can figure out the index of an item in a list by starting at the beginning of the list and counting.

 

The only tricky part is you have to start counting at zero because the first item in a list has an index of zero. So the first item in a list is at index zero, the second item in a list is index one, and so on.

 

Counting starting at zero takes some getting used to, so don’t worry if it frustrates you at first. You can access each item in a list with its index using the syntax [list_name][[index]] .

 

I put the index in two brackets to represent that [index] should be replaced but should be inside brackets.

my_list = [“Apple”, “Orange”, “Pear”]

my_list[0]

my_list[1]

my_list[2]

>> Apple

>> Orange

>> Pear

You can change an item in a list by setting the index of the item to a new object:

color_list = [“blue”, “green”, “yellow”]

color_list

color_list[2] = “red”

color_list

>> [“blue”, “green”, “yellow”]

>> [“blue”, “green”, “red”]

If you try to access an index that doesn’t exist, Python will raise an exception:

color_list = [“blue”, “green”, “yellow”]

color_list[4]

>> IndexError: list index out of range

You can remove the last item from a list with pop :

color_list = [“blue”, “green”, “yellow”]

color_list

item = color_list.pop()

item

color_list

>> [“blue”, “green”, “yellow”]

>> “yellow”

>> [“blue”, “green”]

You cannot pop from an empty list, if you do Python will raise an exception.

You can check if an item is in a list using the keyword in :

color_list = [“blue”, “green”, “yellow”]

“green” in color_list

>> True

Add the keyword not to check if an item is not in a list:

color_list = [“blue”, “green”, “yellow”]

“black” not in color_list

>> True

You can get the size of a list (the number of items in it) with the len function:

len(color_list)

>> 3

Finally, you can get a range of items in a list with slicing. We slice a list with a start index and an end index separated by a colon in brackets outside of our list. Slicing returns a new list (a “slice” of the old one) made up of everything between the start and end index.

 

The syntax for slicing is [list_name][[start_index:end_index]]. Here is an example of slicing a list:

new_list = ['Apple', 'Orange', 'Pear', ‘Banana’, ‘Peach’] new_list[0:3]

>> ['Apple', 'Orange', 'Pear']

 

A “gotcha” with slicing is the start index includes the item at that index, but the end index doesn’t include the item at the end index, it only includes the item before the end index.

 

This means if you want to slice from “Apple” to “Pear”, you need to slice from index 0, to index 3 (instead of index 2), because the item at the end index is not included in the slice.

 

Tuples

A tuple is an immutable container that stores objects in a specific order. When a container is immutable it means the contents of the container cannot change. That means unlike a list, once you put an object into a tuple, you can no longer change it.

 

Once you create a tuple you cannot change the value of any of the items in it, you cannot add new items to it, and you cannot remove items from it. Tuples are represented with parenthesis. There are two syntaxes to create a tuple:

my _tuple = tuple()

my_tuple

>> ()

And

my_tuple = ()

my_tuple

>> ()

 

If you want your tuple to contain objects, you must add them to your tuple when you create it.

Here is how you add items to a tuple using the first syntax:

my_tuple = tuple(“brown”, “orange”, “yellow”)

my_tuple

>> (“brown”, “orange”, “yellow”) And the second:

my_tuple = (“brown”, “orange”, “yellow”)

my_tuple

>> (“brown”, “orange”, “yellow”)

A tuple with one item in it still needs a comma after the item:

(‘self_taught’,)

>> (‘self_taught’,)

 

Once you’ve created your tuple, if you try to add an object to it, Python will raise an exception:

my_tuple = (“brown”, “orange”, “yellow”)

my_tuple[1] = “red”

>> TypeError: 'tuple' object does not support item assignment

You can, however, access data from a tuple like a list—you can reference an index and slice a tuple:

my_tuple = (“brown”, “orange”, “yellow”)

my_tuple[0]

my_tuple[1:2]

>> yellow

>> ('yellow', 'orange')

 

You can check if an item is in a tuple using the keyword in:

my_tuple = (“brown”, “orange”, “yellow”)

“brown” in my_tuple

>> True

Add the keyword not to check if an item is not in a tuple:

my_tuple = (“brown”, “orange”, “yellow”)

“black” not in my_tuple

>> True

You may be wondering why you would want to use a data structure that appears to be like a list, but less helpful. Tuples are useful when you are dealing with values you know will never change, and you don’t want other parts of your program to have the ability to change those values.

 

A good example is if you are working with geographic coordinates. You may want to store the longitude and latitude of New York in a tuple because you know the longitude and latitude of New York is never going to change, and you want to make sure other parts of your program don’t have the ability to accidentally change them.

 

Dictionaries

Dictionaries are another built-in container for storing objects. They are mutable—but unlike lists and tuples—they do not store objects in a specific order.

 

Instead, dictionaries are used to map one object (called the key) to another object (called the value). Dictionaries are represented with curly brackets. There are two syntaxes for creating dictionaries:

my_dict = dict()

my_dict

>> {}

And:

my_dict = {}

my_dict

>> {}

 

You add objects to a dictionary by mapping a key to a value. Each key mapped to a value in a dictionary is called a key-value pair. Here is how you create key-value pairs when you create a dictionary with the first syntax:

my_dict = dict({“Apple”: “Red”, “Banana”: “Yellow”})

my_dict

>> {'Apple': 'Red', 'Banana': 'Yellow'} And the second:

my_dict = {“Apple”: “Red”, “Banana”: “Yellow”} my_dict

>> {'Apple': 'Red', 'Banana': 'Yellow'}

 

Both syntaxes have a key separated from a value by a colon. Each key-value pair must be separated by a comma. Unlike a tuple, if you have just one key value pair, you do not need a comma after it.

 

Once you’ve added key-value pairs to a dictionary, you can use a key to lookup a value. You can only use a key to lookup a value. You cannot use value to lookup a key:

my_dict[‘Apple’]

>> Red

Dictionaries are mutable, so once you’ve created one you can add more key-value pairs with the syntax [my_dictionary][[key]]=[value] :

my_dictionary = dict()

my_dictionary[“programming”] = “awesome” my_dictionary[“programming”]

my_dictionary[“Bill Gates”] = “rich”

my_dictionary[“Bill Gates”]

my_dictionary[“america_founded”] = 1776

my_dictionary[“america_founded”]

>> awesome

>> rich

>> 1776

You can use the in a keyword to check if a key is in a dictionary. You cannot use the in a keyword to check if a value is in a dictionary.

“Bill Gates” in my_dictionary

>> True

Add the keyword not to check if a key is not in a dictionary:

“Bill Plates” not in my_dictionary

>> True

Finally, you can delete a key-value pair from a dictionary with the keyword del

my_dictionary

del my_dictionary[ 'Bill Gates' ]

my_dictionary

>> {'america_founded': 1776, 'programming': 'awesome', 'Bill Gates': 'Rich'}

>> {'america_founded': 1776, 'programming': 'awesome'}

 

Triple Strings

If a string is more than one line, you need to put it in triple quotes:

“““line one

line two

line three

”””

If you try to define a string that spans more than one line with single or double quotes, you will get a syntax error.

 

Index es

Strings are iterable. You can access each character in a string with its index, just like you can access each item in a tuple. Like tuples, the first character in a string starts with index 0 and each subsequent index is incremented by 1.

my_string = “LAX”

my_string[0]

my_string[1]

my_string[2]

>> ‘L’

>> ‘A’

>> ‘X’

 

In this example, we used the indexes 0, 1, and 2 to access each of the characters in the string “LAX”. If we try to access an element past the last element, Python raises an exception:

my_string = “LAX”

my_string[3]

>> IndexError: string index out of range

 

Strings are Immutable

Strings, like tuples, are immutable. You cannot change characters in a string. If you want to change the characters in a string, you need to create a new string.

Sometimes you will want to create a string using variables. This is done with the format method:

year_started = “1989”

“Python was created in {}.”.format(year_started)

>> ‘Python was created in 1989.’

 

The format function looks for any occurrences of {} in the string and replaces them with the values you pass into the format.

You are not limited to using {} once, you can put as many of them in your string as you’d like:

# calthoff/tstp year_started = “1989”

creator = “Guido van Rossum”

country = “the Netherlands”

my_string = “Python was created in {} by {} in {}.”.format(year_started, creator, country)

print(my_string)

>> ‘Python was created in 1989 by Guido van Rossum in the Netherlands.’

 

split

Strings have a method called split used to separate one string into two strings. You pass the split method the character or characters you want to use to separate the string—for example, we can pass in a period to separate this quote by Daniel Coyle into two different strings:

 

””” Practice doesn’t make perfect. Practice makes myelin, and myelin makes perfect.”””.split(“.”)

>> ["Practice doesn't make perfect", ' Practice makes myelin, and myelin makes perfect', '']

The result is a list with two different strings split at the period in the original string.

 

join

The join method lets you add new characters in between every character in a string:

my_string = ‘abc’

join_result = ‘+’.join(my_string)

join_result

>> ‘a+b+c’

You can turn a list of strings into a single string by calling the join method on an empty string ( “” ) and passing in a list:

the_Fox = [“The”, “fox”, “jumped”, “over”, “the”, “fence”, “.”]

one_string = “”.join(the_Fox)

one_string

>> The fox jumped over the fence.

 

replace

The replace method lets you replace every occurrence of a character(s) with another character(s). The first parameter is the character(s) to replace and the second parameter is the character(s) to replace it with.

my_string = “The cat jumped over the hat.”

my_string = my_string.replace(“a”, “@”)

my_string

>> “The c@t jumped over the h@t.”

 

index

We can get the index of the first occurrence of a character in a string with the index method. We pass in the character we are looking for, and we get the index of the first occurrence of the character in the string:

‘cat’.index(‘a’)

>> 1

The in keyword checks if one string is in another string and returns True or False :

“Playboy” in ””” A picture from Playboy magazine is the most widely used for all sorts of image processing algorithms” ””

>> True

Add the keyword not in front of in to check if one string is not in another string:

“ hello ” not in ””” The computer virus was initially designed without any harmful intentions ”””

>> True

 

Escaping Strings

What if you want to use quotes inside a string? If we use quotes inside a string we get a syntax error:

””” Sun Tzu said "The supreme art of war is to subdue the enemy without fighting." "


>> SyntaxError: invalid syntax

We can solve this with escaping, which means putting a special symbol in front of a character that has special meaning in Python (in this case the special character is a quote), to let Python know that this particular character is meant to be a character and not the special Python symbol it usually represents.

The special symbol we use to escape our quote is a backslash.

“““Sun Tzu said \"The supreme art of war is to subdue the enemy without fighting.\" ”””

>> ' Sun Tzu said "The supreme art of war is to subdue the enemy without fighting." '

 

Newline

We can use “\n” inside a string to represent a newline:

print(“line1 \nline2”)

>> ‘line1’

>> ‘line2’

 

Concatenation

We can add two strings together using the addition operator. The result will be one string with the characters from the first string followed by the characters from the text strings. This is called concatenation:

“cat” + “in” + “the” + “hat”

> ‘catinthehat’

“cat ” + “in ” + “the ” + “hat ”

>> ‘cat in the hat’

 

String Multiplication

We can also multiply a string by a number with the multiplication operator:

“cat” * 3

>> ‘catcatcat’

 

For Loops

For loops execute a set of instructions a certain number of times. You give a for loop a number to start at (we will call this number a ), a number to stop at (we will call this number z ), a set of instructions, and a variable that keeps track of the number of times the instructions have been executed (we will call this variable i ).

# calthoff/tstp

for i in range(0, 10):

print(i)

>> 0

>> 1

…

>> 9

In this example, we chose I as our variable_name. This is the variable name people in the Python community usually use when they write a for a loop. Your code_to_execute has access to the variable i.

 

When we run this program, our program enters ours for the loop. a starts at 0, z starts at 10 and I start at the same value as a — 0.

 

The first time around the loop i is 0, and our for loop executes its instructions— print(i) — which prints 0 (because i is equal to 0 ). The next time around the loop, i is incremented by 1, so i is now 1, and so 1 gets printed.

 

The next time around the loop, i is incremented by 1 again, and so now i is 2, and so 2 gets printed. Eventually, I will equal 10. When this happens, nothing will print, because i is equal to z and so the loop ends.

 

When a loop ends, its instructions stop executing, and Python moves on to the next line of code after the loop. That is why 9 is the last number printed. If there were more code after our loop, Python would execute it, but in this example, there is not, and so the program ends.

 

There is another for loop syntax we can use for iteration. Iteration means going one by one through an iterable. An iterable is an object that has indexes. Some examples of iterables are strings, lists, tuples, and dictionaries. Iteration is done with the syntax for variable_name in iterable: [code_to_execute] .

 

Just like the first syntax, we chose a variable name and define the code to be executed each time around the loop. In this syntax, instead of our loop executing code until i is equal to z, our loop starts at the first index in the iterable and stops after the last index.

 

Also, variable_name gets assigned to the item at the index we are at (in the iterable). Here is an example:

# calthoff/tstp

my_string = “Python”

for character in my_string:

print(character)

>> ‘P’

>> ‘y’

>> ‘t’

>> ‘h’

>> ‘o’

>> ‘n’

# calthoff/tstp

my_list = [“a”, “b”, “c”]

for item in my_list:

print(character)

>> ‘a’

>> ‘b’

>> ‘c’

# add github

my_tuple = (“a”, “b”, “c”)

for item in my_tuple:

print(character)

>> ‘a’

>> ‘b’

>> ‘c’

# add github

my_dict = {“self”: “taught”, “programming”: “wizard”} for key in my_dict:

print(key)

>> “self”

>> “wizard”

Each of these examples loops through an iterable, and prints each item in it. You will notice we used several different variable names for variable_name. In this syntax, instead of using i, you want to use a descriptive variable name. In the first example, we used the character, because each item at an index in a string is called a character.

 

In the second and third examples, we used item, because each object in a list or tuple is called an item. In the last example, we used key, because when you iterate through a dictionary like this, you can only access each key in the dictionary, not the value— so we chose a variable name to make this clear.

 

Being able to loop through an iterable is very useful. Iterables are used to store data, and you can use for loops to loop through your data to easily make changes to it or move the data from one iterable to another.

 

Break

You can prematurely end a for or while loop with the keyword break. For example, the following loop will run one hundred times:

# calthoff/tstp for i in range(0, 100):

print(i)

>> 0

>> 1

...

But if we add a break statement to the code the loop executes, the loop only runs once:

# calthoff/tstp

for i in range(0, 100):

print(i)

break

>> 0

The loop goes around once and prints 0. When the break keyword is executed, the loop ends. This is useful in many situations. For example, we can write a program that asks the user for input until they type “q” to quit:

# calthoff/tstp

“““If you are unfamiliar the reference in this example, go watch Monty Python and the Holy

Grail!”””

questions = [“What is your name?”, “What is your favorite color?”, “What is your

quest?”]

n = 0

while True:

print(“Type q to quit”)

answer = input(questions[n])

if answer == “q”:

break

n += 1

if n > 2:

n = 0

Each time through our infinite loop, our program will ask the user a question from our list of questions.

 

We use the variable n to keep track of a number which we use as an index to get a question from our questions list. When n becomes greater than 2, we’ve run out of questions and we set n back to 0 which will ask the first question in the list.

 

This will go on indefinitely unless the user types in “q”, in which case our program hits the break keyword and the loop end, which ends our program.

 

Continue

You can use the keyword continue to stop executing a for or while loop’s code, and jump to the top of a loop. Say, for instance, we want to print the numbers from 1 to 5, except for the number 3. We can do this by using a for loop with the continue keyword:

# calthoff/tstp

for i in range(1, 6) :

if i == 3:

continue

print(i)

>> 1

>> 2

>> 4

>> 5

 

In our loop, when I equal 3, our program hits the continue keyword. Instead of causing our loop to exit completely—like the break keyword—the loop persists but we get jumped to the top of our loop, which means any of the loop’s code that would have executed that time around the loop gets skipped.

 

In this case, when i is equal to 3, everything after continues is skipped (in this case print(i) ). The result is 3 is not printed. Here is the same example with a while loop:

# add GitHub i = 1

while i <= 5:

if i == 3:

continue

print(i)

 

Importing Built-in Modules

In order to use a module, you must import it first, which means use a special syntax to make the code in the module available to use in your program. This is done with the syntax import [module_name]. import is a keyword for importing modules and must be followed by the module name.

 

For now, we are only going to learn how to import built-in modules and modules located in the same folder as the module you are importing it from.

 

We can import Python’s built-in math module with the following syntax:

import math

>> 

 

The math module is a module that comes with Python when you install it. It is a regular Python file with a bunch of math-related functionality—it contains Python functions that are useful when you are doing the math.

 

Once you’ve imported a module, you can use any of the code from it. You can access a function in the module with the syntax [path_to_module]. [function_name]() . With this syntax, you can use any of the code (such as a function) from the math module in your program:

import math

math.fabs(-3)

>> 3

The fabs function returns the absolute value of the parameter you pass in. You may be wondering how you are supposed to know there is a math module with a function called fabs in it.

 

A list of Python’s built-in modules can be found at Python Module Index. If you search for the math module, there is a link that takes you to a page that lists every function in the math module, what each function does, and what parameters it takes.

 

Another built-in module is the random module. Here is an example of importing the random module, and using a function from it called rant that takes two numbers as parameters and returns a random number between them.

# The output of this program might not be 52 when you run it—it’s random! import random

random.randint(0,100)

>> 52

 

There are other syntaxes for importing modules, but in general import [module_name] is the syntax you should use, and the one we will be using throughout the blog. Finally, you should do all of the imports for your program at the top of your file.

 

Hangman

First, we create a function called hangman we can call to start the game. The word to guess is stored in the variable word (you can change the word the player has to guess by changing the variable). We use another variable wrong_guesses to keep track of how many incorrect letters the player has guessed.

 

When each string in the stages list is printed on a new line, a picture of hangman forms. letters_left is a list made up of each character in a word. We will use it to keep track of which letters are left to guess in our word.

 

The scoreboard is a list of strings used to keep track of the hints we display to the user e.g., “c __ t” if the word is “cat”. The initial value of score_board is calculated with [‘__’] * len(word) which returns a list made up of the number of underscores to start with. For example, if the word is “cat” score_board starts as [“__”, “__”, “__”] .

 

Finally, we have a winning variable that starts as False to keep track of whether the player has won the game yet or not. To start the game off we print Welcome to Hangman. When you build a game, you normally use a loop that continues until the game is over.

 

Here is the loop we will use in our game:

while wrong_guesses < len (stages) - 1 :

print ( ' \n ' )

guess = input ( "Guess a letter" )

if guess in letters_left:

character_index = letters_left.index(guess)

score_board[character_index] = guess

letters_left[character_index] = '$'

else :

wrong_guesses += 1

print ( '' .join(score_board))

print ( ' \n ' .join(stages[ 0 : wrong_guesses + 1 ]))

if '__' not in score_board:

print ( 'You win! The word was:' )

print ( ' ' .join(score_board))

win = True

break

Our loop continues as long as the variable wrong_guesses is less than the len(wrong_guesses) - 1. wrong_guesses keeps track of the number of wrong letters the user has guessed, so as soon as the user has guessed more wrong letters than the number of strings that make up the hangman, the game is over.

 

The reason we subtract 1 is that the length function in Python does not count from 0, it counts from 1. In order to compensate for this, we have to subtract one, to compensate for the fact that length counts starting from 1 instead of 0.

 

The reason we want to count from 0 is that stages are a list, and we are going to be using wrong_guesses as an index to get the strings from stages and indexes to start at 0.

 

Inside our loop, we print a blank space to make our game look nice when it’s printed in the shell. Next, we collect the player’s guess with the built-in input function and store the value in the variable guess.

 

I the guess is in letters_left (a variable from the beginning of our program that keeps track of the letters that haven’t been guessed yet), the player guessed correctly.

 

If the player guessed correctly we need to update our score_board list, which we use later in the game to display the score. If the user guessed “c”, we want to change our score_board to look like this: [“c”, “__”, “__”].

 

We use the index method on our letters_left list to get the first index of the letter that was guessed. We use that index to replace the underscore in score_board at the index with the correctly guessed letter.

 

We have a problem though. Because index only returns the first index of the character we are looking for, our code will not work if the word (the word the player is guessing) has more than one of the same character.

 

To compensate for this, we modify letters_left by replacing the character that was just correctly guessed with a dollar sign so that the next time around the loop, if there is more than one letter in the word index will find the second occurrence of the letter since the first occurrence of the letter was replaced by a dollar sign.

 

If on the other hand, the player guesses an incorrect letter, we simply increment wrong_guesses by 1.

 

Next, we print the scoreboard and print our hangman using our score_board and stages lists. To print the scoreboard, all we have to do is print '' .join(score_board).

 

Printing the hangman is trickier. When each of the strings in our stages list is printed on a new line, a complete picture of a hangman is printed. We can easily create the entire hangman by printing ' \n ' .join(stages).

 

This connects each string in the stages list with a blank space ( \n). But we want to print our hangman at the stage we are currently at, which we accomplish by slicing our stages list.

 

We start at stage 0 and slice up until whatever stage we are at (represented by the variable wrong_guesses ) plus one.

 

The reason we add one is because when you are slicing, the end slice does not get included in the results. This gives us only the strings we need to print the stage of the hangman we are currently on which we then print.

 

The last thing we do in our loop checks if the user has won the game If there are no more underscores in the score_board list, we know the user has guessed all the letters and won the game.

 

If the user has won, we print that they won and we print the word they correctly guessed. We also set the variable win to True which is used when we break out of our loop.

 

Once we break out of our loop, if the user won, we do nothing, and the program is over. If the user did not win, the variable win will be False. If that is the case we know the user lost the game and we print the full hangman, print “You lose!” followed by the word they incorrectly guessed:

if not win:

print ( ' \n ' .join(wrong_guesses[ 0 : stage]))

print ( 'You lose! The words was {}' .format(word))

hangman()

 

State

One of the fundamental differences between different programming paradigms is the handling of state. The state is the data your program has access to. Programs store data in variables—so the state is the value of a program’s variables at a given time the program is running.

 

Imperative Programming

In Part I, we learned to program imperatively. Imperative programming can be described as “do this, then that”. An imperative program is a sequence of steps moving toward a solution—with each step changing the program’s state. An example of imperative programming would be:

x = 2

y = 4

z = 8

xyz = x + y + z

>> 14

Each step of the program changes the program's state. We get to xyz by first defining x, followed by y, followed by z and finally defining xyz.

 

Functional Programming

Functional programming is another popular programming paradigm. It originates from lambda calculus. Functional programming involves writing functions that—given the same input— always return the same output.

 

In functional programming, you only program with functions, you do not use classes—a feature of object-oriented programming we will learn about shortly.

 

There is a lot of jargon in functional programming and Mary Rose Cook does a great job cutting through it with her definition, “Functional code is characterized by one thing: the absence of side effects. It doesn’t rely on data outside the current function, and it doesn’t change data that exists outside the current function.” 

 

She follows her definition with an example which I will also share with you. Here is an unfunctional function:

a = 0

def increment ():

global a

a += 1

Here is a functional function :

def increment (a):

return a + 1

The first function is unfunctional because it relies on data outside of itself, and changes data outside of the current function by incrementing a global variable.

 

The second function is functional because it does not rely on any data outside of itself, and it does not change any data outside of itself either. Functional programmers write functions this way to eliminate side effects—the unintended consequences that happen when you are constantly changing the state of your program.

 

Inheritance

Inheritance in programming is similar to genetic inheritance. In genetic inheritance, you can inherit attributes from your parents, like your eye color.

 

Similarly, when you create a class, it can inherit from another class (which is then called its parent class)—giving the new class you created the parent class’s variables and methods.

 

In this section, we will model a kid and adult using inheritance. First, we define a class to represent an adult:

 

class Adult ():

def __init__ ( self , name , height , weight , eye_color):

"""height is in feet, weight in lbs."""

self .name = name

self .height = height

self .weight = weight

self .eye_color = eye_color

def print_name ( self ):

print ( self .name)

tom = Adult( "Tom" , 6 , 150 , "brown" )

print (http://tom.name)

print (tom.height)

print (tom.weight)

print (tom.eye_color)

tom.print_name()

>> Tom

>> 6

>> 150

>> brown

>> Tom

Using this class we can create Adult objects with a name, height, weight and eye color. In addition, our Adult objects have a method called print_name that prints the parent’s name.

 

We can model a human child that also has a name, height, weight, eye color and can print its name; with an extra method, we don’t want our Adult objects to have called print_cartoon; using inheritance.

 

You inherit from a parent class by adding parenthesis to the class name you are defining and passing in the class name you want to inherit from as a parameter. Here is an example:

class Adult ():

def __init__ ( self , name , height , weight , eye_color):

# height is in feet, weight in lbs. self .name = name

self .height = height self .weight = weight

self .eye_color = eye_color

def print_name ( self ):

print ( self .name)

class Kid (Adult):

def print_cartoon ( self , favorite_cartoon):

print ( "{}'s favorite cartoon is {}" .format( self .name , favorite_cartoon))

child = Kid( "Lauren" , 3 , 50 , "blue" )

print (http://child.name)

print (child.height)

print (child.weight)

print (child.eye_color)

child.print_name()

child.print_cartoon( 'DuckTales' )

>> brown

>> Ricky

>> DuckTales s

By passing in Adult to our Kid class, our Kid class inherits the variables and methods of our Adult class: when we create a Kid object we pass it a name, height, weight, and eye color; and our Kid object is able to use the method print_name; all of which was inherited from its parent class (without having to define any of it in our Kid class).

 

This is important because not having to repeat code makes our program smaller and therefore more manageable.

 

After inheriting from our Adult class, all we had to do was define a new method called print_cartoon in our Kid class to create a Kid class with all of the functionality of our Adult class, plus additional functionality; all without affecting our Adult class.

 

Polymorphism

The best definition I’ve found of polymorphism is “polymorphism is the ability (in programming) to present the same interface for differing underlying forms (data types) ” An interface refers to one or more functions or methods. Let’s take a look at a situation where this is the case:

print( 'Hello World' )

print( 200 )

print( 200.1 )

>> “Hello World”

>> 200

>> 200.1

In this example, we were able to present the same interface (the print function) for three different data types: a string, an int, and a float. We didn’t need to call three separate functions — print_string, print_int, or print_float in order to print these three different data types— instead Python has just one interface for all of them.

 

Let’s take a look at another example. Say we want to write a program that can draw different shapes: triangles, squares, and circles. Each of these shapes is drawn in a different way, so the methods to draw them would all have different implementations.

 

In Python, we can create different draw methods for each shape so that Triangle.draw() will draw a triangle, Square.draw() will draw a square, and Circle.draw() will draw a circle.

 

Each of these shape objects has its own draw interface that knows how to draw itself. When we have a shape object, we know we can call the draw function to draw the shape. The same interface is presented for all the different shape data types.

 

If Python did not support polymorphism— we would need a function that creates a triangle, and another function called draw_triangle to draw it; a function to create a circle, and a function called draw_circle to draw it; etc. Because Python has polymorphism, every shape can simply be drawn with its draw method.

 

This makes our shape objects much easier to use and explain. Instead of explaining—we have three functions representing three different shapes, and another three functions that draw each of them; we can simply tell whoever is using the code: we have three shapes—if you want to draw one—call its draw method.

 

Abstraction

We use abstraction in object-oriented programming when we create a class and define its methods. Say we create a class to represent a person. When we define our person class— and the methods that go with it— we are creating an abstraction.

 

Our definition of a person could include eye color, hair color, height and ethnicity as well as the ability to read, write and draw.

 

We could have a five foot three person with blue eyes, blonde hair unable to read, write or draw. Or we could have a six-foot five-person with brown eyes, brown hair that can read, write and draw. Both of these fall into the category of the person abstraction we’ve created.

 

When we design object-oriented programs, we create abstractions of different concepts that all work together to form our program.

 

For example, we may create an abstraction of a person, and an abstraction of a government and model how many people live under each government in the world. Abstraction allows us to model objects with clear boundaries, and have them interact with each other. 

 

Encapsulation

In object-oriented programming, encapsulation hides our codes internal data. When the code is encapsulated, it means when it is called, the caller cannot access the code's internal data. Take a look at the method get_data :

class Data :

def get_data (self, index , n):

data = [ 1 , 2 , 3 , 4 , 5 ]

data.append(n)

 

The method has a variable called data. When we call get_data, there is no way for us to access this variable because of encapsulation. If there was no encapsulation, we might be able to access the variable data —and append n to it—like this:

# warning this code does not work Data.get_data.data.append(6)

 

If this was allowed, anyone could access the data variable in our get_data method. Instead of relying on our implementation of the get_data method—they could append n to data themselves.

 

This is not a problem—until we change the implementation of get_data. What if we decide to want the variable data to be a tuple instead of a list?

 

If we make this change, it will break anyone’s code calling append on the variable data, because tuples do not have an append method. But because of encapsulation, this scenario is not possible (which is why the code does not work), and we can be assured changes to the internal implementation of our code won’t break our client’s code (client is a term for the person using a piece of code).

 

Composition

While the composition is not one of the four pillars of object-oriented programming, it is an important concept related to the rest. The composition is used to represent “has a” relationships—it occurs when one object stores another object as a variable.

 

For example, say we want to represent the relationship between a dog and its owner—this is a “has a” relationship—a dog has an owner. First, we define our dog and people classes:

class Dog ():

def __init__ (self, name, breed, owner):

self .name = name

self .breed = breed

self .owner = owner

class Person ():

def __init__ ( self , name):

self .name = name

When we create our dog object, we pass in a person object as the owner parameter:

mick = Person( "Mick Jagger" )

dog = Dog( "Stanley" , "French Bulldog" , mick)

print (dog.owner)

>> Mick Jagger

Now our dog Stanley has an owner—a Person object named Mick Jagger—we can easily reference.

 

How Variables Work

In this section, we are going to learn more about variables. Variable “point” to an object.

number = 100

number points to an integer object with the value 100.

[illustration of a point to an object]

number = 101

 

When we assign a new value to number, it points to a new integer object with the value 101, and the old integer object with a value of 100 is discarded because it is no longer being used. Two variables can point to the same object:

x = 100

y = x

x points to an integer object with a value of 100. When we assign y to x, y now points to the same integer object x points to they both point to an integer object with a value of 100.

What do you think the following program will print?

x = 10

y = x

x += 1

print(x)

print(y)

 

The answer is 11 and 10. x points to an integer object with a value of 10, and when we create y, it points to the same integer object. When we increment x, x points to a new integer object —with a value of 11, but the integer object with a value of 10 is not discarded, because it is being used: y still points to the integer object with a value of 10.

 

So when we print x, 11 prints because we assigned x to a new integer object—11—but when we print y — 10 prints because changing the value of x, which points to the integer object 11, does not affect the value of y. Here is another example to illustrate this point. What do you think the output of this code will be?

 

x = [1, 2, 3]

y = x

y[2] = 100

print(x)

print(y)

The output will be [1, 2, 100] twice. The reason is that both x and y point to the same list object. In the third line, we make a change to that single list object, and when we print both variables, it prints the list object they both point to, with the changes made in line 3.

 

The keyword is returned True if two objects are the same object (they are stored in the same location in memory) and False if not.

class Person :

def __init__ ( self ):

self .name = 'Bob'

bob = Person()

the_same_bob = bob

print (bob is the_same_bob)

another_bob = Person()

print (bob is another_bob)

>> True

>> False

When we use the keyword is with bob and the_same_bob, the result is True because both variables point to the same Person object. When we create a new Person object and compare it to the original bob the result is False because the variables point to different Person objects.

 

None

The built-in constant None is used to represent the absence of a value:

x= None

x

>> None

We can test if a variable is None using conditional statements.

x = 10

if x:

print ( "x is not None" )

else :

print ( "x is None :( " )

>> x is not None

x = None

if x:

print ( "x is not None" )

else :

print ( "x is None :( " )

>> x is None :(

While this may not seem useful now, it will be later.

 

Classes Are Objects

In Python, classes are objects. This idea comes from the influential programming language SmallTalk. This means that when running a program in which you define a class—

 

Python turns it into an object—which you can then use in your program:

class Pterodactyl :

pass

print (Pterodactyl)

>> <class '__main__.Pterodactyl'>

Without any work on our part, Python turns our class definition into an object which we can then use in our program, by printing it for instance.

 

Overriding Built-in Methods

Every class in Python automatically inherits from a parent class called Object. All classes in Python inherit methods from this parent class. Python’s built-in functions use these methods (which we learned are called magic methods)—in different situations—like when we print an object:

class Lion :

def __init__ ( self , name):

self .name = name

lion = Lion( "Dilbert" )

print (lion)

>> <__main__.Lion object at 0x101178828 >

When we print our Lion object, Python calls the __repr__ method on our object, which it inherited from the Object parent class. It prints whatever the __repr__ method returns.

 

We can override this built-in method to change what happens when the print function prints our object.:

class Lion:

def __init__ ( self , name):

self .name = name

def __repr__ ( self ):

return self .name

lion = Lion( "Dilbert" )

print (lion)

>> Dilbert

Because we overrode the __repr__ method, when we print our Lion object, the Lion object’s name— Dilbert — gets printed instead of something like <__main__.Lion object at 0x101178828 > which the inherited __repr__ method would have returned.


Not all magic methods are inherited. Python expressions like 2 + 2 expect the operands to have a method the operator can use to evaluate the expression. In example 2 + 2 , integer objects have a method called __add__ which is called when the expression is evaluated, but __add__ is not inherited when you create a class. We can create objects that can be used as operands in an expression with the addition operator by defining a __add__ method in our class:
class AlwaysPositive :

def __init__ ( self , number):

self .number = number

def __add__ ( self , other):

return abs ( self .number + other.number)

x = AlwaysPositive(- 20 )

y = AlwaysPositive( 10 )

print (x + y)

>> 10

Our AlwaysPositive objects can be used as operands in an expression with the addition operator because we defined a method called add. The method must accept the second parameter, because when an expression with an addition operator is evaluated, __add__ is called on the first operand object, and the second operand object gets passed into __add__ as a parameter. The expression then returns the result of __add__ .


In this example, we added a twist. We used the function abs to return the absolute value of the two numbers being added together. Because we defined __add__ this way, two AlwaysPositive objects added together will always return the absolute value of the sum of the two objects.

 

Finding Bash

You can find Bash by searching for a terminal from the icon titled Search your computer and online resources if you are using Ubuntu or from Spotlight search if you are using a Mac.

 

Using Bash

Bash is similar to the Python Shell. The Bash command line shell takes its own programming language called Bash as input. The programming language Bash has commands—which are like functions in Python.

 

We start with a keyword; type a space; type the parameter we want to give the command (if any); hit the enter key, and Bash returns the result. One of Bash’s commands is an echo, which is similar to the print function in Python. Here is an example:

$ echo Hello, World! >> Hello, World!

 

We typed the command echo into Bash, followed by a space and Hello, World! as a parameter. When we press enter, Hello, World! prints in the Bash command line shell. You can also use programs you’ve installed—like Python—from the Bash command line shell. Enter the command python3 to use Python from the shell:

$python3

>> 

Now you can execute Python code:

print(“Hello, World!”)

>> Hello, World! Enter exit() to exit Python.

 

Flags

Commands have a concept called flags that allow the issuer of the command to change the behavior of the command.

 

If you use the command without any flags, all of the command's flags are set to false. But if you add a flag to a command, the flag is set to true and the behavior of the command changes.

 

The - and -- symbols are used to attach flags to a command. -- the author is an example of a flag you can add to the ls command to print the author of all of the directories and files that get listed.

 

This example is for Linux, on OS X you can use the same flag but you need to use one dash instead of two. Here is an example of using the --author flag with the ls command:

$ ls --author

>> drwx------+ 13 coryalthoff 442B Sep 16 17:25 Pictures

...

When you add the --author flag to your ls command, the name of each directory and folder in your current directory will print—as well as some additional information, including the name of the person that created them.

 

vim

Vim is a command line text editor. It's like Microsoft Word, except you use it from the command line. If you are using Ubuntu, first install vim with the command apt-get install vim. Make sure to enter Y when prompted. If you are using a Mac, it should come with vim If you are using the online bash shell, it already has vim installed.

 

You can create a new file with vim by going to the command line and using the command vim [name of the file to create]. Use the command vim self_taught.txt to create a new text file called self_taught.txt.

 

Press the I or insert key and type a paragraph of text into the file. Any paragraph of text you find on the internet will do.

 

The reason you have to hit the I or insert key when you first enter a file is that vim has different modes optimized for different activities. vim starts in, Normal Mode, which is not meant for adding text to the file (it is meant for easy navigation)—you can delete text in normal mode, but you cannot insert new text.

 

Once you press the I or insert key and enter normal mode, you can use vim like a word processor—try typing into vim.

 

Since you can’t use your mouse to move the cursor around, it’s important to learn a few shortcuts to jump to different locations in your document, so that you don’t end up using the arrow keys on your keyboard (because that’s slow). To practice moving around, first make sure you are in Normal Mode ( control-c ).

 

You can move to the beginning of a word by pressing b and the end of a word with e. 0 will move you to the beginning of the line you are on, while the dollar signs $ will move you to the end of the line. H will move you to the first line of the page and L will move you to the last.

 

You can delete entire lines of text in normal mode by pressing the d key twice. Spend some time using these keys to get familiar with navigating through a file using vim.

 

To exit vim you need to first switch to Normal Mode by pressing control-c. Next press the shift key and then hit the colon key (while still holding the shift key).

 

From here you can type q! if you want to quit without saving your changes to the file, or type x if you want to save your changes and quit. Once you’ve typed one of these options, press the enter key to exit.

 

vim is useful in a few situations: servers are usually only accessed with a command line shell, so if you want to make changes to a file on a server, you need to use a command line text editor, and once you get good at using vim, it is often faster to use it to make changes than using a conventional word processor. Try typing vimtutor in the Bash command line shell and see what happens.

Touch

You can use the command touch followed by a file path to quickly create a new file:

$ touch purple_carrots.txt

>> 

 

Tab Complete

Tab complete is a feature that will help improve the speed you get things done from the command line shell. If you are in the middle of typing a command you and press the tab button on your keyboard, the command line shell will try to autocomplete the command for you.

 

Try it for yourself by typing ech in the command line followed by tab; ech will automatically get turned into echo. 

You can also use tab to complete file or directory paths.

 

Start typing the path of the directory you are in and finish it off by pressing tab. If you press tab and nothing happens, it is because two commands or paths are named similarly, and the shell doesn’t know which to choose.

 

For example, if you have a directory named the car, and another directory named candy, and you type ca and try to tab complete, nothing will happen because the shell won’t know whether to choose car or candy. If you add an n so you’ve type can, and press tab complete, the shell will autocomplete candy because the shell knows the car is not correct.

 

Packages

A package is software “packaged” for distribution—it includes the files that make up the actual program, as well as files with metadata (data about data); such as the software’s name, version number, and dependencies (programs that need to be downloaded in order for it to run properly).

 

Package managers download packages, install them—which means downloading any dependencies the package has.

 

Apt-get

Apt-get is a package manager that comes with Ubuntu. You cannot use apt-get from the online Bash command line shell emulator. You need to have Ubuntu installed so you can use the command sudo. If you do not have Ubuntu installed on your computer, you can skip this section.

 

You install a package with sudo apt-get install [package_name]. Here is an example of installing a package named aptitude :

$ sudo apt-get install aptitude

>> Do you want to continue? [Y/N] Y

…

>> Processing triggers for libc-bin (2.19-0ubuntu6.7) …

 

Make sure to type Y when prompted with “Do you want to Continue [Y/N]”. The aptitude package should now be installed. You can use it by typing aptitude into the Bash command line shell:

 

$ aptitude

>> 

This will open up Aptitude, a helpful program that shows information about packages. You should see a list of all the packages installed on your operating system under the option Installed Packages, as well as a list of packages you’ve yet to install, under Not Installed Packages.

 

You can also list the packages that have been installed with apt-get with the command apt list --installed :

$ apt list --installed >> python3-requests...

 

You can remove packages using aptitude with the syntax apt-get uninstall [package_name]. If you want to remove aptitude from your computer, you can uninstall it with sudo apt-get uninstall aptitude. That’s all there is to it—installing and removing programs is as simple as using two commands with apt-get.

Recommend