IMPORTANT: All paragraphs of code are stand alone. You can copy and paste then into PyCharm and have them work. All code has been tested. Two cautions after copy and pasting code from any source.
FIRST: There is always an issue with quote marks when you copy and paste. There are two types of quote marks, depending on the font. Curly quote marks and straight quote marks. Only straight quote marks work in PyCharm. If in doubt retype quote marks in the PyCharm editor.
SECOND: Indentation is significant and important in Python. If the code does not run correctly after copy and pasting to PyCharm, check the indentations.
We are writing this manual for first year engineering students who need to know basic programming skills. Typically this will be in an introduction to engineering course. We start with simple examples and proceed to more complex examples. I have addressed common problems to improve the student’s learning curve. My goal is to make the manual as user friendly as possible. All programs have been tested and can be used as templates for your own programs. When done with the manual you will be prepared to program. There are two basic types of programming languages, procedural and object oriented. Python can be used as both. We will concentrate on procedural programming for the first part of the tutorial. This programming is similar to Basic programming in Calc and VBA programming in Excel. But without the spreadsheet. You may ask why Python? Python is capable of infinitely more than is covered in this tutorial. It is used in web applications, science, and machine learning (artificial intelligence). Even though C++ runs faster, Python development times are much faster. Python is friendly. At the end of this tutorial we will spend some time on OOP, Object Oriented Programming. OOP allows us to reuse functions in new ways. By following along in the code, I hope you can understand some of its potential. If you run into a problem do not be afraid to explore the internet!
We will be using PyCharm as the IDE for Python 3.7. IDE stands for Integrated Development Environment, which is a fancy way of saying where you write your code. PyCharm makes programming much easier by organizing your work and putting all the tools at your fingertips. Python programs consist of files with code, custom functions, and built-in functions. (Custom functions are written by you, built-in functions are included in Python libraries.)
Chapter six concludes the first part of this book with a bubble sort sample program. It combines all the elements described in the first part of the manual. It includes, variables, loops, if statements, lists, and functions.
1.1.1 First we must install Python, then we will install PyCharm. System requirements are 4GB RAM minimum, with 8GB RAM suggested.
1.1.2 Search Google for python.org. Click on Download Python exe file. Select Windows x86-64 executable installer to start download. Click Save. Click Open folder.The exe file will be open in Windows Explorer Downloads.
The MD5 sum is listed on the Python download release page. Copy and paste the MD5 sum to a text editor (Notepad) for future reference.
1.1.3 Check the integrity of the download with MD5 checksum. You must be sure the download occurred correctly, or you will waste days of time later on.
Shift + Right Click on the python exe file name in Explorer Downloads, and select Copy as path.
We will use Windows Powershell to get the MD5 hash for the downloaded Python exe file. At the bottom left of the desktop use Search to locate powershell.exe. Click on the powershell.exe name and the command line will open. Duplicate the example below, except inserting your file pathname. Right click to paste the file name location and path. The Python exe will appear (in double quotes). Press Enter. In a minute the hash produced by the MD5 algorithm will appear. Compare the hash number to the hash number on the python.org website. If they are the same you are ready to install. If they are different, download the python exe again.
PowerShell Example
get-filehash "C:\Users\user1\Downloads\python.exe" -algorithm MD5
1.1.4 Double click on the Python exe file, and start the installation.
Check the box Add Python to PATH. Click on Install Now. Follow the prompts. Click on Disable path length limit.
Next look at the programs list on your computer for the Python icons.
Click on the IDLE tab. Type print(“Hello World”) at the command line. Press Enter and Hello World will appear. Python is working.
If IDLE does not work, try the Python3.9 tab.
Open the Windows command prompt. Type python --version and press Enter. The python version will appear. Python from the prompt is working.
1.2.1 After the Python installation, we will download PyCharm. Google PyCharm download and select the www.jetbrains.com Download. On the download website, select Windows and click on Community download. This is a free application. You will see a window Thank you for downloading PyCharm. Click on Save. Click Open Folder. Copy and paste the SHA-256 checksum to a text editor (Notepad) for future reference.
1.2.2 Verify the SHA-256 Checksum for the PyCharm.exe Using Windows Powershell. The exe file will be open in Windows Explorer Downloads folder. Shift + Right Click on the exe file name, and select Copy as path.
At the bottom left of the desktop use Search to locate powershell.exe. Click on the application name and the command line will open. Duplicate the example below, except inserting your file pathname. Right click to paste the filename location and path for PyCharm exe. It will appear (in double quotes). Press Enter. In a minute the hash produced by the SHA-256 algorithm will appear. Compare the hash number to the hash number on the PyCharm website. If they are the same you are ready to install. If they are different, download the PyCharm exe again. PowerShell Example
get-filehash "C:\Users\user1\Downloads\PyCharm.exe"
Notice that there is no --Algorithm SHA-256 at the end of the command. SHA-256 is the default value.
1.2.3 Double click on the PyCharm.exe file in Downloads, and start the installation.
1.2.3.1 Follow the prompts.It is always important to save and backup your programs. Remember in Section 1 we saved Test 1 (Hello World) with File→Save All. Both the project and the file in the project are saved. Save->All command saves the Project 1 to the folder PycharmProjects in your home directory.
I suggest signing up for a free Google Drive cloud account. You can store up to 15GB of data free.
Variable names in Python can be any length and can consist of uppercase and lowercase letters ( A-Z , a-z ), digits ( 0-9 ), and the underscore character ( _ ). Although a variable name can contain digits, the first character of a variable name cannot be a digit.
Variable names are case sensitive, and variable names are lowercase by convention. This means that My_variable and my_variable are different variable names.
A variable is created when you assign it a value. This is called declaring a variable. The interpreter infers the type of variable by how it is assigned. (Notice the comments which begin with the # sign. They are not interpreted.)
name = 'Steve' #name is a string
age = 45 #age is an integer
number = 8.7 #number is float
print(type(name)) #prints the type as string
print(type(age)) #prints the type as integer
print(type(number)) #prints the type as float
We can tell how a variable is assigned by using the type() function.
Go to File→New→Python File and type in a name such as test-2. Hit Enter. Copy and paste the code above into your PyCharm code window and run it.
IMPORTANT: When running code the quote marks must be of the straight kind. You will have to retype the quote marks in PyCharm to make them straight or you will get an error message.
The results will be as shown below.
class 'str'
class 'int'
class 'float'
The PyCharm screen is divided into three main parts. The left hand side is the Project Files management pane. The right hand side is the code pane, and at the bottom is the results pane. It is important to know where your cursor is because it determines the active pane. Be sure the code pane is active when running the code.
Eventually you will want to add a new project in addition to the one created when we set up PyCharm. Be sure to save the current project with Save All. Go to File→New Project. Type in the new project name, such as Project2 and accept the default New environment using. Click Create. An Open Project window will appear. Be sure to select Attach. This will add the project to your project list.
We will often use print functions to print out the value of a variable at a point in our program. It is especially useful for debugging. The basic statement is as follows:
x=5
print('x = ' , x)
"""
Sometimes you will see comments like
this with triple quotes, instead of the # sign:
"""
With the result:
x = 5
I have given an example of a comment with triple quotes, instead of the # sign, in case you see it in other texts.
We also need to input values. A simple input function follows:
myinteger = input("Input an integer: ")
print('myinteger= ' ,myinteger)
myinteger = int(myinteger)
# Output
# Input an integer: 8
# myinteger= 8
Copy and paste this to PyCharm, and run the program. The words “Input an integer” will appear in the lower output window. You must type in the integer “8” and press enter for “myinteger= 8” to appear. It is important to keep you cursor pointed in the lower left code box when entering the number, or you will type in the wrong spot.
Please note that 8 is not an integer, it is a string. To make the input a float or an integer, we must specify. The same is true for a float number.
mynumber = input("Input a float number: ")
print('mynumber= ' ,mynumber)
mynumber = float(mynumber)
# Output
# Input a float number: 16.1
# mynumber = 16.1
A list contains a list of values assigned to a variable. A list is essentially a single dimension array. Lists can also be two dimensional. Arrays proper are imported in a separate module. We will do that later. We create a list as follows:
mylist = [3,6,9,32]
print(mylist)
print(mylist[0])
print(mylist[1])
print(mylist[2])
print(mylist[3])
#Output
# [3, 6, 9, 32]
# 3
# 6
# 9
# 32
In the second half of the code I have printed individual items of the array. Notice the list starts with index 0.
Table of Contents
Ch1-Starting Out
Ch2-Loops
Ch3-If Statements
Ch4-Functions
Ch5-Variable Scope
Ch6-Bubble Sort
Ch7-Intro to OOP
Ch8-Inheritance
Ch9-Plotting
Ch10-Files
Ch11-Print Format
Ch12-Dict-Zip-Comp
Ch13-Slice Arrays