Python 3home |
Introduction to Python
davidbpython.com
A variable is a value assigned ("bound") to an object.
xx = 10 # assign 10 to xx
yy = 2
zz = xx * yy # compute 10 * 2 and assign integer 20 to variable yy
print(zz) # print 20 to screen
xx is a variable bound to 10 = is an assignment operator assigning 10 to xx yy is another variable bound to 2 * is a multiplication operator computing its operands (10 and 2) zz is bound to the product, 20 print() is a function that renders its argument to the screen.
early on we need to distinguish between a variable and a literal.
xx = 10 # assign 10 to xx
yy = 2
zz = xx * yy # compute 10 * 2 and assign integer 20 to variable yy
print(zz) # print 20 to screen
early on we need to distinguish between a variable and a literal.
xx = 10 # assign 10 to xx
yy = 2
zz = xx * yy # compute 10 * 2 and assign integer 20 to variable yy
print zz # print 20 to screen
An object is a data value of a particular type.
Every data value in Python is an object.
var_int = 100 # assign integer object 100 to variable var_int
var2_float = 100.0 # assign float object 100.0 to variable var2_float
var3_str = 'hello!' # assign str object 'hello' to variable var3_str
# NOTE: 'hash mark' comments are ignored by Python.
At every point you must be aware of the type and value of every object in your code.
The three object types we'll look at in this unit are int, float and str. They are the "atoms" of Python's data model.
data type | known as | description | example value |
---|---|---|---|
int | integer | a whole number | 5 |
float | float | a floating-point number | 5.03 |
str | string | a character sequence, i.e. text | 'hello, world!' |
The string has 3 ways to enquote -- all produce a string.
s1 = 'hello, quote'
s2 = "hello, quote"
s3 = """hello, quote # multi-line strings can be expressed with triple-quotes
Sincerely, Python"""
s4 = 'He said "yes!"' # using single quotes to enquote double quotes
s5 = "Don't worry about that." # using double quotes to enquote a single quote
The way a variable is written in the code determines type.
It's vital that we always be aware of type.
myint = 5
myfloat = 5.0
mystr = '5.0'
Other languages (like Java and C) use explicit type declarations to indicate type, for example int a = 5. But Python does not do this.
The way a variable is written in the code determines type.
It's vital that we always be aware of type.
myint = 5 # written as a whole number: int
myfloat = 5.0 # written with a decimal point: float
mystr = '5.0' # written with quotes: str
Other languages (like Java and C) use explicit type declarations to indicate type, for example int a = 5. But Python does not do this.
Printing is usually not enough to determine type, since a string can look like any object.
myint = 5
myfloat = 5.0
mystr = '5.0'
print(myint) # 5
print(myfloat) # 5.0
print(mystr) # 5.0
mystr looks like a float, but it is a str.
If we're not sure, we can always have Python tell us an object's type.
myint = 5
myfloat = 5.0
mystr = '5.0'
print(type(myint)) # <class 'int'>
print(type(myfloat)) # <class 'float'>
print(type(mystr)) # <class 'str'>
This means that what an object can do is defined by its type.
a = 5 # int, 5
b = 10.0 # float, 10.0
c = '10.0' # str, '10.0'
x = a + b # 15.0 (adding int to float)
y = a + c # TypeError (cannot add int to str!)
Even though the value '10.0' looks like a number, it is of type str. Python will not add an int to a str.
You must follow correct style even though Python does not always require it.
name = 'Joe'
age = 29
my_wordy_variable = 100
student3 = 'jg61'