Presentation is loading. Please wait.

Presentation is loading. Please wait.

Variables and Expressions CMSC 201 Chang (rev. 2015-02-05)

Similar presentations


Presentation on theme: "Variables and Expressions CMSC 201 Chang (rev. 2015-02-05)"— Presentation transcript:

1 Variables and Expressions CMSC 201 Chang (rev. 2015-02-05)

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

3 Variables Variables are names of places that store information. Making variables in Python: someVariable = 10 someVariable is a variable. someVariable has value 10. someVariable can have a different value later. The = is called the assignment operator. It allows you to change the value of a variable.

4 Variables The right hand side of the assignment can be any mathematical expression. someVariable = 10 anotherVariable = 5 + 9 aThirdVariable = 10 * anotherVariable someVariable = 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.

5 Types There are many different types of values ! Numbers (integer or floating point) True / False values (called booleans) Strings (collections of characters) aString = ‘Hello everyone’ x = 1.12 bool = True

6 Rules for Naming Variables Variable names are case sensitive. Hello different from hello Only may contain alphabetic letters, underscores or numbers. Should not start with a number. Cannot be any other python keyword (if, while, def, etc). Give your variables meaningful names!

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

8 Output We’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 = 10 print(someVariable) You can even do combinations! print("Your variable is ", someVariable)

9 Exercise What will the following code snippet print? a = 10 b = a * 5 c = "Your result is: " print(c, b)

10 Exercise What will the following code snippet print? a = 10 b = a a = 3 print(b) There are two possible options for what this could do! Any guesses?

11 Exercise a = 10 b = a a = 3 print(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.

12 Input Sometimes, 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 10 10

13 Input This 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("Enter first number.") userInput2 = input("Enter second number.")

14 Input Note: All input vlaues are strings. a = "10" b = 10 Variables a and b are different. To turn an input into a number: stringInput = input("Enter a number: ") aNumber = int(stringInput) int stands for integer

15 Operator Precedence Like algebra, multiply and divide before adding and subtracting: 3 * 4 + 5 is 17, not 27. Python has many more operators. Precedence allows this expression to make sense without parentheses: 3 * 4 + 5 > 2 + 2 * 4 and 17 + 9 > 31 - 22

16 Associativity Associativity does not hold (4.05 - 4.05) + 0.0000000000000001 does not equal 4.05 – (4.05 + 0.0000000000000001) are not the same. Most operations associate left to right by default: a + b + c means (a + b) + c

17 More operators Exponentiation: 2**3 is 8 2.5**2 is 6.25 Integer division (rounds down): 5//2 is 2 Modulus (gives the remainder): 28 % 5 is 3

18 Exercise Write, on paper or on your computer, a program that asks the user for two numbers a prints out the average.

19 Exercise Pretend you’re writing a program to compute someone’s weighted grade. You have so far: hwWeight = 0.4 examWeight = 0.5 discussionWeight = 0.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.


Download ppt "Variables and Expressions CMSC 201 Chang (rev. 2015-02-05)"

Similar presentations


Ads by Google