SlideShare a Scribd company logo
Variables and
Expressions
CMSC 201
Chang (rev. 2015-02-05)
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.
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.
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.
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
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!
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)
Exercise
What will the following code snippet print?
a = 10
b = a * 5
c = "Your result is: "
print(c, b)
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?
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.
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
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.")
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
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
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
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
Exercise
Write, on paper or on your computer, a program that asks
the user for two numbers a prints out the average.
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.

More Related Content

PDF
pythonQuick.pdf
PDF
python notes.pdf
PDF
python 34💭.pdf
PPT
Introduction to C++
PPTX
Python_Unit-1_PPT_Data Types.pptx
PPT
Python-review1 for begineers to code.ppt
PDF
Python-review1.pdf
PPT
Python-review1.ppt
pythonQuick.pdf
python notes.pdf
python 34💭.pdf
Introduction to C++
Python_Unit-1_PPT_Data Types.pptx
Python-review1 for begineers to code.ppt
Python-review1.pdf
Python-review1.ppt

Similar to 03 Variables - Chang.pptx (20)

PDF
‘How to develop Pythonic coding rather than Python coding – Logic Perspective’
PDF
Python Training Course in Chandigarh(Mohali)
PDF
Python Training in Chandigarh(Mohali)
PPTX
lecture 2.pptx
PPT
Python-review1.ppt
PPTX
Python programing
PPTX
made it easy: python quick reference for beginners
PPTX
Learn about Python power point presentation
PPT
Lập trình C
PPTX
Bikalpa_Thapa_Python_Programming_(Basics).pptx
PPT
introduction to python in english presentation file
PDF
Python Unit 3 - Control Flow and Functions
PPT
PE1 Module 2.ppt
PPTX
Each n Every topic of C Programming.pptx
PPTX
MODULE. .pptx
PPTX
Introduction to learn and Python Interpreter
PPS
C programming session 02
DOCX
C++ Tutorial.docx
PPTX
Python Lecture 4
PPTX
UNIT – 3.pptx for first year engineering
‘How to develop Pythonic coding rather than Python coding – Logic Perspective’
Python Training Course in Chandigarh(Mohali)
Python Training in Chandigarh(Mohali)
lecture 2.pptx
Python-review1.ppt
Python programing
made it easy: python quick reference for beginners
Learn about Python power point presentation
Lập trình C
Bikalpa_Thapa_Python_Programming_(Basics).pptx
introduction to python in english presentation file
Python Unit 3 - Control Flow and Functions
PE1 Module 2.ppt
Each n Every topic of C Programming.pptx
MODULE. .pptx
Introduction to learn and Python Interpreter
C programming session 02
C++ Tutorial.docx
Python Lecture 4
UNIT – 3.pptx for first year engineering

Recently uploaded (20)

PPTX
Global journeys: estimating international migration
PPTX
1_Introduction to advance data techniques.pptx
PPTX
ALIMENTARY AND BILIARY CONDITIONS 3-1.pptx
PPTX
The THESIS FINAL-DEFENSE-PRESENTATION.pptx
PPTX
IB Computer Science - Internal Assessment.pptx
PPTX
Introduction-to-Cloud-ComputingFinal.pptx
PDF
“Getting Started with Data Analytics Using R – Concepts, Tools & Case Studies”
PPTX
Supervised vs unsupervised machine learning algorithms
PDF
BF and FI - Blockchain, fintech and Financial Innovation Lesson 2.pdf
PPT
Chapter 2 METAL FORMINGhhhhhhhjjjjmmmmmmmmm
PDF
Fluorescence-microscope_Botany_detailed content
PPT
Reliability_Chapter_ presentation 1221.5784
PPTX
STUDY DESIGN details- Lt Col Maksud (21).pptx
PDF
168300704-gasification-ppt.pdfhghhhsjsjhsuxush
PPTX
05. PRACTICAL GUIDE TO MICROSOFT EXCEL.pptx
PPT
Quality review (1)_presentation of this 21
PPTX
mbdjdhjjodule 5-1 rhfhhfjtjjhafbrhfnfbbfnb
PPTX
Introduction to Knowledge Engineering Part 1
PPTX
Business Ppt On Nestle.pptx huunnnhhgfvu
PDF
22.Patil - Early prediction of Alzheimer’s disease using convolutional neural...
Global journeys: estimating international migration
1_Introduction to advance data techniques.pptx
ALIMENTARY AND BILIARY CONDITIONS 3-1.pptx
The THESIS FINAL-DEFENSE-PRESENTATION.pptx
IB Computer Science - Internal Assessment.pptx
Introduction-to-Cloud-ComputingFinal.pptx
“Getting Started with Data Analytics Using R – Concepts, Tools & Case Studies”
Supervised vs unsupervised machine learning algorithms
BF and FI - Blockchain, fintech and Financial Innovation Lesson 2.pdf
Chapter 2 METAL FORMINGhhhhhhhjjjjmmmmmmmmm
Fluorescence-microscope_Botany_detailed content
Reliability_Chapter_ presentation 1221.5784
STUDY DESIGN details- Lt Col Maksud (21).pptx
168300704-gasification-ppt.pdfhghhhsjsjhsuxush
05. PRACTICAL GUIDE TO MICROSOFT EXCEL.pptx
Quality review (1)_presentation of this 21
mbdjdhjjodule 5-1 rhfhhfjtjjhafbrhfnfbbfnb
Introduction to Knowledge Engineering Part 1
Business Ppt On Nestle.pptx huunnnhhgfvu
22.Patil - Early prediction of Alzheimer’s disease using convolutional neural...

03 Variables - Chang.pptx

  • 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. 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)
  • 8. Exercise What will the following code snippet print? a = 10 b = a * 5 c = "Your result is: " print(c, b)
  • 9. 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?
  • 10. 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.
  • 11. 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
  • 12. 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.")
  • 13. 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
  • 14. 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
  • 15. 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
  • 16. 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
  • 17. Exercise Write, on paper or on your computer, a program that asks the user for two numbers a prints out the average.
  • 18. 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.