Python basics Introduction to Computer Science 2015 Project

Python basics Introduction to Computer Science © 2015 Project Lead The Way, Inc.

Canopy environment Introduction to Computer Science © 2015 Project Lead The Way, Inc.
![Variables and Arithmetic In []: a = 3 In []: a * 7 Out[]: Variables and Arithmetic In []: a = 3 In []: a * 7 Out[]:](http://slidetodoc.com/presentation_image_h2/e4cebd06bcf6fe48e73c0ceb84f35c3e/image-3.jpg)
Variables and Arithmetic In []: a = 3 In []: a * 7 Out[]: 21 In []: a ** 2 Out[]: 9 Introduction to Computer Science © 2015 Project Lead The Way, Inc.

Expressions Expression in mathematics Expression in Python 2+3 2 -3 2× 3 = 2 • 3 = 2(3) 2*3 Introduction to Computer Science © 2015 Project Lead The Way, Inc.

Data Types Data type int float str bool list Introduction to Computer Science Examples 3 3. 3. 4 '3' False [False, '3', 3. 14] © 2015 Project Lead The Way, Inc.

Expressions for enrichment Expression in mathematics Expression in Python 2/3 23 Introduction to Computer Science 2**3 3**(1. /2)or 3**(0. 5) © 2015 Project Lead The Way, Inc.
![Lists, Elements by Index In []: a = [10, 12, 13] In []: Out[]: Lists, Elements by Index In []: a = [10, 12, 13] In []: Out[]:](http://slidetodoc.com/presentation_image_h2/e4cebd06bcf6fe48e73c0ceb84f35c3e/image-7.jpg)
Lists, Elements by Index In []: a = [10, 12, 13] In []: Out[]: Introduction to Computer Science a[0] 10 a[1] 12 © 2015 Project Lead The Way, Inc.
![Iteration for n in [1, 2, 3]: print n**2 Output: 1 4 9 Introduction Iteration for n in [1, 2, 3]: print n**2 Output: 1 4 9 Introduction](http://slidetodoc.com/presentation_image_h2/e4cebd06bcf6fe48e73c0ceb84f35c3e/image-8.jpg)
Iteration for n in [1, 2, 3]: print n**2 Output: 1 4 9 Introduction to Computer Science © 2015 Project Lead The Way, Inc.
![Built in functions: In []: Out[]: abs(4) 4 abs(-4) 4 In []: range(7) Out[]: Built in functions: In []: Out[]: abs(4) 4 abs(-4) 4 In []: range(7) Out[]:](http://slidetodoc.com/presentation_image_h2/e4cebd06bcf6fe48e73c0ceb84f35c3e/image-9.jpg)
Built in functions: In []: Out[]: abs(4) 4 abs(-4) 4 In []: range(7) Out[]: [0, 1, 2, 3, 4, 5, 6] In []: len(['a', 2, 4 e 12]) Out[]: 3 Introduction to Computer Science © 2015 Project Lead The Way, Inc.

Counted loop for n in range(3): print n Output: 0 1 2 Introduction to Computer Science © 2015 Project Lead The Way, Inc.

Counted loop Don’t assign to the loop index You can evaluate the loop index for n in range(3): print n*2 Output: 0 2 4 Introduction to Computer Science © 2015 Project Lead The Way, Inc.

Optional enrichment: Iteration across strings Multiplication with characters Comma for no-new-line print for n in 'this': print n*2, Output: tthhiiss Introduction to Computer Science © 2015 Project Lead The Way, Inc.

Conditionals a = 2 if a == 2: print a else: print a+1 print a+2 Output: 2 4 Introduction to Computer Science © 2015 Project Lead The Way, Inc.

Debugging: Introduction to Computer Science © 2015 Project Lead The Way, Inc.
- Slides: 14