Python 3

home

Introduction to Python

davidbpython.com




Creating and Identifying Objects by Type


the variable

A variable is a value assigned ("assigned" or "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.






the literal: a value typed into our code

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


next slide should be an update or continuation of this same slide, with bullet points added






the literal: a value typed into our code

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






the object

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

At every point you must be aware of the type and value of every object in your code.






object types for this session

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 typeknown asdescriptionexample value
intintegera whole number5
floatfloata floating-point number5.03
strstringa character sequence, i.e. text'hello, world!'






sidebar: string literal syntax

The string may be bounded by 3 different quotation marks -- all produce a string.


s1 = 'hello, quote'          # single quotes
s2 = "hello, quote"          # double quotes

# triple quotes:  put quotes around multiple lines
s3 = """hello, quote
Sincerely, Python"""


s4 = 'He said "yes!"'               # using single quotes to include double quotes
s5 = "Don't worry about that."      # using double quotes to include a single quote






identifying type through syntax

The way a variable is written in the code determines type.


It's vital that we always be aware of type.


a = 5.0
b = '5.0'
c = 5


the next slide should be a continuation of this one






identifying type through syntax

The way a variable is written in the code determines type.


It's vital that we always be aware of type.


a = 5.0         # float (written with a decimal point)
b = '5.0'       # str   (written with quotes)
c = 5           # int   (written as a whole number)

Other languages (like Java and C) use explicit type declarations to indicate type, for example int c = 5. But Python does not do this. Instead, it relies on the syntax of the literal (whole number, floating-point, quotation marks, etc.)






can we identify type through printing?

Printing is usually not enough to determine type, since a string can look like any object.


a = 5.0
b = '5.0'
c = 5

print(a)         # 5.0
print(b)         # 5.0
print(c)         # 5

b looks like a float, but it is a str.






identifying type through the type() function

If we're not sure, we can always have Python tell us an object's type.


a = 5.0
b = '5.0'
c = 5

print(type(a))         # <class 'float'>
print(type(b))         # <class 'str'>
print(type(c))         # <class 'int'>

exercise 2.1






python is strongly typed

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!)






variable naming rules

You must follow correct style even though Python does not always require it.


name = 'Joe'
age = 29

my_wordy_variable = 100

student3 = 'jg61'





[pr]