Changes between Version 18 and Version 19 of ControlSystems/SoftwareTeam/IntroToPython
- Timestamp:
- Sep 4, 2017, 5:22:09 PM (8 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
ControlSystems/SoftwareTeam/IntroToPython
v18 v19 11 11 (A quick note...If you know all this already and are getting bored...CONGRATULATIONS! Here's a '''[https://www.youtube.com/watch?v=x94yTpv0T1k link]''' to some cute corgi puppy videos as your reward. Then, come back and dive into a later section. This is all self-paced.) 12 12 13 We'll be starting with a programming language called [https://www.python.org/ Python], specifically Python 3. There are a lot of programming languages. Some are created because they're better for solving certain problems, others because people are bored and want to try something new. Python has been around for a while, and its pretty popular because its easy to use. The team uses Java to control the robot but that language is a little 13 We'll be starting with a programming language called [https://www.python.org/ Python], specifically Python 3. There are a lot of programming languages. Some are created because they're better for solving certain problems, others because people are bored and want to try something new. Python has been around for a while, and its pretty popular because its easy to use. The team uses Java to control the robot but that language is a little trickier. We'll get to that one later once you get a handle on the basics. 14 14 15 15 Python, like every programming language, has a structure. Here are the big parts: … … 20 20 * ''Libraries'': A lot of other people have written functions to do things. A lot of things. When these things are bundled together they're called libraries. You can tell a computer to use a library and save your self a lot of typing. 21 21 22 Much like in English, you blend all of these together to create stories, or programs. Both have a beginning middle and end. So lets write the first programming storythat all programmers start with: "Hello World"22 Much like in English, you blend all of these together to create stories, or ''programs''. Both have a beginning middle and end. So lets write the first program that all programmers start with: "Hello World" 23 23 24 24 {{{ … … 29 29 This tells the computer to print, you guessed it, "Hello World" 30 30 31 Now you try it :Do the following:31 Now you try it...Do the following: 32 32 33 1. Highlight all the text in the central area and hit back space (this'll we'll clear out all the sample code.) 34 1. Then highlight the code above, right click and click {{{Copy}}. 35 1. Go [https://www.tutorialspoint.com/execute_python3_online.php here]. This site is our code editor, and its pretty cool. You can put code in the center section, and click execute to run it. The lower window shows the results. 36 1. Highlight all the code already in the center window. 37 1. Right click and pick !{{{Paste}}}. This should replace everything in the window with the contents above 38 1. Click !{{{Execute}}} right above the center window. 33 1. Then highlight the code above, right click and click {{{Copy}}. This put the code on the clipboard so you can share it with another window. 34 1. Go [https://www.tutorialspoint.com/execute_python3_online.php here]. This site is our code editor for training purposes, and its pretty cool. You can put code in the center section, and click '''Execute''' to run it. The lower window shows the results. 35 1. On that page, highlight all the code already in the center window. 36 1. Right click and pick !{{{Paste}}}. This should replace everything in the window with the contents above. 37 1. Finally click !{{{Execute}}} right above the center window to run the program. 39 38 40 (From now on, we'll call this step "''Go [https://www.tutorialspoint.com/execute_python3_online.php here] and run the code above''". (Just come back to these steps if you forget how to do this).39 (From now on, we'll call this step "''Go [https://www.tutorialspoint.com/execute_python3_online.php here] and run the code above''". (Just come back to these steps if ever you forget how to do that). 41 40 42 After a bit, you'll be rewarded with themessage in the bottom window:41 After a bit, you'll be rewarded with the following message in the bottom window: 43 42 44 43 {{{ 45 44 Hello World! 46 45 }}} 47 And, boom, now you're a programmer. The rest is just more details. This is the end of Lesson 1. 46 The program told the computer to print the value of variable a on the screen. 48 47 49 = Lesson 2: More Things To Do = 48 And, boom, now you're a programmer. The rest of learning how to code is just more details. This is the end of Lesson 1. 49 50 51 = Lesson 2: Making decisions = 50 52 In lesson 1, we had you create a program, "Hello World". Now, "Hello World" is not too exciting if you want to program robots, but its an important first step. We showed you how to run a function (or verb) `print`, which does something (show on the screen the value of its argument `a`, where `a` is assigned the value "Hello World". 51 53 … … 63 65 print("Hope you had a good time\n") 64 66 }}} 65 It should print:67 This program uses a IF-THEN-ELSE statement to tell the computer to make a decision in your program, and you'll do it all the time. ''Go [https://www.tutorialspoint.com/execute_python3_online.php here] and run the code above.'' It should print: 66 68 67 69 {{{ 68 Welcome to our store 69 70 We're here! Come on in 71 72 Our ours are 11:00am-11:52am 73 74 Hope you had a good time 75 70 Welcome to our store 71 72 We're here! Come on in 73 74 Our ours are 11:00am-11:52am 75 76 Hope you had a good time 77 76 78 }}} 77 Three things to note:78 79 79 1. The double equals in location=='HERE' on the first line. It is called a ''condition''. This is a equals comparison. Other kinds of comparisons in python are listed here. Also note on that line 'HERE' is in quotes. That's to indicate its a string (a text value) not some variable. 80 1. Spacing and indents in Python are important. the line "We're here..." and the line below it are indented to indicate its a part of a statement ''block'' to run when the condition evaluates to ''True''. The second block on the else is run when the condition to ''False'', and only contains one line. If you want to do stuff outside of the IF-THEN, don't indent it like the first and last print statements. 80 Three things to note about this program: 81 82 1. The double equals in location=='HERE' on the first line. It is called an ''expression''. In this case it is an equals expression (other kinds of comparisons in python are listed here) . It returns ''True'' when the condition evaluates to ''True'', otherwise its ''False''. Note on that line the word 'HERE' is in quotes. That's to indicate its a string (a text value) not some variable (which doesn't exist). If you used ''='' instead of ''=='' it wouldn't work as you expected because it would be a variable assignment. If a IF-THEN statement ever behaves in an unexpected way, check for this problem first, its a common error. 83 1. Spacing and indents are important in Python. the line "We're here..." and the line below it are indented to indicate they are part of a statement ''block'' that runs when the condition evaluates to ''True''. The second block on the else is run when the condition to ''False'', and only contains one line. If you want to do stuff outside of the IF-THEN, don't indent it like the first and last print statements. 81 84 1. You'll see that a few of the print statements end with a {{\\n}}. This is called a ''escape sequence'', and its shorthand for a newline. When the computer sees it in a print statement, it will add an extra line to the output. See here for other escape sequences you can add. 82 85 83 Now, change the ''condition'' to Location=='Poughkeepsie, NY'. It should print "Go away, we're out eating tacos". What happens?Why? If I was in Poughkeepsie, I'd be out eating tacos too. Yummmm, tacos.86 Now, in the editor change the ''condition'' to location=='Poughkeepsie, NY' and run it again. What happens? It should print "Go away, we're out eating tacos". Why? If I was in Poughkeepsie, I'd be out eating tacos too. Yummmm, tacos. 84 87 85 This control struction evaluates a *expression* (location=='HERE') and if its true, then do the THEN part, otherwise do the ELSE part. This is called the IF-THEN-ELSE structure, and it’s the most basic control structure. 88 If you're writing your own code, you want to the skip the else, just omit the word else: and the indented block below it. For example: 86 89 90 {{{ 91 medianSalary=98260 #Median annual salary of a software engineer in the US 92 salary=100000 93 if salary > medianSalary: 94 print("Whohoo! Dinner's on you!") 95 }}} 96 " 97 98 99 87 100 Here's a couple other control structures that you should know 88 101