Exercise on Variables


EXERCISE

Write a program that calculates the area of a square and prints the area of the square.

Try your hands on this exercise before looking at the solution below.

SOLUTION

In this program, we will create two variables named length and width and store the value of our preference in the variables; in this case, I chose 4 and 6. We could have used actual variables like x and y for the length and width, but when the program becomes more complex, it may seem confusing.

Let's save our program using Command S or Control S and run our program to see if it is working. 







And it is working. We can also change the value of our variables and run it by pressing F5.
 

















You can also do similar operations using the division sign (/), addition sign (-), etc.
 
Our program is too static; anytime we want to change a variable, we have to change something in the code. In our next tutorial, we will learn how to make our program more dynamic. We will learn how to assign user input to a variable.

SUMMARY
print("This program calculates the area of a square")
length = 6
width = 10
print("The length and width of the square are", length, "and", width, "respectively")
print("The area of the square is:", length * width)

Output:
This program calculates the area of a square
The length and width of the square are 6 and 10 respectively
The area of the square is: 60
>>> 











Beginner python Tutorial 3

Variables

Variables are used to store information in Python, and that information can be anything—a line of text, the name of a person, or a number. Several data types can be stored in a variable, which we will talk about later in this course.

Examples of variables:



In this code, age is the variable, and the equal sign shows it has been assigned the value of 22, so anytime we mention age in our code or print the variable age, we know it is going to output the value it has stored, which is 22. Print the variable age and run your code to see the results below in the Python interpreter.







We can also do some more coding in Python Interpreter. 













As you can see above, if you want to change the value of the variable from 22 to 33, just assign the variable to your preferred value and print it. The red syntax you see with the # sign is a comment.

Let's look at some feasible rules before we end this tutorial.

  • In our next toturial we are going to do some exercises on variables good luck!



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!


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...