Beginner Python Tutorial _2

 Statements

Let's open our Python IDLE and start with statements. One of the functions we will be using the most is the print() function. You can check for more Python-built-in functions and how they operate at https://docs.python.org/3/library/functions.html

 
Alright Let's get into our program:
We want to display the hello world message so we use double quotation marks inside parenthesis to output our message Hello word!

print("Hello World")
Hello world
This message is a statement, it's an instruction for our computer to execute.

In your Python Terminal, go to file and create a new file, that is where you are going to be writing your Python commands and it is going to be executed in the Python terminal (>>>)




After typing our statement in the file as shown below, we can hit F5 or run from the menu bar to run your program. NB. You will be asked to save the program file before running so you can create a folder on your desktop and name it "MyfirstPythonCode."  After that, save this statement exercise as statement.py in you folder and run your program.

In our Python interpreter, Our results look like this


Let's try some more operations with Python;

let's create a program that calculates the average of two numbers. 


This is what our program looks like; let's save it as average. py in our MyFirstPythonCode folder to keep our files organized.


  • Run it!


As you can see Our code is well executed! 

print("This program calculates the average of two numbers")
print("The numbers are 3 and 5 ")
print("The average is: ", (3 + 5) / 2 )

output:

This program calculates the average of two numbers
The numbers are 3 and 5 
The average is:  4.0
>>>
Let's make our program more dynamic and interactive to the user in our next tutorial. Happy Coding!


No comments:

Post a Comment

Python Programming Tutorial 15

 Error Handling Anytime we work with user input, we should make sure not only the data is valid and within the range we want, like what we d...