15}{2:>15}".format("angle",\ "sin", "cos")) for ang in range(0,190,30): rad=ang*math.pi/180 print("{0:<5}{1:>15.2f}{2:>15.2f}".format(ang,\ math.sin(rad), math.cos(rad))) main()"> 15}{2:>15}".format("angle",\ "sin", "cos")) for ang in range(0,190,30): rad=ang*math.pi/180 print("{0:<5}{1:>15.2f}{2:>15.2f}".format(ang,\ math.sin(rad), math.cos(rad))) main()">

Presentation is loading. Please wait.

Presentation is loading. Please wait.

functions: argument, return value

Similar presentations


Presentation on theme: "functions: argument, return value"— Presentation transcript:

1 functions: argument, return value
Class 11 printing in columns functions that print Unicode str, chr, ord, int append accumulating a string functions: argument, return value functions – cooking.

2 printing in columns with titles
#example of printing in columns import math def main(): #print the titles print("{0:<5}{1:>15}{2:>15}".format("angle",\ "sin", "cos")) for ang in range(0,190,30): rad=ang*math.pi/180 print("{0:<5}{1:>15.2f}{2:>15.2f}".format(ang,\ math.sin(rad), math.cos(rad))) main()

3 printing in columns with titles (fixed width font)
angle sin cos

4 Don't call T() in a print statement, there are print statements in T()
def T(): print("-----") print(" | ") def main(): T() # NOT print(T()) – you will get None, the return value.

5 String Representation
In the early days of computers, each manufacturer used their own encoding of numbers for characters. ASCII system (American Standard Code for Information Interchange) uses 127 bit codes Python supports Unicode (100,000+ characters) Python Programming, 3/e

6 String Representation
The ord function returns the numeric (ordinal) code of a single character. The chr function converts a numeric code to the corresponding character. >>> ord("A") 65 >>> ord("a") 97 >>> chr(97) 'a' >>> chr(65) 'A' Python Programming, 3/e

7 Unicode numerals 0-9 are in order upper case letters are in order
ord('0') is 48, ord('1') is 49,... ord('4')-ord('0') is 4 – the int upper case letters are in order ord('A') is 65 lower case letters are in order ord('a') is 97 chr(65) is 'A' and chr(97) is 'a'

8 Coding and Encryption See discussion in the book

9 Building a string accumulate a string First initialize it: message=""
as we go through loop adding characters (or other strings): message= message + <string>

10 Lists Have Methods, Too The append method can be used to add an item at the end of a list. squares = [] for x in range(1,101): squares.append(x*x) We start with an empty list ([]) and each number from 1 to 100 is squared and appended to it ([1, 4, 9, …, 10000]). Python Programming, 3/e

11 Lists Have Methods, Too we can add two lists, but cannot add a list and an int or a string, so need append() Python Programming, 3/e

12 str vs chr, int vs ord str to convert a number into a string (so can concat, for exam) Not the same as chr! str(65) returns '65' chr(65) returns 'A' int('9') returns 9 ord('9') returns 57

13 argument, return value num=ord('K') argument, (or actual parameter)
'K' is a literal variable num is assigned the return value of the function ord

14 argument, (or actual parameter)
argument, return value x='T' num=ord(x) argument, (or actual parameter) x is a variable

15 functions - cooking


Download ppt "functions: argument, return value"

Similar presentations


Ads by Google