![Page 1: Variables in Python · 03/05/2020 · 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](https://reader034.vdocument.in/reader034/viewer/2022050122/5f52a14f577dc6230f2fa267/html5/thumbnails/1.jpg)
Variables
in Python
Powered by AFRL NM STEM Academy
![Page 2: Variables in Python · 03/05/2020 · 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](https://reader034.vdocument.in/reader034/viewer/2022050122/5f52a14f577dc6230f2fa267/html5/thumbnails/2.jpg)
• 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
![Page 3: Variables in Python · 03/05/2020 · 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](https://reader034.vdocument.in/reader034/viewer/2022050122/5f52a14f577dc6230f2fa267/html5/thumbnails/3.jpg)
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.
![Page 4: Variables in Python · 03/05/2020 · 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](https://reader034.vdocument.in/reader034/viewer/2022050122/5f52a14f577dc6230f2fa267/html5/thumbnails/4.jpg)
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.
![Page 5: Variables in Python · 03/05/2020 · 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](https://reader034.vdocument.in/reader034/viewer/2022050122/5f52a14f577dc6230f2fa267/html5/thumbnails/5.jpg)
# 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.
![Page 6: Variables in Python · 03/05/2020 · 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](https://reader034.vdocument.in/reader034/viewer/2022050122/5f52a14f577dc6230f2fa267/html5/thumbnails/6.jpg)
Your program and output should look like the following:
Variables
![Page 7: Variables in Python · 03/05/2020 · 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](https://reader034.vdocument.in/reader034/viewer/2022050122/5f52a14f577dc6230f2fa267/html5/thumbnails/7.jpg)
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”.
![Page 8: Variables in Python · 03/05/2020 · 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](https://reader034.vdocument.in/reader034/viewer/2022050122/5f52a14f577dc6230f2fa267/html5/thumbnails/8.jpg)
# 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.
![Page 9: Variables in Python · 03/05/2020 · 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](https://reader034.vdocument.in/reader034/viewer/2022050122/5f52a14f577dc6230f2fa267/html5/thumbnails/9.jpg)
Take some time to make your own program that will create variables and print their values.
Variables
![Page 10: Variables in Python · 03/05/2020 · 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](https://reader034.vdocument.in/reader034/viewer/2022050122/5f52a14f577dc6230f2fa267/html5/thumbnails/10.jpg)
# 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.
![Page 11: Variables in Python · 03/05/2020 · 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](https://reader034.vdocument.in/reader034/viewer/2022050122/5f52a14f577dc6230f2fa267/html5/thumbnails/11.jpg)
# 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.
![Page 12: Variables in Python · 03/05/2020 · 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](https://reader034.vdocument.in/reader034/viewer/2022050122/5f52a14f577dc6230f2fa267/html5/thumbnails/12.jpg)
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
![Page 13: Variables in Python · 03/05/2020 · 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](https://reader034.vdocument.in/reader034/viewer/2022050122/5f52a14f577dc6230f2fa267/html5/thumbnails/13.jpg)
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.
![Page 14: Variables in Python · 03/05/2020 · 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](https://reader034.vdocument.in/reader034/viewer/2022050122/5f52a14f577dc6230f2fa267/html5/thumbnails/14.jpg)
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.")
![Page 15: Variables in Python · 03/05/2020 · 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](https://reader034.vdocument.in/reader034/viewer/2022050122/5f52a14f577dc6230f2fa267/html5/thumbnails/15.jpg)
Here is one alternative solution. There are usually a variety of solutions to any coding challenge.
Variables
![Page 16: Variables in Python · 03/05/2020 · 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](https://reader034.vdocument.in/reader034/viewer/2022050122/5f52a14f577dc6230f2fa267/html5/thumbnails/16.jpg)
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
![Page 17: Variables in Python · 03/05/2020 · 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](https://reader034.vdocument.in/reader034/viewer/2022050122/5f52a14f577dc6230f2fa267/html5/thumbnails/17.jpg)
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)
![Page 18: Variables in Python · 03/05/2020 · 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](https://reader034.vdocument.in/reader034/viewer/2022050122/5f52a14f577dc6230f2fa267/html5/thumbnails/18.jpg)
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.
![Page 19: Variables in Python · 03/05/2020 · 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](https://reader034.vdocument.in/reader034/viewer/2022050122/5f52a14f577dc6230f2fa267/html5/thumbnails/19.jpg)
Congratulations!
You now have a basic knowledge of using variables in Python.