YCL Week 3 Lets talk about variables! Variables Variables are - - PowerPoint PPT Presentation

ycl week 3 let s talk about variables variables
SMART_READER_LITE
LIVE PREVIEW

YCL Week 3 Lets talk about variables! Variables Variables are - - PowerPoint PPT Presentation

YCL Week 3 Lets talk about variables! Variables Variables are containers for data. Variables have two parts: a name and a value, which is of a certain data type. A variables value is usually assigned to it using an equal sign. x = 5


slide-1
SLIDE 1

YCL Week 3 Let’s talk about variables!

slide-2
SLIDE 2

Variables

Variables are containers for data. Variables have two parts: a name and a value, which is of a certain data type. A variable’s value is usually “assigned” to it using an equal sign. x = 5 print(x)

slide-3
SLIDE 3

Data Types

  • 1. Numerical Types
  • 1. Integers aka int: positive/negative whole number. Example: 5
  • 2. floating-point : any number with a decimal point, most like decimals.

Example: 3.14

  • 3. Long
  • 4. Complex Numbers
  • 2. Non-numerical types:
  • 1. String: set of characters. Example: “Hello World!”
  • 2. List: container that holds a number of other objects, in a given order.
  • 3. Tuple
  • 4. Dictionary(or "dict")
slide-4
SLIDE 4

Naming Variables

Good Names:

  • temperature_in_celsius
  • tree_position
  • car_speed
  • number_of_children
  • simpson

Legal, But Bad Names:

  • temperatureInCelsius [Uses capital letters. Keep it lower case and use underscores.]
  • x [Too short, and not descriptive.]
  • Simpson [Starts with a capital letter.]

Illegal Names:

  • tree position - you cannot use spaces
  • 4runner - you cannot start with a number
slide-5
SLIDE 5

Naming Example(s)

# Calculate mpg using confusing variable names m = 294 g = 10.5 m2 = m / g print(m2) # Calculate mpg using good variable names miles_driven = 294 gallons_used = 10.5 mpg = miles_driven / gallons_used print(mpg)

slide-6
SLIDE 6

Constants

PI = 3.14159 SCREEN_WIDTH = 600 RED = (255, 0 ,0)

slide-7
SLIDE 7

Operators

Operator Description + Addition

  • Subtraction

* Multiplication / Division // Integer division (rounds down)

slide-8
SLIDE 8

PEMDAS

  • Parentheses
  • Exponent
  • Multiplication
  • Division
  • Addition
  • Subtraction
slide-9
SLIDE 9

Different from Math

There are two things that don’t work like you’d expect:

  • 1. There is no “juxtaposition” used to multiply

items.

  • 2. The = is not an algebraic equality.
slide-10
SLIDE 10

= does not mean equal

# The last two lines will error x = 3 y = 2x z = 2(3 + x)

You can rewrite the code above to work by explicitly multiplying: # This code works. Although it doesn't print anything. x = 3 y = 2 * x z = 2 * (3 + x)

slide-11
SLIDE 11

Different from Math

The = evaluates what is on the right, and puts it in the variable on the left. For example: # This works x = 3 + 4 x = 8 The code above does not signify equality; it is “assigning” the value of 7 to x. Later on, we could assign the value 8 to it, and x would print this new value. What would happen if we then assigned x a value of 9 and printed x, i.e. print(x)?

slide-12
SLIDE 12

Counter Variables

x = 7 x = x + 1 print(x) What will this print?

slide-13
SLIDE 13

calculator.py: Part 1

Write a program that prints out the results of at least one calculation for each of the basic

  • perations: addition, subtraction, multiplication, division, and exponents. Use integers.

HINT: Type the word print and put whatever expression you would like to print in

  • parentheses. This is a review of the print statement and introduces the printing of integers.

Remember, this is not a variable assignment (we are not assigning a value to anything). 1. Example solution: 1. print(6 + 4) 2. print(12 - 2) 3. print(2 * 5) 4. print(30 / 3)

slide-14
SLIDE 14

calculator.py: Part 2

Store at least 4 integers into 4 different variables, and use these variables to perform

  • perations within your print statement.

HINT: 1. Assign var1 a value of ‘3’. Do this by writing var1=3. 2. Assign variable 2 a value of ‘2’. Do this by writing var2=2. 3. Add the variables together, and print the result. Do this by writing print(var1+var2). 4. Save and run. What do you get?

slide-15
SLIDE 15

calculator.py: Part 3 (OPTIONAL)

Get the user’s input on two numbers they would like to add, storing each in a separate variable. Remember, input comes in as a string, so you will have to cast these values from strings to integers using int(). The print the two numbers you’ve stored as a math expression using the addition, subtraction, multiplication, and division operators.