Class code for pythonroom.com cchsp2cs
Python basics Presentation Name Course Name Unit # – Lesson #.# – Lesson Name This presentation will cover the basics of Python. Python basics
Canopy environment All languages use a development environment. We will use Python in the Canopy development environment. It has two main parts. You can edit and run whole programs in the code editor. They don't run when you type; they only run when you execute them with this green arrow. You can execute a single line of code in the interactive session. You can expand either section by dragging the line between them.
Variables and Arithmetic In []: a = 3 In []: a * 7 Out[]: 21 In []: a ** 2 Out[]: 9 Working in the interactive session, You can do some arithmetic. Here we store the number 3 in the variable "a". Then we evaluate two expressions. a times 7, that's 3 times 7: 21. a to the power of 2, that's 3 squared: 9.
Expression in mathematics Expressions Expression in mathematics Expression in Python 2 + 3 2+3 2 - 3 2-3 2×3 = 2•3 = 2(3) 2*3 The operators for arithmetic are a little different than mathematics. We never use the asterisk in math. In math, we show multiplication using the times sign, a dot at the center of the line, or by writing two things next to each other, sometimes using parentheses to show that the two numbers are separate factors.
Data Types Data type Examples int 3 float 3. 3.4 str '3' bool False list [False, '3', 3.14] Every value in Python has a type. Numbers can be int or float types. Floats have a decimal point in them. Data stored in int can only be an integer, while floats can be any number, including the integers. (Integers are 0, 1,2,3,… and the negative integers -1,-2,-3,…)
Expressions for enrichment Expression in mathematics Expression in Python 2 3 or ⅔ 2/3 2 3 or ⅔ 2./3 23 2**3 3 = 31/2 = 30.5 3**(1./2)or 3**(0.5) Dividing two integer types rounds down so that the result is also an int. This is the "floor function" or "greatest integer function" shown on the left. Use a float when dividing if you want a float- that's what the decimal point next to the 2 does; it make the 2 a float, so the result will be a float: 0.6666666. Python uses ** for exponents. Most programming languages use ^. Enrichment: Powers can be fractions. The denominator takes the root; one half takes a square root, one third takes a cube root, and so on. You can use a decimal equivalent for the fraction, too.
Lists, Elements by Index In []: a = [10, 12, 13] In []: a[0] Out[]: 10 In []: a[1] Out[]: 12 One type of data is a list. Each element of the list can be any of the type of data. You can even have a list that contains a list. This list contains three ints: 10, 12, and 13. We use the square brackets to write a list and separate the elements with a comma. We also use square brackets alongside a variable name. App Inventor is one-indexed: the first element in the list has index 1. Python is zero-indexed. The first element has index 0. To read this aloud, say "a index zero or "a zero." It's value is 10. The value of the element at index 1, a[1], is 12.
Iteration for n in [1, 2, 3]: print n**2 Output: 1 4 9 You just learned about lists. Lists are useful for iteration. The "for" loop in Python executes over and over. The "for" block, shown by indentation, starts at the colon and includes all the lines of code indented after the "for" statement. The "for" block is executed once for each value in the list. Output: 1 4 9
Built in functions: In []: abs(4) Out[]: 4 In []: abs(-4) In []: range(7) Out[]: [0, 1, 2, 3, 4, 5, 6] In []: len(['a', 2, 4e12]) Out[]: 3 Python has 30 keywords like "for", "in", "print", and so on. Python also has 80 built-in functions. Functions are "called," using parentheses. Arguments go inside the parentheses. Many of the Python built-in functions are math functions, like the absolute value function shown here. The range function produces a list, starting at 0, counting by 1s, and STOPPING SHORT of the argument. That way range(7) is a list of seven numbers, from 0 to 6, stopping short of 7. The len() function tells you the length of a list— the number of items in the list.
Counted loop for n in range(3): print n Output: 1 2 1 2 You can create a counted loop using the range() function. Whatever number you put in the range function, the "for" block will get executed that many times.
for n in range(3): print n*2 Counted loop Don’t assign to the loop index You can evaluate the loop index for n in range(3): print n*2 Output: 2 4 n here is called the loop index. You can use the value of the loop index, as we do here, evaluating n squared. But don't assign to the loop index. It would not be ok, for example, to make n = n+1 inside the loop. Although the assignment statement and loop would execute in a predictable manner, it is hard to read and can be a source of hard-to-find bugs. Don't assign to a loop index.
Optional enrichment: for n in 'this': print n*2, Output: tthhiiss Iteration across strings Multiplication with characters Comma for no-new-line print for n in 'this': print n*2, Output: tthhiiss
Conditionals a = 2 if a == 2: print a else: print a+1 print a+2 Indentation is also used after a colon to show which code should get executed when a conditional is true or false. Output: 2 4
Debugging: When you get an error, look at the last line!