LESSON 01 –
PROGRAMMING BASICS

 

 

LESSON NOTE/GUIDE

INSTALLING SOFTWARE

·       Install Python 3.

·       Install Pycharm (Community).

 

CREATE A FOLDER FOR YOUR WORK (WORKSPACE)

·       Create a folder where all your files will be stored.

o   Mr. C. uses a folder called workspaces and then a subfolder called workspace-python-202X for his folder name.

 

UNDERSTANDING THE IDE

·       Create a new project in Pycharm (called DAY01).

·       When the new project window appears, browse to your workspace folder that you created.

·       Create a new Python file.

·       To increase editor font size: Go under File > Settings > Editor > Font and change the Size.

·       To create a file to code in, right click on the project name and choose New > Python File.

 

PROGRAM 1 – HELLO WORLD

·       Create a new file.  A good filename would be program01.  Avoid spaces in filenames.

·       Introduction to comments.

·       Write the program that outputs “Hello World” to screen.

·       Discuss the idea of syntax.

·       Run a program for the first time.

o   Option 1 – Set Configuration

o   Option 2 – Right click on file (on left side) and run that file.

 

THE CODE

 

#Pat Campeau

#This is our first Python program.

 

print("Hello world")   

 

EXPLANATION

 

In the above code, the first two lines that start with a # symbol are comments.  They are completely ignored when the program is executed.

 

The last line uses the print function (or command) to output a string (or message) to screen.  Strings are always placed inside double quotes.

 

 

PROGRAM 2 – VARIABLE TYPES (int, float, string)

·       Explain that we should now create a new file for a new program.

·       Create three variables and set them to integer values and add them up.  Output the result.

·       Show how to output text and the number at once.

·       Do the same for floating point numbers (float).

·       Do the same for strings.  What happens when you add strings together?

·       Also show the comma used in a print statement

 

THE CODE

 

#1-Integers (int)

x = 4

y = 6

answer = x + y

print("The answer is", answer)

 

#2-Floating point numbers (float)

a = 4.2

b = 3.1

answer2 = a + b

print("The answer is", answer2)

 

#3-Strings

w1 = "hi"

w2 = "yo"

answer3 = w1 + w2    

print("The answer is", answer3)

 

EXPLANATION

 

We are using variables for the first time.  Variables allow us to store information from one line of code and use it in another.  

 

We create a variable by giving it a name and assigning it a value.  Variable names must start with a letter and can contain numbers. 

 

We are considering three types of variables in the program above:

  • int – integers
  • float – numbers with decimal points
  • string – sequences of characters


Each section contains four lines of code.  The first two lines in each create and give a value to a variable.  The next add the first two variables and stores that answer in a third variable. The final line outputs both a message and the content of the answer variable.

 

In the third section, you may notice that the third line is apparently adding two words together.  In Python, this is called string concatenation. It simply means that both words are glued together to create on word.

 

 

PROGRAM 3A – USER INPUT (TEXT)

·       Use the input function to get users to input a string value.

·       Do the greeting app.

·       Show how type casting can be used to get a number.

 

THE CODE

 

name = input("Enter your name")

print("Hello there", name)

 

EXPLANATION

 

The input function displays a message (usually an instruction to the user) and waits until the user enters a word (or a number) and hits enter.  The value entered is then stored in the name variable.

 

In the second line, we output the greeting message along with the name that was just entered by the user.


PROGRAM 3B – USER INPUT (NUMBERS)

·       Get the user to input numbers.

·       Learn how to convert (type cast) a value from string to number.

 

THE CODE

 

num1 = input("Enter a number")

num2 = input("Enter a number")

ans = int(num1) + int(num2)

print("The sum is", ans)

 

EXPLANATION

 

The first two lines simply get input from the user.  Eventhough the user enters numbers (we hope), the information is still seen as strings.

 

In the third line, we first convert each string to its integer value and then we add them up.

 

Note that to convert from string to int, we use the int() function.  The float() function works similarly.

 

In the final line, we simply output the sum to screen.

 

PROGRAM 4 – BASIC ARITHMETIC

·       Get two numbers from the user.  Remember that by default, you will get string variables, so you need to type cast them to int or float.

·       Add them together and output the value.

·       Multiply them together and output the result.

·       Divide one from the other and output the result.

 

THE CODE

 

num1 = int(input("Enter a number"))

num2 = int(input("Enter a number"))

 

#Adding numbers

ans1 = num1 + num2

print("The sum is", ans1)

 

#Multiplying numbers

ans2 = num1 * num2

print("The product is", ans2)

 

#Dividing numbers

ans3 = num1 / num2

print("The quotient is", ans3)

 

EXPLANATION

 

The first two lines simply get input from the user.  Note that we immediately use the int() functions to make the inputted data an integer.  So our variables are integers!

 

The rest of the program shows addition, multiplication and division.  Notice the symbols used.

 

 

PROGRAM 5 – MATH EXPRESSIONS & BEDMAS

·       Write a program that evaluates different expressions.  Show the effect of BEDMAS.

·       Show how to use brackets to specify an order of operations.

 

THE CODE

 

#BEDMAS forces the * to be first

x = 1 + 3 * 2      

print(x)

 

#Brackets force the + to be first

y = (1 + 3) * 2

print(y)

 

#BEDMAS forces the / to be first

z = 3 + 4 / 6 + 1

print(z)

 

EXPLANATION

 

BEDMAS is used to decide the order of operations.  Brackets need to be used to force a specific operation to be done first.

 

 

PROGRAM 6 – MORE ARITHMETIC (remainder, exponents)

·       Demonstrate the mod operator %.

·       Demonstrate the exponentiation operator **.

·       Recall that the square root is the same as exponent 0.5.

·       Write a program that outputs the remainder of 100 divided by 3. 

·       Then, at the bottom of the same program, calculate 28.

 

THE CODE

 

#Exponents

print(2**3)

print(2**10)

 

#Square root

print(100**0.5)

 

#Remainders (mod)

print(100 % 3)

print(7 % 5)

 

EXPLANATION

 

We can do exponents in Python by using the **.  So the value of 210 is written as 2**10.

 

The square root of a number is the same giving that number an exponent of 0.5.  So, 100**0.5 is 10.

 

Do you remember when divisions lead to an answer along with a remainder.  For example, 12 divided by 5 gave us 2 with a remainder of 2.  Oh the good ole days…

 

Well, if we want to know what the remainder would be for such a divisions, we can simply use the mod symbol %.  So, 12 % 5 gives 2.