Simple Python Data Victor Norman CS104 Calvin College

advertisement
Simple Python Data
Victor Norman
CS104
Calvin College
Reading Quiz
• Counts toward your grade.
Data Type (or “class”)
• Everything has a type
– Defines legal values and the operations you can do
on those values.
– integers: values = whole numbers; operations:
add, multiply, etc.
– strings: series of characters; operations: uppercase it, append to it, etc.
• Python must know what type of thing it is
dealing with, always.
Literal Values
• Code must provide hints as to what type of
thing you mean:
– all digits  integer (int)
– all digits and a dot  floating point number (float)
– characters surrounded by quotes  string (str)
– more to come…
Clicker questions
String Literals
• Can use single, double, triple-single, or tripledouble quotes:
– ', ", ''', """
• Triple quotes useful for creating strings with
quotes in them and/or newlines in them.
• Examples…
Type Conversion Functions
• Useful when you have a value that is the wrong
type.
– Really only needed by us when we read in a value
from the user (which is always a str) and need to treat
it like an int or float.
• Takes a value and produces a value of another
type.
• Do NOT do this kind of thing:
– float(3.14)
– int(3)
– str(“hello”)
Clicker Questions
Namespace and Objectspace
• Assignment creates/updates name in the
namespace to refer to an object (new or
existing) in the objectspace.
• Assignment steps:
– evaluate the rhs (i.e., get a value for it)
• find object with that value or create it in objectspace.
– Look up the name on lhs.
• if it does not exist, create it in the namespace.
– Make name point to the value.
Clicker Questions
Assignment for Thursday
• Do TuringsCraft CodeLab questions before lab on
Thursday.
– These are graded and finishing before lab is crucial.
• Note for TC assignment:
– you can print out multiple items on one line this way:
x = 3
y = 4
print(x, y)
print(“Result is”, x * y)
Download