130 likes | 201 Views
Explore essential Python programming concepts such as legal variable names, integer vs. true division, expressions vs. statements, string operations, and more in this Q&A session for CS104 students. Get answers to common programming queries and deepen your understanding of Python fundamentals.
E N D
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? 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.
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.
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.
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?)
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)
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…)
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.)
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.
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.
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
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.)
Modules • TBD