variables and expressions cmsc 201. today we start python! two ways to use python: you can write a...
TRANSCRIPT
![Page 1: Variables and Expressions CMSC 201. Today we start Python! Two ways to use python: You can write a program, as a series of instructions in a file, and](https://reader036.vdocuments.mx/reader036/viewer/2022082613/5697bff61a28abf838cbe0e1/html5/thumbnails/1.jpg)
Variables and Expressions
CMSC 201
![Page 2: Variables and Expressions CMSC 201. Today we start Python! Two ways to use python: You can write a program, as a series of instructions in a file, and](https://reader036.vdocuments.mx/reader036/viewer/2022082613/5697bff61a28abf838cbe0e1/html5/thumbnails/2.jpg)
Today we start Python!Two ways to use python: You can write a program, as a series of
instructions in a file, and then execute it. You can also test simple python commands in
the python interpreter.
![Page 3: Variables and Expressions CMSC 201. Today we start Python! Two ways to use python: You can write a program, as a series of instructions in a file, and](https://reader036.vdocuments.mx/reader036/viewer/2022082613/5697bff61a28abf838cbe0e1/html5/thumbnails/3.jpg)
Variables Last class we talked about variables being places with names that store information. In python, you can make a variable like this:
someVariable = 10
This creates a variable, called someVariable, that has the value of 10. This value can change later on.
The equals sign is called the assignement operator. It allows you to change the value of a variable.
![Page 4: Variables and Expressions CMSC 201. Today we start Python! Two ways to use python: You can write a program, as a series of instructions in a file, and](https://reader036.vdocuments.mx/reader036/viewer/2022082613/5697bff61a28abf838cbe0e1/html5/thumbnails/4.jpg)
Variables The right hand side of the assignment can be any mathematical expression. For example!
someVariable = 10anotherVariable= 5 + 9aThirdVariable= 10 * anotherVariablesomeVariable = someVariable + anotherVariable
Whenever a variable is on the right hand side of an expression, imagine it being replaced by whatever value is currently stored in that variable.
![Page 5: Variables and Expressions CMSC 201. Today we start Python! Two ways to use python: You can write a program, as a series of instructions in a file, and](https://reader036.vdocuments.mx/reader036/viewer/2022082613/5697bff61a28abf838cbe0e1/html5/thumbnails/5.jpg)
VariablesThere are many different kinds of variables!
Numbers (Integers or decimals) True / False values (called booleans) Strings (collections of characters)
aString = ‘Hello everyone’decimal = 1.12bool = True
![Page 6: Variables and Expressions CMSC 201. Today we start Python! Two ways to use python: You can write a program, as a series of instructions in a file, and](https://reader036.vdocuments.mx/reader036/viewer/2022082613/5697bff61a28abf838cbe0e1/html5/thumbnails/6.jpg)
Rules for Naming Variables
We have certain rules about what a variable can be named:
Variable names are case sensitive. A variable named Hello and a variable named hello are regarded as different.
Only may contain alphabetic letters, underscores or numbers.
Should not start with a number. Cannot be any other python keyword (if,
while, def, etc).
You should always attempt to give your variables meaningful names!
![Page 7: Variables and Expressions CMSC 201. Today we start Python! Two ways to use python: You can write a program, as a series of instructions in a file, and](https://reader036.vdocuments.mx/reader036/viewer/2022082613/5697bff61a28abf838cbe0e1/html5/thumbnails/7.jpg)
When do I make variables?
Variables are designed for storing information. Going back to our averaging example from earlier, when we were summing up the list we needed a place to put that sum as it was being generated.
Any piece of information you wish your program to use or record must be stored in a variable. Think of it as giving that piece of information a name.
![Page 8: Variables and Expressions CMSC 201. Today we start Python! Two ways to use python: You can write a program, as a series of instructions in a file, and](https://reader036.vdocuments.mx/reader036/viewer/2022082613/5697bff61a28abf838cbe0e1/html5/thumbnails/8.jpg)
OutputWe’d like to see what’s stored in our variables!
Python can print things for us:
print(“Hello world”)
You can also output the contents of variables:
someVariable = 10print(someVariable)
You can even do combinations!
print(“Your variable is “, someVariable)
![Page 9: Variables and Expressions CMSC 201. Today we start Python! Two ways to use python: You can write a program, as a series of instructions in a file, and](https://reader036.vdocuments.mx/reader036/viewer/2022082613/5697bff61a28abf838cbe0e1/html5/thumbnails/9.jpg)
ExerciseWhat will the following code snippet print?
a = 10b = a * 5c = “Your result is: “print(c, b)
![Page 10: Variables and Expressions CMSC 201. Today we start Python! Two ways to use python: You can write a program, as a series of instructions in a file, and](https://reader036.vdocuments.mx/reader036/viewer/2022082613/5697bff61a28abf838cbe0e1/html5/thumbnails/10.jpg)
ExerciseWhat will the following code snippet print?
a = 10b = aa = 3print(b)
There are two possible options for what this could do! Any guesses?
![Page 11: Variables and Expressions CMSC 201. Today we start Python! Two ways to use python: You can write a program, as a series of instructions in a file, and](https://reader036.vdocuments.mx/reader036/viewer/2022082613/5697bff61a28abf838cbe0e1/html5/thumbnails/11.jpg)
Exercise
a = 10b = aa = 3print(b)
It will print out 10. When you set one variable equal to another, they don’t become linked; b is set to 10 and no longer has anything else to do with a.
![Page 12: Variables and Expressions CMSC 201. Today we start Python! Two ways to use python: You can write a program, as a series of instructions in a file, and](https://reader036.vdocuments.mx/reader036/viewer/2022082613/5697bff61a28abf838cbe0e1/html5/thumbnails/12.jpg)
InputSometimes, we’d like the user to participate! We can also get input from the user.
userInput = input(“Please enter a number “)print(userInput)
The output will look like this:
Please enter a number 1010
![Page 13: Variables and Expressions CMSC 201. Today we start Python! Two ways to use python: You can write a program, as a series of instructions in a file, and](https://reader036.vdocuments.mx/reader036/viewer/2022082613/5697bff61a28abf838cbe0e1/html5/thumbnails/13.jpg)
InputThis line:
userInput = input(“Please enter a number “)
Takes whatever the user entered and stores it in the variable named “userInput”
You can do this as many times as you like!
userInput1 = input(“Please enter your first number.”)userInput2 = input(“Please enter your second number.”)
![Page 14: Variables and Expressions CMSC 201. Today we start Python! Two ways to use python: You can write a program, as a series of instructions in a file, and](https://reader036.vdocuments.mx/reader036/viewer/2022082613/5697bff61a28abf838cbe0e1/html5/thumbnails/14.jpg)
InputNote: All input will come as a string, a series of characters. There is a difference between “10” and the number 10. “10” is composed of two characters stuck together, while python understands 10 as a number.
To turn an input into a number, you can do the following:
someNumber = input(“Please enter a number: “)someNumber = int(someNumber)
int stands for integer
![Page 15: Variables and Expressions CMSC 201. Today we start Python! Two ways to use python: You can write a program, as a series of instructions in a file, and](https://reader036.vdocuments.mx/reader036/viewer/2022082613/5697bff61a28abf838cbe0e1/html5/thumbnails/15.jpg)
ExerciseWrite, on paper or on your computer, a program that asks the user for two numbers a prints out the average.
![Page 16: Variables and Expressions CMSC 201. Today we start Python! Two ways to use python: You can write a program, as a series of instructions in a file, and](https://reader036.vdocuments.mx/reader036/viewer/2022082613/5697bff61a28abf838cbe0e1/html5/thumbnails/16.jpg)
ExercisePretend you’re writing a program to compute someone’s weight grade. You have so far:
hwWeight = .4examWeight = .5discussionWeight = .1
Write a program starting with these three lines that asks the user for their homework grade, exam grades, and discussion grades and prints out their total grade in the class.