![]() | ![]() |
The rest of the illustrations will assume you are running the python programs in a Windows environment.
I. Start IDLE
II. Navigate to the File menu and click New Window
III. Type the following:
print (“Hello World!”)
IV. On the file, menu click on Save. Type the name of myProgram1.py
V. Navigate to Run and click Run Module to run the program.
The first program that we have written is known as the “Hello World!” and is used to not only provide an introduction to a new computer coding language but also test the basic configuration of the IDE. The output of the program is “Hello World!” Here is what has happened, the Print() is an inbuilt function, it is prewritten and preloaded for you, is used to display whatever is contained in the () as long as it is between the double quotes. The computer will display anything written within the double quotes.
Practice Exercise
Now write and run the following python programs:
a. print(“I am now a Python Language Coder!”)
b. print(“This is my second simple program!”)
c. print(“I love the simplicity of Python”)
d. print(“I will display whatever is here in quotes such as owyhen2589gdbnz082”)
Now we need to write a program with numbers, but before writing such a program, we need to learn something about Variables and Types.
Remember python is object-oriented and it is not statically typed which means we do not need to declare variables before using them or specify their type. Let us explain this statement; an object-oriented language simply means that the language supports viewing and manipulating real-life scenarios as groups with subgroups that can be linked and shared mimicking the natural order and interaction of things. Not all programming languages are object-oriented; for instance, Visual C programming language is not object-oriented. In programming, declaring variables means that we explicitly state the nature of the variable. The variable can be declared as an integer, long integer, short integer, floating integer, a string, or as a character including if it is accessible locally or globally. A variable is a storage location that changes values depending on conditions.
For instance, number1 can take any number from 0 to infinity. However, if we specify explicitly that int number1 it then means that the storage location will only accept integers and not fractions for instance, fortunately or unfortunately, python does not require us to explicitly state the nature of the storage location (declare variables) as that is left to the python language itself to figure out that.
Before tackling types of variables and rules of writing variables, let us run a simple program to understand what variables when coding a python program are.
I. Start IDLE
II. Navigate to the File menu and click New Window
III. Type the following:
num1=4
num2=5
sum=num1+num2
print(sum)
IV. On the file, menu click on Save. Type the name of myProgram2.py
V. Navigate to Run and click Run Module to run the program.
The expected output of this program should be “9” without the double quotes.
Discussion
At this point, you are eager to understand what has just happened and why the print(sum) does not have double quotes like the first programs we wrote. Here is the explanation.
The first line num1=4 means that variable num1(our shortened way of writing number1, first number) has been assigned 4 before the program runs.
The second line num2=5 means that variable num2(our shortened way of writing number2, second number) has been assigned 5 before the program runs.
The computer interprets these instructions and stores the numbers given
The third line sum=num1+num2 tells the computer that takes whatever num1 has been given and add to whatever num2 has been given. In other terms, sum the values of num1 and num2.
The fourth line print(sum) means that display whatever sum has. If we put double quotes to sum, the computer will display the word sum and not the sum of the two numbers! Remember that cliché that computers are garbage in and garbage out. They follow what you give them!
Point to Note
+ is an operator for summing variables and has other users that will be discussed later.
Now let us try out three practice exercises involving numbers before we explain types of variables and rules of writing variables so that you get more freedom to play with variables. Remember variables values vary for instance num1 can take 3, 8, 1562, 1.
Follow the steps of opening the Python IDE and do the following:
a) The output should be 54
num1=43
num2=11
sum=num1+num2
print(sum)
b) The output should be 167
num1=101
num2=66
sum=num1+num2
print(sum)
c) The output should be 28
num1=9
num2=19
sum=num1+num2
print(sum)