Learning Basic Python in 6 days — Day 1
Day 1
This blog post serves the purpose of allowing anyone to learn basic programming using Python. The topics cover list, strings, functions, logic, read and writing text files, packing and unpacking variables, and loops. We will be using Learn Python the Hard Way, which I encourage everyone to purchase since this online book does a great job of explaining the tutorials we will be completing. In today’s tutorial, we will be learning how to use the command line to run our programs, printing string to the command line, defining variables formatting string with variables, and asking for user input.
Ex0 Downloads and Command Line intro
Running programs through the command line are important for any programmer regardless of experience to programming. The sooner you become familiar with the command line the sooner you will be able to create dynamic programs, for example, automate when an excel file is sent to your boss each day via email.
We start by downloading Python 3.6.2 from the following link https://www.python.org/downloads/, after completing this we should be able to find idle from start. You can run your programs from idle although I encourage you to rather run the program from the command line. Next, we will use the Atom text editor which can be downloaded from the following link https://atom.io/. Now that we have downloaded Python 3 and our text editor we can now create a path in the command line to run our programs.
Next create a folder in Atom named ‘Python the Hard Way’, this way we both can use the same directory path to run our programs. Open the command line enter the following path.
We can now run Python from on the command line, now it is time for the part and running our first Python program.
ex1.py
In Atom, under the folder named ‘Python the Hard Way’, create a new file named ex1.py. We will type the following into the text editor:
On line 1 we are print the string “Hello World” to command line. Notice on line 4 that we use single quotes compared to double quotes in lines 1, 2 and 3. We can use either single or double quote to create a string in Python. On line 5 we use both the single and double quotes, we can use single quotes inside of double quotes without raising an error and vice versa on line 6. I encourage you to try using single quotes inside other single quotes and see what happens.
The run the program to the command line we do the following commands:
We start by typing python which tells the command line we will be running the Python program. Next, we type in the name of Python our Python program we created in Atom named ex1.py.
Ex2.py
In this example, we once again print a string to the command line and introduce comments in Python, which allows us the describe what we are doing in our program.
Notice how in Python we create a comment using # symbol. On line 7 we type in the # symbol followed by the print function. So let’s find out if this will print to the command line when we run the program.
The output of the following program should be as follows:
Notice how the print command on line 7 did not show up since we use the # symbol before the print statement.
Ex3.py
In this example, we will be learning to concatenate strings with integers and floats. We will also learn different math operations within Python: adding, subtracting, divide, multiply and modulo. We will also print boolean statements to the command line, boolean statements are True/False statements.
On line 2, we use a comma after the closing quotes, which allows us to concatenate the string with our math operation which will reduce to a float. A float is a number with a decimal after it, example 2.5 compared to a line 14 which will reduce to an integer. An integer is a whole number, examples 2 or 6. Another key point to call out is on line 2, Python will adhere to rules of operation: parenthesis, exponent, multiply, divide, add, then subtract (Please excuse my dear aunt Sally). So Python will divide 30 by 6 before adding the output to 25.
On line 12 we are creating some basic boolean logic, with the statement is 2 plus 3 less than 5 less 7. Python will print True or False to the command line depending on if the statement if Ture or False. On lines 21–24 we practice more with boolean logic, this will become important in later examples when evaluating if we should execute a line of code.
The output should be as follows:
We can see that on line 2 we have an output of a float since we used division. We use the division operator in Python, Python will always evaluate it to a float. Compare this to lines 3, 7 and 8 where Python evaluated the answer to an integer.
We can also see the answer to line 12 was False since 5 (2+3) is not less than -2 (5–7).
Ex4.py
In this example, we are going to be working with assigning integers and floats to variables. Think of a variable as a box, and the box holds contents of what we assign to it. We can use the contents of what we stored inside the box when calling the variable. We will also build upon what we already know with math operations and concatenation in Python. Please reference ex3.py, if you having trouble understand concatenating strings with floats and integers or need more practice with math operation in Python.
On line 1 we create the variable cars and assign the integer 100 to it. We do the following for lines 2–4, but then on line 5, we create a variable from the previously assigned variables. We create the variable named cars_not_driven by taking variable cars (100) minus variable drivers (30). On line 6 we assign cars_driven to the variable drivers, so now cars_driven holds the contents of what we assigned to drivers.
The output should be as follows:
Notice on the last line, we get the answer of 3.0. We get the following output from dividing 90 (passengers) by 30 (cars_driven which was set equal to drivers).
Ex5.py
In this example, we work with formatting strings, which is another way to insert variables into our statements. We will also be working with variables but this time we will also assign strings to the variables as well as integers and floats.
Notice how on line 9 we use the letter f before we start the string. This tells Python we are formatting the string so we can use the curly {} brackets. We call the variable we want to insert into the string, so on line 9 we inter my_name. On line 17 we once again define a variable by adding together the previously declared variables.
We should see the following printed to the command line:
Take some time and create your own variables and print statements using the f formatting before moving on. The best way to develop your programming skills is to try it on your own.
Ex6.py
For this last example, we will work on more advanced formatting and assign formatted strings to variables. This example will also use an old computer science joke.
On line 2 we set a formatted string equal to the variable x. Inside this formatted string, we call the variable defined about it named types_of_people. We do more practice on lines 4–6. We then print both variables on lines 8 and 9.
Notice on line 16 we just have the {} brackets without using the letter f as we had previously. Instead on line 18, we use the dot format method to call the variable hilarious into the variables joke_evaluation. If this is not easy to follow, I understand this took me some time and practice to fully grasp what is happing. Just try to read it line by line.
On line 23 we use the plus sign (+), which is another way to concatenate strings. As you learn to program you will find out there are multiple ways to complete an objective but there is only 1 right way.
We should get the following output:
Since this will be today’s last example, I encourage you to try creating your own programs. Set something equal to a variable and then call it using both formatting methods.
Thank You for taking the time to learn basics of Python, we will continue to keep building upon your new found skills in each subsequent tutorials.