variables in python · 03/05/2020 · copy and paste the below code into your python editor....
TRANSCRIPT
Variables
in Python
Powered by AFRL NM STEM Academy
• The first line of this code creates a variable called “bignumber” to store a number.
• The second line creates a variable named “food” to store text or a string of characters.
bignumber = 3452962food = "pancakes"
A variable is a container that can store different kinds of information.
Variables
bignumber = 3452962food = "pancakes"
Notice that commas are not used in the number.If 2 commas were used, the number would be split into 3 separate numbers.
Variables
When you want to use text, or a string of characters, use quotation marks. They can be single or double quotation marks, as long as they match.
print("bignumber =", bignumber)print("food = ", food)print("Bring me", bignumber, food)
The print function can be used to display information stored in variables.
Variables
Function parameters are enclosed in parenthesis.If there is more than one parameter, they’re separated with commas.
# Variables Example 1bignumber = 3452962food = "pancakes"print("bignumber =", bignumber)print("food = ", food)print("Bring me", bignumber, food)
Copy and paste the below code into your Python editor.
Variables
Save your code and click on Run.If the Stop button is visible, click on it and then click on Run.
Your program and output should look like the following:
Variables
smallnumber = 7bignumber = smallnumber
Variables are always open to be changed.
Variables
Here, “bignumber” is replaced with 7 or whatever value is stored in “smallnumber”.
sweetOfTheDay = "donuts"food = food + ' and ' + sweetOfTheDay
Two variables and the word “and” are assembled with plus symbols to replace “pancakes” with “pancakes and donuts”.
# Variables Example 2bignumber = 3452962food = "pancakes"smallnumber = 7bignumber = smallnumbersweetOfTheDay = "donuts"food = food + ' and ' + sweetOfTheDayprint("Bring me", bignumber, food)
Copy and paste the below code into your Python editor.
Variables
Save your code and click on Run.If the Stop button is visible, click on it and then click on Run.
Take some time to make your own program that will create variables and print their values.
Variables
# Variable Example 3jeans = 30shoes = 30shirt = 10total = jeans + shoes + shirtprint('New clothes will cost', total, "dollars.")
Now let’s do a little bit of math.
Variables
Copy, paste and run the below program.
# Variable Example 3jeans = 30shoes = 30shirt = 10total = jeans + shoes + shirtprint('New clothes will cost', total, "dollars.")tax = .1totalTax = (jeans + shoes + shirt) * taxtotal = total + totalTaxprint('New clothes will cost', total, "dollars.")
We forgot to include a sales tax. We’ll use a 10% sales tax.
Variables
Copy, paste and run the below program.
In this program we are using two different types of numbers. The clothing prices are all integers or whole numbers and the tax rate has a decimal place. Numbers that have a decimal place are called float, which is short for floating point. The “total” variable is transformed from integer to float after the calculation.
Variables
totalTax = (jeans + shoes + shirt) * taxtotal = total + totalTax
Computer programmers must be aware of the order of operations rule in math. In our calculation, multiplication and division will be done before addition and subtraction unless parenthesis are used to change the order.
In the code below, we need to add the cost of the 3 items before we multiply by the tax rate. If you need to be convinced that parenthesis are needed, take the parenthesis out of example 3 and run it again.
Variables
You should see that the total tax and total would be huge.
Now, try to come up with another, possibly simpler way to write the below code to calculate the total bill with sales tax. Include a new print function that will display all variables.
Variables
# Variable Example 3jeans = 30shoes = 30shirt = 10total = jeans + shoes + shirtprint('New clothes will cost', total, "dollars.")tax = .1totalTax = (jeans + shoes + shirt) * taxtotal = total + totalTaxprint('New clothes will cost', total, "dollars.")
Here is one alternative solution. There are usually a variety of solutions to any coding challenge.
Variables
There are some rules related to creating variable names.
• Variable names cannot use spaces. The underscore is a good substitute for spaces. So, myVariable or my_variable are good examples of variable names.
• Python is case sensitive. In other words, Jeans and jeans are not the same variable name.
• Variable names cannot use a number for the first character. The name “7up” would generate an error. A better name choice would be “seven_up”.
• Try to use meaningful names whenever possible.
Variable Names
The below program will generate an error. Copy, paste and run it. Then try to fix the mistake created.
Variables
# Variables Example 4softdrink = "Coca Cola"print(SoftDrink)
VariablesWrite a program that uses variables to complete a sentence as shown below. Use at least one number and a variable called “name” that will include your name in the sentence.
Congratulations!
You now have a basic knowledge of using variables in Python.