Presentation is loading. Please wait.

Presentation is loading. Please wait.

The Beauty and Joy of Computing Lecture #18 Besides Blocks I: Intro to Python Amazon Dash is Not an April Fools Joke UC Berkeley EECS Head TA Michael Ball.

Similar presentations


Presentation on theme: "The Beauty and Joy of Computing Lecture #18 Besides Blocks I: Intro to Python Amazon Dash is Not an April Fools Joke UC Berkeley EECS Head TA Michael Ball."— Presentation transcript:

1 The Beauty and Joy of Computing Lecture #18 Besides Blocks I: Intro to Python Amazon Dash is Not an April Fools Joke UC Berkeley EECS Head TA Michael Ball https://www.amazon.com/oc/dash-button

2 UC Berkeley “The Beauty and Joy of Computing”: Besides Blocks I (2) Ball Admin Notes  Reminder: Explore Project Part 2 is due Friday  Comments  Artifact + Explanation  Final Projects start next week!  Use Snap! or Python  Individual  3-weeks to work  Demo: (review)) Exporting files from Snap!

3

4 UC Berkeley “The Beauty and Joy of Computing”: Besides Blocks I (4) Ball The Goals of BJC  BJC’s goal is not to teach you Snap!  Teach you critical thinking about societal implications of computing  Teach you how to program (Snap! is the best intro language we know) and help you succeed in the future  More importantly: Teach you how to think like a computer scientist in life, called “computational thinking”

5 UC Berkeley “The Beauty and Joy of Computing”: Besides Blocks I (5) Ball What is Computational Thinking?  Using abstraction (removing detail and generalization with parameters)  It’s understanding the value of a “spec” that specifies a contract  The iterative design cycle: design, prototype, implement, evaluate (loop)  Thinking about how solutions scale, parallelize, generalize, and trying to foresee the unintended consequences!

6 UC Berkeley “The Beauty and Joy of Computing”: Besides Blocks I (6) Ball Why Learn Python?  You already know it!  Syntax is very similar to Snap!, especially like writing Snap! on paper.  Python (also) runs everywhere  OS X and Windows, and now there are browser apps (Cloud9.io) and even iOS apps  Lots of online support  Plenty of advanced libraries  Everything from graphics processing to AI to games!  Used in industry and academia

7 UC Berkeley “The Beauty and Joy of Computing”: Besides Blocks I (7) Ball What You’ll Learn  New Syntax  Different way to write programs you already know how to write (in Snap!)  A little bit about the command line  A text-based interface that exposes you to the internals of how a computer works  How to find help online  A little more about Object Oriented Programming  Programming is really not about the language or environment

8

9

10 UC Berkeley “The Beauty and Joy of Computing”: Besides Blocks I (10) Ball Getting Python 3  We’ll be using Python 3 for this class.  It does have some minor changes from Python 2, but you don’t need to worry about what they are.  Download Python 3 from  https://python.org/downloads https://python.org/downloads  Run the graphical installer  All official Python documentation is at  https://docs.python.org https://docs.python.org

11 UC Berkeley “The Beauty and Joy of Computing”: Besides Blocks I (11) Ball Intro to the Command Line  “Terminal” on OS X (and Linux) and “Command Prompt” on Windows  a.k.a. “Unix shell” on Mac and Linux  Does the same things as the rest of your computer (browse and edit files, even browse the web!)  OS X:  Open: /Applications/Utilities/Terminal.app  Windows:  Use the search bar for “cmd”

12 UC Berkeley “The Beauty and Joy of Computing”: Besides Blocks I (12) Ball Python Programs  Python programs are just a text file with Python syntax.  To run a program you type:  python file_name.py  (Sometimes this is python3 )  Aside: a moonscape font indicates a command to run.  Python has two modes – “normal” and “interactive”  Interactive mode happens if you don’t provide a file to run.  After each command Python evaluates your code and returns the response. (Kind of like clicking a block in Snap!)  Use python -i file_name.py  Force a file to be run in interactive mode.

13

14 UC Berkeley “The Beauty and Joy of Computing”: Besides Blocks I (14) Ball Text and Numbers  Numbers in Python are called  ints (numbers w/o decimals)  floats (numbers with decimals)  Strings:  Some text in between quotes “” or ‘’ >>> 2 + 2 4 >>> "Hello, " + "world" 'Hello, world'

15 UC Berkeley “The Beauty and Joy of Computing”: Besides Blocks I (15) Ball Lists  Lists Work in much the same way:  Syntax : [itemA, itemB, itemC] >>> ['B', 'J', 'C'] ['B', 'J', 'C'] >>> len(['B', 'J', 'C']) 3

16 UC Berkeley “The Beauty and Joy of Computing”: Besides Blocks I (16) Ball BEWARE: Item 0!  However, there is one big difference!  The first item in Python is item 0.  This also applies for strings as well.  Access items using [#] >>> letters = ['B', 'J', 'C'] >>> letters[0] 'B' >>> 'Hello, World!'[7] 'W'

17 UC Berkeley “The Beauty and Joy of Computing”: Besides Blocks I (17) Ball Variables  No need to “declare” variable in Python,  Just use =  To access a variable, type it’s name >>> course = ['B', 'J', 'C'] >>> school = ’UC Berkeley’ >>> course ['B', 'J', 'C'] >>> school ’UC Berkeley'

18 UC Berkeley “The Beauty and Joy of Computing”: Besides Blocks I (18) Ball Conditionals >>> if (5 % 2) == 1:... print('This number is odd')... else:... print('This number is even')... This number is odd

19 UC Berkeley “The Beauty and Joy of Computing”: Besides Blocks I (19) Ball Conditionals  Conditionals Work the same way.  End the condition with :  Parentheses are optional around the condition.  Indent the body 1 “level” (usually 4 spaces  Indentation matters in Python!  To end a condition, just un-indent your code  You can also see that mod in Python is a %  Note that the equals check is ==  Python also supports an if (without the else) just like Snap!

20 UC Berkeley “The Beauty and Joy of Computing”: Besides Blocks I (20) Ball Loops >>> print('this is a song...') this is a song... >>> while(True):... print('that goes on and on...')... that goes on and on... [omitted] >>> for i in range(1, 11):... print(i)... 1 [omitted 2…10[

21 UC Berkeley “The Beauty and Joy of Computing”: Besides Blocks I (21) Ball Loops  Loops are similar to conditionals.  Instead of an “until” loop, Python has a “while” loop.  Python is missing the repeat(n) loop and the forever loop, but you can make these with whole and for loops.  Note: range() is a function which is includes the first item, but not the last!  range(1,10) counts from 1 to 9!  You can use this function anywhere in Python.

22 UC Berkeley “The Beauty and Joy of Computing”: Besides Blocks I (22) Ball Functions >>> def factorial(n):... if n < 1:... return 1... else:... return n * factorial(n - 1)... >>> factorial(4) 24

23 UC Berkeley “The Beauty and Joy of Computing”: Besides Blocks I (23) Ball Functions  There is no distinction between a command, reporter or predicate.  You can simply use: return None or just return  Python uses the word def  The body of function is indented  All arguments are specified in () and must come at the end of the function name  report  return  Recursion works exactly the same as in Snap!  Call a function like this: name(arg1, arg2..)

24 UC Berkeley “The Beauty and Joy of Computing”: Besides Blocks I (24) Ball Summary  Lots of little syntax differences!  The Python documentation is your friend  Don’t get too hung up on the differences and don’t get discouraged when you get an error!  There’s so much more to Python in the coming weeks:  Python has thousands of additional, useful built in tools  Python supports HOFs and lambdas  Lots of cool libraries to explore (including turtle graphics)


Download ppt "The Beauty and Joy of Computing Lecture #18 Besides Blocks I: Intro to Python Amazon Dash is Not an April Fools Joke UC Berkeley EECS Head TA Michael Ball."

Similar presentations


Ads by Google