1 / 13

Q and A for Chapter 2 – 3.3

Q and A for Chapter 2 – 3.3. CS 104 Victor Norman. Legal Variable Names. Q: Why can’t python allow you to start variables with something like @ or a number?

helena
Download Presentation

Q and A for Chapter 2 – 3.3

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. Q and A for Chapter 2 – 3.3 CS 104 Victor Norman

  2. Legal Variable Names Q: Why can’t python allow you to start variables with something like @ or a number? A: That’s how they decided to do it… The interpreter must be able to figure out the type of everything. For a number, variable, string, etc., the interpreter must know. The beginning symbols of each thing give a hint as to its type.

  3. type(blah) Q: When the textbook says type(blah) do we actually type in type or is that just textbook stuff? A: You can actually type type(blah) to find out the type of blah. This is really only useful in interactive mode.

  4. Interactive vs. Script Mode Q: I’m really confused about this interactive mode vs. script mode. Please explain! A: Start interactive mode by running python interpreter, without feeding it a script to run. It just repeatedly waits for you to give it commands. Very useful for investigating short problems, syntax, etc. In script mode, if you want to see output, you have to tell the program to print output.

  5. Integer vs. true division Q: When do you get integer division (aka “floor division”) and when do you get true division? A: integer division happens when both sides of the / operator are of type integer. Otherwise, you get true division. (Extra credit: how would you implement the round function?)

  6. integer  float? Q: Is it possible to do something like adding two integers together and end up with a float? A: You have to use a type conversion function: c = float(a + b)

  7. What about zipcode = 02132? Q: What is going on with Exercise 2.1 (page 12)? A: Python allows you to enter non-decimal numbers. Specifically, you can enter values in octal (base 8) and binary (base 2): 010 = 8 (in decimal) 0100 = 64 0b10 = 2 0b100 = 4, 0b1000 = 8 (You may forget all about this…)

  8. Expressions vs. Statements Q: Can you expand on the difference between expressions and statements? A: Yes. A statement is a command. An expression just is an evaluation of operands that produces a result. If you don’t print it or store it (with an assignment statement), you lose it. (Not terribly important.)

  9. Comments Q: Can you have a comment embedded in a line by starting and ending it with #? A: No. Comments start from # and go to the end of the line. Q: Can you print a #? A: Yes: if the # is in a string, it will be printed.

  10. String Operations Q: What are the uses of string oeprations and what other operators can be used? A: Only + is really useful. I’ve only use * maybe once or twice in 15 years… Those are the only two that are defined.

  11. Putting ‘ ‘ in a string Q: Can you use math operators to put ‘ ‘ in a string? A: If you have two strings you can concatenate them together to form a new string with ‘ ‘ in the middle. fName = ‘John’ lName = ‘Calvin’ name = fName + ‘ ‘ + lName

  12. The Beginning of the End… Q: Is there a built-in function, like float(), that converts a string of characters to a variable name that a value can then be assigned to? A: No! Thank goodness! (You cannot do this: ‘Hello’ = 4. ‘Hello’ is a literal value, not a variable name referring to a value in memory.)

  13. Modules • TBD

More Related