Python 3home |
Introduction to Python
davidbpython.com
Built-in functions activate functionality when they are called.
aa = 'hello' # str, 'hello'
bb = len(aa) # pass string object aa as an argument to function len(),
# which returns an integer object as a return value.
print(bb) # int, 5
The len() function takes a string argument and returns an integer -- the length of (number of characters in) the string.
varx = 'hello, world!'
vary = len(varx) # int, 13
pythonreference
The round() function takes a float argument and returns another float, rounded to the specified decimal place.
aa = 5.9583
bb = round(aa, 2) # float, 5.96
cc = round(aa) # int, 6
Some floating-point operations will result in a number with a small remainder:
x = 0.1 + 0.2
print(x) # 0.30000000000000001 (should be 0.3?)
y = 0.1 + 0.1 + 0.1 - 0.3
print(y) # 5.551115123125783e-17 (should be 0.0?)
This remainder represents the float imprecision of your computer. No binary machine is capable of calculating floating-point math with perfect precision, although many programs (like Excel) may simulate it.
The solution when using Python is to round any result:
x = 0.1 + 0.2 # 0.30000000000000001
z = round(x, 1)
print(z) # 0.3
This function allows us to enter data into the program through the keyboard.
cc = input('enter name: ') # program pauses! Now the user types something
print(cc) # [a string, whatever the user typed]
The exit() function terminates execution immediately. An optional string argument can be passed as an error message.
exit(0) # 0 indicates a successful termination (no error)
exit('error! here is a message') # string argument passed to exit()
# indicates an error led to termination
This function can be used as a temporary stop to the program if we'd like to isolate some statements.
We can also use exit() to simply stop program execution in order to debug:
aa = '55'
bb = float(aa)
print('type of bb is:')
print((type(bb)))
exit() # we inserted this to stop the code
# from continuing; we'll remove it later
cc = bb * 2 # because of exit() above, this code
# will not be reached
This function can convert a str or float to the int type.
# str -> int
aa = '55'
bb = int(aa) # int, 55
print(type(bb)) # <class 'int'>
# float -> int
var = 5.95
var2 = int(var) # int, 5: the remainder is lopped off (not rounded)
This function converts an int or str to the float type.
# int -> float
xx = 5
yy = float(xx) # float, 5.0
# str -> float
var = '5.95'
var2 = float(var) # float, 5.95
This function converts any value to the str type.
var = 5 # int, 5
var2 = 5.5 # float, 5.5
svar = str(var) # str, '5'
svar2 = str(var2) # str, '5.5'
Any object type can be converted to str. ex. 2.12 - 2.16
Because Python is strongly typed, conversions can be necessary.
Numeric data sometimes arrives as strings (e.g. from input() or a file). Use int() or float() to convert to numeric types.
aa = input('enter number and I will double it: ')
print(type(aa)) # <class 'str'>
num_aa = int(aa) # int() takes the string as an argument
# and returns an integer
print(num_aa * 2) # prints the input number doubled
You can use int() and float() to convert strings to numbers.
Just starting out, some students improvise syntax that doesn't exist.
Imagine that would like to find the length of a string. What do you do? Some students begin writing code from memory, even though they are not completely familiar with the right syntax.
they may write something like this...
var = 'hello'
mylen = var.len() # or mylen = length('var')
# or mylen = lenth(var)
...and then run it, only to get a strange error that's difficult to diagnose. The solution is to never improvise syntax. Instead, always start with an existing example.
When you want to use a Python feature, you must follow an existing example !
Let's say you have a string and you'd like to get its length:
s = "this is a string I'd like to measure"
You look up the function in a reference, like pythonreference.com:
mylen = len('hello')
Then you use the feature syntax very carefully:
slen = len(s) # int, 36
However, the code you write may be slightly different than the example code:
early on we need to distinguish between a variable and a literal.
xx = 10 # int, 10
yy = 2 # int, 2
zz = xx * yy # int, 20
print(zz)
next slide should be an update or continuation of this same slide, with bullet points added
early on we need to distinguish between a variable and a literal.
xx = 10 # int, 10
yy = 2 # int, 2
zz = xx * yy # int, 20
print(zz)
Here's an example of this common error that beginners make - try to avoid it!
Going back to our previous example - you'd like to use len() to measure this string:
s = "this is a string I'd like to measure"
You look up the function in a reference, like pythonreference.com:
mylen = len('hello')
You have been told to make your syntax match the example's. But should you do this?
slen = len('s') # int, 1
You were expecting a length of 36, but you got a length of 1. Can you see why? The variable s points to a long string. The literal string, 's', is just a one-character string. In trying to match the example code, you may have thought you needed to also match the quotes. But keep in mind that you may be using a variable where the example code has a literal, but these two are interchangeable. The takeaway is this: anyplace a literal is used, a variable can be used instead; and anyplace a variable is used, a literal can be used instead. ex 2.17 and 2.18 illustrate not confusing literal and variable