1 / 72

Variables, Expressions, and Standard Functions

Variables, Expressions, and Standard Functions. Topics. Basic calculation Expressions, variables, and operator precedence Data types Input / Output Examples. A calculator. We can use a computer as a calculator. Just type expressions into the Python Shell. Python Shell in Wing IDE.

anne
Télécharger la présentation

Variables, Expressions, and Standard Functions

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Variables, Expressions, and Standard Functions

  2. Topics • Basic calculation • Expressions, variables, and operator precedence • Data types • Input / Output • Examples

  3. A calculator • We can use a computer as a calculator. • Just type expressions into the Python Shell Python Shell in Wing IDE

  4. A calculator • Try this. Type it intoPython Shell >>> 10 * 5 50 >>> 1 + 2 + 3 + 4 10 >>> 1+2+3+4 10 >>> 1 * 4 + 5 ** 2 29 The answer Spaces are irrelevant ** is for exponentiation

  5. Expression • What we have just typed into the Python Shell is called expressions. • After the shell reads each expression, the shell evaluate it and reports the result.

  6. Easy calculation • An object moves with the starting speed of 10 m/s with an acceleration of 2 m/s2. After 5 seconds, how far is the object from its starting position? s = ut + at2/2 10 * 5 + 2 * (5*5) / 2

  7. Operators (1) • In previous examples, we use many operators such as +, -, or /, to tell Python to perform various computations with the data. • An operator tells Python what operation to perform with its operands. operands 10 * 5 operator

  8. Operators (2) • Operators can be • Binary operators that work with two operands, e.g., +, -, or *.5 * 3 10 – 2 15*7 • Unaryoperators that work with a single operand, e.g, –.-3 +2 -5 * 7

  9. Operators (2) • Basic mathematical operators are shown in the table below

  10. Division in Python • There are two operators related to division division modulo – find the remainder

  11. Numbers in Python • There are two types for numbers in Python: integer(type int) andfloating points (type float)

  12. Quick test

  13. Integers v.s. Floating-points • If you write a number without a "dot", it will be treated as an integer. • Results • Every mathematical operation between integer and integer returns an integer, except for division. • Division returns floating-point numbers. • Any operations with floating-point numbers return floating-point numbers.

  14. 3/5*2 • Evaluation is usually done from left to right ((3/5)*2) ( 0.6 *2)

  15. Operator precedence • But operators have different precedence, e.g., * or/ have higher precedence over + or-. 2+3*6 2+(3*6)

  16. This is just…. • High-school math!

  17. Operator precedence • Evaluation order is from left-to-right for operators with the same precedence, except **.

  18. Try this (1) 2+5*6/3+(7-2*3) What is the result? 13.0

  19. Try this(2) 2 ** 2 ** 3 What is the result? 2 ** (2 ** 3) 28 = 256

  20. Reusing values • A force of 2.5 newton pushes a rock with mass 1 kg to the left. Where is the rock after 1 second, 5 second and 15 second? (1.0/2.5)*1*1/2 Redundant (1.0/2.5)*5*5/2 (1.0/2.5)*15*15/2

  21. Variables • We can use variables to refer to result from previous computation. a = 1.0/2.5 a*1*1/2 a 0.4 a*5*5/2 a*15*15/2

  22. Variables a = 1.0/2.5 • A variableis used to refer to various data. • Use "=" to assign a value to a variable. • When we refer to that variable, we get the value that the variable is referring to. 0.4 a

  23. Variables can be "modified" (1) a = 10a * 5b = 3a + ba = 7a + ba = b + 5aa + b 50 13 10 8 11

  24. Variables can be "modified" (2) a = 10a = a + 1 11

  25. Variables can be "modified" (3) x = 10x = x * 2 20

  26. Variables can be "modified" (4) x = 10x = x * 2 + 5 25

  27. Working onWing IDE Shellor Console After you type commands or expressions, the Python Interpreter evaluates them and prints out the output

  28. Typing programs inWingIDE Editing area Any commands here are executed after you hit the "run" button.

  29. A program • A program is a sequence of commands (or statements) • a = 10b = a + 5a - bc = 12b = a + cc = a*ba + b + c1 + a - c Try totype this intoWing IDE

  30. Result • Because the program does not have statements that output anything. Empty

  31. Printing • We can use function print to display results

  32. A program • A program is a sequence of commands (or statements) Add"print"to display the valueof the required expressions • a = 10b = a + 5print(a – b)c = 12b = a + cc = a*bprint(a + b + c)print(1 + a – c)

  33. See the output after hitting "Run"

  34. Function calls • print(10) Function name Arguments

  35. What's going on? • print(a + b * 2) • The expressions on the parameter list are evaluated before sending the result to the function. • 20 Assume that a = 5 b = 10 • 25 • print(25)

  36. A simple calculation program • We have the following coins • 5 one-baht coins • 7 ten-baht coins • 2 twenty-baht notes • 3 hundred-baht notes How muchmoney do wehave ? sum1 = 1 * 5sum10 = 10 * 7sum20 = 20 * 2sum100 = 100 * 3sum = sum1+sum5+sum20+sum100print(sum)

  37. A simple program(2) • Or we can even remove variablesum sum1 = 1 * 5sum10 = 10 * 7sum20 = 20 * 2sum100 = 100 * 3print(sum1+sum5+sum20+sum100)

  38. Meaningful names Comparethese twoprograms a = 1 * 5b = 10 * 7c = 20 * 2d = 100 * 3e = a + b + c + dprint(e) Theyperform thesame task sum1 = 1 * 5sum10 = 10 * 7sum20 = 20 * 2sum100 = 100 * 3sum = sum1+sum5+sum20+sum100print(sum) Which oneis easier to understand?

  39. Suggestions • Write programs for people to read • At the very least, one of the audience is yourself.

  40. Comments (#) • To make a program easier to read, we can add comments to the program • Everything after the # symbol is a comment.

  41. A program with comments # this program calculates total money# from the amount of each type of coins or# bank notes that you havesum1 = 1 * 5 # value of 1-baht coinssum10 = 10 * 7sum20 = 20 * 2sum100 = 100 * 3print(sum1+sum5+sum20+sum100)

  42. Strings • A computer can work with many types of data. • Astring is another data type which is very important. It is a type for texts or messages. • Formally, a string is a sequence of characters. ”Hello ”Hello, world”

  43. String constants • We can either use single or double quotes to specify strings, e.g., • ”Hello” • ’World’ • However, the starting quotes and the ending quotes must match. • We can also have special characters inside a string. They will start with backslash" \ ".

  44. Examples (1) print("hello") hello print('hello') hello print("I'm 9") I'm 9 print('I'm 9') ERROR print('I\'m 9') I'm 9 print("I\'m 9") I'm 9

  45. Examples (2) print("123") 123 print(123) 123 print("12" + "3") 123 print(12 + 3) 15 print("12" + '3') 123 print("12" + 3) ERROR

  46. A slightly better program sum1 = 1 * 5sum10 = 10 * 7sum20 = 20 * 2sum100 = 100 * 3sum = sum1+sum5+sum20+sum100print("The total is",sum) The total is 415

  47. A slightly even better program sum1 = 1 * 5sum10 = 10 * 7sum20 = 20 * 2sum100 = 100 * 3sum = sum1+sum5+sum20+sum100print("The total is",sum,"bath.") The total is 415 bath.

  48. Sidenote: printing and new lines • We can display data usingfunction print. • It will always add a new line at the end. • If we want to avoid the new line, we can add an additional option "end" to print. print(10)print(20)print(10,end='')print(20) 10201020 This tells print to end this output with an empty string, instead of a new line.

  49. Reading inputs • We can use function input to read data from the user input • The function returns a string that the user types into the shell.

  50. Examples in the Python Shell >>> name = input() Somchai >>> print("Hello", name) Hello Somchai >>> a = input() 10 >>> b = input() 100 >>> print(a+b) 10100

More Related