ycl week 3 let s talk about variables variables
play

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


  1. YCL Week 3 Let’s talk about variables!

  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)

  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")

  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

  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)

  6. Constants PI = 3.14159 SCREEN_WIDTH = 600 RED = (255, 0 ,0)

  7. Operators Operator Description + Addition - Subtraction * Multiplication / Division // Integer division (rounds down)

  8. PEMDAS ● Parentheses ● Exponent ● Multiplication ● Division ● Addition ● Subtraction

  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.

  10. = does not mean equal # The last two lines You can rewrite the code above to work by explicitly multiplying: will error # This code works. Although it doesn't x = 3 print anything. y = 2x z = 2(3 + x) x = 3 y = 2 * x z = 2 * (3 + x)

  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)?

  12. Counter Variables x = 7 x = x + 1 print(x) What will this print?

  13. calculator.py: Part 1 Write a program that prints out the results of at least one calculation for each of the basic operations: 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)

  14. calculator.py: Part 2 Store at least 4 integers into 4 different variables, and use these variables to perform operations 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?

  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.

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend