www.umbc.edu
CMSC201 Computer Science I for Majors
Lecture 03 – Variables
- Prof. Katherine Gibson
- Prof. Jeremy Dixon
Based on slides by Shawn Lupoli and Max Morawski at UMBC
Lecture 03 Variables Prof. Katherine Gibson Prof. Jeremy Dixon - - PowerPoint PPT Presentation
CMSC201 Computer Science I for Majors Lecture 03 Variables Prof. Katherine Gibson Prof. Jeremy Dixon Based on slides by Shawn Lupoli and Max Morawski at UMBC www.umbc.edu Last Class We Covered Algorithms Program Development
www.umbc.edu
CMSC201 Computer Science I for Majors
Lecture 03 – Variables
Based on slides by Shawn Lupoli and Max Morawski at UMBC
www.umbc.edu
Last Class We Covered
– Sequential – Decision Making – Loops
– Syntax – Logic
2
www.umbc.edu
Any Questions from Last Time?
www.umbc.edu
Exercise
1.print("Hello') Error – Need to have matching ' and " 2.Print('Hello') Error – Need to have lowercase print 3.print('Hello World') Hello World
4
www.umbc.edu
Today’s Objectives
– How to use them – Different types
– To do interesting things with our program
5
www.umbc.edu
“Cowboy Coding”
–No formal management of project –No standard way of coding –Not planning things out
6
www.umbc.edu
Software Development Process
– Determine specifications (requirements)
7
www.umbc.edu
Example: Temperature Converter
You have been invited to live in Europe during a semester abroad. You aren’t sure how to dress because the temperature is given in Celsius.
–Temperature is given in Celsius
–Write a program to convert Celsius to Fahrenheit
8
www.umbc.edu
Input/Process/Output
– What information do you need for your converter?
– What formulas do you need for your converter?
– What is the output from your converter?
9
www.umbc.edu
Introduction to Python (Variables)
www.umbc.edu
Python
– General purpose – High-level language
– More streamlined than some other languages
11
www.umbc.edu
“Hello World!”
print("Hello World!")
#include <iostream> int main() { std::cout << "Hello, world!\n"; }
12
www.umbc.edu
Elements of a Program
– Variables – Modules (later in the semester) – Functions (later in the semester)
– Code that manipulates or evaluates identifiers
13
www.umbc.edu
We Start Python Today!
–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
14
We will write programs
www.umbc.edu
What Is a Variable?
– Can change (unlimited number of times)
“box” that you can put stuff in
15
www.umbc.edu
Rules for Naming Variables
– Uppercase letters (A-Z) – Lowercase letters (a-z) – Numbers (0-9) – Underscores (_)
– Special characters like $, #, &, ^, ), (, @
16
www.umbc.edu
More Rules for Naming Variables
– x – IsKanyeRunningForPresidentIn2020 – myName
– 2cool4school is not a valid variable – cool4school is a valid variable
17
www.umbc.edu
Variables and Keywords
– or is not a valid variable name – orange is an acceptable variable name
18
False class finally is return None continue for lambda try True def from nonlocal while and del global not with as elif if
yield assert else import pass break except in raise
www.umbc.edu
Exercise: Variables
19
1spam
No – Illegal!
raise1
Yes – legal!
Spam_And_Eggs
Yes – legal! But it doesn’t follow
spamAndEggs or spam_and_eggs
www.umbc.edu
Using Variables in Python
– Use the assignment operator (=)
richFiddy = 50000000 poorFiddy = 0.50 brokeFiddy = 0
20
assignment operator
www.umbc.edu
Introduction to Python (Expressions)
www.umbc.edu
Expressions
–Allows us to do interesting things
22
www.umbc.edu
Expressions Example
numCandy = 10 priceCandy = 0.50 totalCandy = numCandy * priceCandy
23
assignment operator variable being set value (a “literal”) expression
www.umbc.edu
Common Mistake
right hand sides of the assignment operator
24
numCandy = 10 10 = numCandy
www.umbc.edu
Variable Types
–Numbers
–Booleans (True and False) –Strings (collections of characters)
25
www.umbc.edu
Variables Types: Examples
aString = "Hello class" float_1 = 1.12 myBool = True anInteger = 7 dogName = "Mrs. Wuffington" classCode = 201
26
www.umbc.edu
Variable Usage
27
www.umbc.edu
Introduction to Python (Input and Output)
www.umbc.edu
Output
–So the user can see it and respond
– Use the keyword “print” and put what you want to be displayed in parentheses after it
29
www.umbc.edu
Output Example
print (3 + 4) print (3, 4, 3 + 4) print() print("The answer is", 3 + 4) 7 3 4 7 The answer is 7
30
www.umbc.edu
Output Exercise 1
a = 10 b = a * 5 c = "Your result is: " print(c, b) Your result is: 50
31
www.umbc.edu
Output Exercise 2
a = 10 b = a a = 3 print(b) 10
32
There are two possible
could do! Any guesses?
www.umbc.edu
Output Exercise 2 Explanation
they don’t become linked!
anything else to do with a
33
www.umbc.edu
Output Exercise 2 Explanation
34
a = 10 b = a a = 3 print(b)
10
a b
www.umbc.edu
Output Exercise 2 Explanation
35
a = 10 b = a a = 3 print(b)
10
a
10
b
www.umbc.edu
Output Exercise 2 Explanation
36
a = 10 b = a a = 3 print(b)
3
a
10
b
www.umbc.edu
Output Exercise 2 Explanation
37
a = 10 b = a a = 3 print(b)
3
a
10
b
www.umbc.edu
Input
– We must tell them what we want first
userNum = input("Please enter a number: ") print(userNum)
Please enter a number: 10
38
10
www.umbc.edu
How Input Works
userNum = input("Please enter a number: ")
– In the variable named userNum
userNum = input("Enter another number: ") userNum2 = input("Enter a new number: ") userAge = input("Please enter your age: ")
39
www.umbc.edu
Input as a String
will come in the form of a string
– "10" is a two character long string – 10 is understood by Python as a number
40
www.umbc.edu
Converting from String
you can do the following:
aNum = input("Enter a number: ") aNum = int(aNum)
aNum = int(input("Enter a number: "))
41
www.umbc.edu
Class Exercise: Mad Libs
where one player prompts others for a list
before reading the – often comical or nonsensical – story aloud
party game or as a pastime
42
www.umbc.edu
Exercise: Calculating Averages
program that asks the user for two numbers and prints out the average.
input from the user!
for this exercise?
43
www.umbc.edu
Exercise: Assignment Weighting
someone’s weight grade. You have so far: hwWeight = 0.4 examWeight = 0.5 discussionWeight = 0.1
homework grade, exam grade, and discussion grade and prints out their total grade in the class.
44
www.umbc.edu
Announcements
– Go to your scheduled location and time
– Due by next Monday (Feb 8th) at 8:59:59 PM – You must have completed the Syllabus Quiz to see it
– Must complete to see Homework 2 next week
45
www.umbc.edu
Practice Problem
and you are trying to figure out if you should take the train or drive.
– Taking the train costs $4.90 each way – It is 75 miles round trip, your car averages 28 mpg, and gas is $2.03 per gallon plus $0.06 per mile in maintenance. Your new work has free parking, so you don’t need to pay.
drive to work, and print out if it is cheaper to drive to work, or to take the train. By how much?
46