1 / 24

Input and Variables

Input and Variables. Reading: Section 4 of Non-Programmers Tutorial for Python Appendix G(A68-A69) of Big Java. To learn about variables To learn about how to input values into variables using the Input function To learn how to assign values to variables using the assignment operator.

jon
Télécharger la présentation

Input and Variables

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. Input and Variables Reading: Section 4 of Non-Programmers Tutorial for Python Appendix G(A68-A69) of Big Java

  2. To learn about variables • To learn about how to input values into variables using the Input function • To learn how to assign values to variables using the assignment operator TopicGoals

  3. Variables • Used to store a value that you want to use at a later time • A variable has • a type: e.g., int, string, float • a name: e.g., size, message, price • a value: e.g., 10, “Hello world!”, 2.95 • Can you relate all of the above examples? Example: variable named message has type string and value “Hello world!”? “Hello world!” message type string

  4. Variable Identifiers/names • A Python identifier is a name used to identify a variable • Variable names must start with a letter (usually lower case) or an underscore character • Python does not allow punctuation and non-alphabetic characters (except the underscore’_’ ) such as @, $, and % within identifiers • Python is a case sensitive programming language. Thus weekday and Weekday are two different identifiers in Python.

  5. Meaningful Variable Names • Important to have meaningful variable identifiers in a programExample: number better than x to name a variable used to store a number • How to make multiword variable names easy to understand • Use underscores to separate wordsExample: student_grade, price_per_gallon • Or use camel case (preferred)Example: studentGrade, pricePerGallon • Note convention to start with a lower case letter

  6. Variables and their values • Value stored in a variable must match its type • It is an error to store a value whose type does not match the type of the variableExample: We can’t store variable size (of type int) in variable message (type string)- type mismatch error! • Variables can be used in place of the values that they storeExample: If variables myNameand yourNameare both of type string, then we can store yourNamein myName

  7. Type conversions • Suppose that xFloat is declared to be of type floatand we want to assign it an integer value. What happens? • In Python, this seems not to be an issue,as the variable type can change. • On the next slide, we first give the variablenX a floating point value, then an integer value. Note how the shell responds.

  8. More from the Python Shell • >>> ## First we assign nX a real value>>> nX = 2.67>>> nX2.67>>> ## Now we assign nX an integer value>>> nX = 4>>> nX4>>> ## We shall revisit the idea of type conversions>>> ## when we study Java, a strongly typed language.

  9. Which of the following are legal identifiers? Greeting1 g 101dalmatians Hello, World <greeting> NOTE: You might see expressions such as <your name>in program listings. This is commonly a directive to placeyour name in that place. For example ## Written by <your name> for me becomes## Written by Ed Bosworth Self Check 2.5

  10. Self Check 2.5 • Which of the following are legal identifiers? • Greeting1 • g • 101dalmatians • Hello, World • <greeting> • Answer: Only the first two are legal identifiers.

  11. How do we put a value in a variable? • Assignment operator: = • Used to change the value of a variable: • width = 10 • Variable width now has value 10, any old value it had has been overwritten! 10 width

  12. Assignment operator examples • myName = “Big Ed” • yourName = “Shannon” • myName = yourName • Remember, myNameand yourNameare both of type string, so it’s legal to store yourNamein myName • Types on left hand and right hand sides of = must matchCan’t have: • yourName = 10 • width = yourName

  13. Assignment • The right-hand side of the =symbol can be a mathematical expression: • height = 10 • width = height + 5 • Means: • compute the value of height + 5 • store that value in the variable width • variable width now has the value 15

  14. SelfCheck2.7 Is 12 = 12 a valid expression in the Python language? Answer: No, the left-hand side of the =operator must be a variable. NOTE:For all lectures up to this point, the character “=”,which we call the “equals sign”, is really the assignment operator.A future lecture will cover the operation of determiningwhether or not two values are equal.

  15. Self Check 2.8 How do you change the value of the greeting variable to "Hello, Nina!"? Answer: greeting = "Hello, Nina!"

  16. How can the user provide values for variables at run-time? - Function input An example program: print("Halt!") userInput= input("Who Goes there? ") print("You may pass, " + userInput) When you run it, here is what your screen will show (the name Josh is what you would have typed in): Halt! Who Goes there? Josh You may pass, Josh

  17. What function input does • First it prints the string you give as an argument (in this case ”Who goes there?”), • input("Who Goes there? ") • Then it waits for a line to be typed in by you, and returns the string of characters typed. • This value is assigned to the variable userInputuserInput = input("Who Goes there? ") A string argument

  18. Try this in IDLE • Modify the message.pyprogram as follows in the editor • person = input('Enter your name: ') • print('Hello', person) • Save it with File ‣ Save As....`, using the name greeting.py.

  19. Try this in IDLE # Program to illustrate input and output applicantName= input("Enter the applicant's name: ") interviewerName= input("Enter the interviewer's name: ") time = input("Enter the appointment time: ") print(interviewer, "will interview", applicant, "at", time)

  20. Inputting numbers Until now, we’ve seen examples of string type values being input Example: applicantName = input("Enter the applicant's name: ") When the expected input is a number, not a string, we use: studentNumber= int(input("Enter your student number: ")) Or, if the number to be input has a decimal point: itemPrice= float(input("Enter price per item: "))

  21. A Simple Python Program • ## A test to show the functioning of the input statement.## Written by Ed Bosworth on Thursday, January 23, 2014##sX= input("Give me a string: ")print (sX)nX= int(input("Give me an integer: "))print (nX)fX= float(input("Give me a real number: "))print (fX)print ("All done.")##

  22. Running the Program • >>> Give me a string: sXsXGive me an integer: 2323Give me a real number: 2323.0All done.>>>

  23. What we learned • A variable is a location in the computer’s memory for storing a value of a certain type. • It has an address for its memory location, but we usually refer to it by its name. • Each variable has a type and will only accept values matching its type • We use the assignment operator, = , to store values in variables

  24. What we learned (cont.) • The input function enables input from the keyboard • It also takes an argument and outputs it as a prompt for user input • Computers use the binary number system, not the decimal system we are familiar with https://encrypted-tbn2.gstatic.com/images?q=tbn:ANd9GcSsKOnlYM71YJc6VSIHosQzwckHfovwMgECHMCgY249tRfML1PF

More Related