Difference between revisions of "CSC111 Lab 1 2015"
(→Playing with Strings) |
|||
(42 intermediate revisions by the same user not shown) | |||
Line 2: | Line 2: | ||
---- | ---- | ||
− | |||
− | |||
__TOC__ | __TOC__ | ||
<center> | <center> | ||
Line 12: | Line 10: | ||
<br /> | <br /> | ||
<bluebox> | <bluebox> | ||
− | This lab is just an introduction to having fun with Python. It's purpose is to have you explore the different systems that are available to you, and get a sense of how to program in an "intuitive" fashion. | + | This lab is just an introduction to having fun with Python. It's purpose is to have you explore the different systems that are available to you, and get a sense of how to program in an "intuitive" fashion. Do not worry about the details. We haven't had time to fully understand what variables are, what strings are, or how the function print works. Today is just a day for you to explore the tools, rund some Python code in Idle, and submit a program on Moodle. |
</bluebox> | </bluebox> | ||
<br /> | <br /> | ||
Line 35: | Line 33: | ||
<br /> | <br /> | ||
− | ==Idle== | + | =A Quick Video Overview= |
+ | <br /> | ||
+ | Here's a quick overview of how to use Idle to program in Python. You will notice that there are two distinct ways of typing in Python statements. The left window, in the video, is the ''console'', where each line you type is interpreted by Python. The window on the right, however, allows you to write a collection of statements (a program), store the collection in a file, and then have Python execute the contents of the program. | ||
+ | <br /> | ||
+ | <center><videoflash>orQp64wnRfQ</videoflash></center> | ||
+ | <br /> | ||
+ | =Editing Programs with the ''Idle'' Editor= | ||
<br /> | <br /> | ||
* '''Idle''' is the name of an ''editor'' one can use to create Python program on the computer. | * '''Idle''' is the name of an ''editor'' one can use to create Python program on the computer. | ||
Line 53: | Line 57: | ||
<br /> | <br /> | ||
* Enter the following code in the '''Untitled''' window: | * Enter the following code in the '''Untitled''' window: | ||
− | + | <br /> | |
+ | <source lang="python"> | ||
age = 20 | age = 20 | ||
year = 2015 | year = 2015 | ||
Line 60: | Line 65: | ||
print( year ) | print( year ) | ||
print( year - age ) | print( year - age ) | ||
− | + | </source> | |
− | * Press '''F5''' to run the program. When prompted to save the program, save it as '''lab1.py''' | + | <br /> |
+ | * Press '''F5''' to run the program. When prompted to save the program, save it as '''lab1.py''', and save it in a folder where you can easily retrieve it (you will need to submit this file for evaluation at the end of the lab). | ||
* Look at the ''Console'' window. Some numbers appear... Do they make sense? | * Look at the ''Console'' window. Some numbers appear... Do they make sense? | ||
* change the numbers that are ''assigned'' to ''age'', and ''year'' and see how your program reacts when you run it. | * change the numbers that are ''assigned'' to ''age'', and ''year'' and see how your program reacts when you run it. | ||
Line 73: | Line 79: | ||
<br /> | <br /> | ||
* Modify your program slightly, as shown below: | * Modify your program slightly, as shown below: | ||
+ | <br /> | ||
+ | <source lang="python"> | ||
age = 20 | age = 20 | ||
Line 79: | Line 87: | ||
print( age, year ) | print( age, year ) | ||
+ | </source> | ||
+ | <br /> | ||
* Run your program | * Run your program | ||
* Notice the slightly different output. | * Notice the slightly different output. | ||
Line 85: | Line 95: | ||
<br /> | <br /> | ||
* Try this: | * Try this: | ||
− | + | <br /> | |
+ | <source lang="python"> | ||
+ | |||
age = 20 | age = 20 | ||
year = 2015 | year = 2015 | ||
Line 92: | Line 104: | ||
print( "you are", age ) | print( "you are", age ) | ||
print( "you were born in", yearBorn ) | print( "you were born in", yearBorn ) | ||
− | + | </source> | |
+ | <br /> | ||
* Run your program. Change the year and the age. Run your program again and see if you can predict the output. | * Run your program. Change the year and the age. Run your program again and see if you can predict the output. | ||
<br /> | <br /> | ||
Line 98: | Line 111: | ||
<br /> | <br /> | ||
* This time we add new variables that contain text. In computer science we refer to text as ''strings of characters'', or ''strings'' for short. | * This time we add new variables that contain text. In computer science we refer to text as ''strings of characters'', or ''strings'' for short. | ||
− | + | <br /> | |
+ | <source lang="python"> | ||
+ | |||
name = "Alex" | name = "Alex" | ||
college = "Smith College" | college = "Smith College" | ||
Line 106: | Line 121: | ||
print( name, "goes to", college ) | print( name, "goes to", college ) | ||
print( name, "is", age, "years old" ) | print( name, "is", age, "years old" ) | ||
− | + | </source> | |
− | * Run your program a few times, each time changing | + | <br /> |
+ | * Run your program a few times, each time changing the value of a variable. For example change "Alex" to "Monique", or year to 2015. | ||
Line 119: | Line 135: | ||
* Write a new program that contains these variables: | * Write a new program that contains these variables: | ||
+ | <br /> | ||
+ | <source lang="python"> | ||
+ | |||
name = "Alex" | name = "Alex" | ||
college = "Smith College" | college = "Smith College" | ||
age = 20 | age = 20 | ||
year = 2015 | year = 2015 | ||
− | + | ||
+ | </source> | ||
+ | <br /> | ||
* Figure out how to add new variables and write several '''print''' statements that will make your program output: | * Figure out how to add new variables and write several '''print''' statements that will make your program output: | ||
Line 140: | Line 161: | ||
<br /> | <br /> | ||
− | === | + | ===Repeating Strings=== |
− | |||
− | |||
− | * | + | * Write a new program: |
− | |||
− | |||
− | |||
− | |||
− | |||
<br /> | <br /> | ||
+ | <source lang="python"> | ||
+ | |||
+ | word1 = "hello" | ||
+ | word2 = "there " | ||
+ | |||
+ | print( word1 * 2 ) | ||
+ | print( word1 * 5 ) | ||
+ | print( word2 * 10 ) | ||
+ | </source> | ||
<br /> | <br /> | ||
+ | * See how you can ''multiply'' a string? | ||
<br /> | <br /> | ||
− | |||
− | |||
− | |||
* Try this: | * Try this: | ||
+ | <br /> | ||
+ | <source lang="python"> | ||
− | word1 | + | print( word1 * 2, word2 * 4 ) |
− | + | </source > | |
− | |||
− | |||
− | |||
− | |||
<br /> | <br /> | ||
+ | * Run the program. Does the output make sense? | ||
<br /> | <br /> | ||
<br /> | <br /> | ||
Line 172: | Line 192: | ||
|- | |- | ||
| | | | ||
− | ===Challenge # | + | |
+ | ===Challenge #2=== | ||
|} | |} | ||
[[Image:QuestionMark3.jpg|right|110px]] | [[Image:QuestionMark3.jpg|right|110px]] | ||
− | * Modify your program and make it print | + | * Modify your program (you are allowed to change the contents of the variables), and make it print the output shown below. Note the new exclamation point: |
− | hello hello hello hello there! | + | hello hello hello hello there! |
hello there! there! there! there! there! hello | hello there! there! there! there! there! hello | ||
<br /> | <br /> | ||
Line 187: | Line 208: | ||
|- | |- | ||
| | | | ||
− | ===Challenge # | + | |
+ | ===Challenge #3 (tricky)=== | ||
|} | |} | ||
[[Image:QuestionMark4.jpg|right|100px]] | [[Image:QuestionMark4.jpg|right|100px]] | ||
− | * | + | * Make your program print the output below, using a string and the string multiplication operation: |
hellothere!hellothere!hellothere!hellothere!hellothere! | hellothere!hellothere!hellothere!hellothere!hellothere! | ||
Line 198: | Line 220: | ||
<br /> | <br /> | ||
<br /> | <br /> | ||
− | |||
===Playing with Lists and Loops=== | ===Playing with Lists and Loops=== | ||
Line 206: | Line 227: | ||
<br /> | <br /> | ||
<source lang="python"> | <source lang="python"> | ||
− | + | ||
− | + | for name in [ "Lea Jones", "Julie Fleur", "Anu Vias" ]: | |
print( name ) | print( name ) | ||
+ | |||
</source> | </source> | ||
<br /> | <br /> | ||
<br /> | <br /> | ||
− | <tanbox>Observe that the line that starts with ''print'' is indented from the line that starts with ''for''. This is a '''very important''' detail: Python uses ''indentation'' to influence how statements depend on each other. | + | <tanbox>Observe that the line that starts with ''print'' is indented from the line that starts with ''for''. This is a '''very important''' detail: Python uses ''indentation'' to influence how statements depend on each other. Because the '''print''' statement is indented relative to the '''for''' statement, it is ''controlled'' by the '''for''' statement, and will be repeated for each string in the list. |
</tanbox> | </tanbox> | ||
<br /> | <br /> | ||
Line 226: | Line 248: | ||
| | | | ||
− | ===Challenge # | + | ===Challenge #4=== |
|} | |} | ||
[[Image:QuestionMark5.jpg|right|100px]] | [[Image:QuestionMark5.jpg|right|100px]] | ||
Line 233: | Line 255: | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
<br /> | <br /> | ||
<br /> | <br /> | ||
Line 238: | Line 263: | ||
===Repeating several statements=== | ===Repeating several statements=== | ||
− | * Play with these different code | + | * Play with these different code snippets. Make sure you understand how your program works before moving on to the next sections. |
+ | <br /> | ||
+ | <tanbox> | ||
+ | The purpose is not to go quickly through the lab, but to start acquiring an intuition for the logic of programming. | ||
+ | </tanbox> | ||
<br /> | <br /> | ||
<br /> | <br /> | ||
<source lang="python"> | <source lang="python"> | ||
− | + | ||
− | + | for name in [ "Lea Jones", "Julie Fleur", "Anu Vias" ]: | |
print( name ) | print( name ) | ||
− | print( "-" ) | + | print( "---------------" ) |
+ | |||
</source> | </source> | ||
<br /> | <br /> | ||
Line 255: | Line 285: | ||
<br /> | <br /> | ||
<source lang="python"> | <source lang="python"> | ||
− | + | for name in [ "Lea Jones", "Julie Fleur", "Anu Vias" ]: | |
− | |||
print( name, len( name ) ) | print( name, len( name ) ) | ||
</source> | </source> | ||
Line 266: | Line 295: | ||
<br /> | <br /> | ||
<source lang="python"> | <source lang="python"> | ||
− | + | ||
− | + | for name in [ "Lea Jones", "Julie Fleur", "Anu Vias" ]: | |
print( name ) | print( name ) | ||
print ( len( name ) ) | print ( len( name ) ) | ||
Line 279: | Line 308: | ||
<br /> | <br /> | ||
<source lang="python"> | <source lang="python"> | ||
− | + | ||
− | + | for name in [ "Lea Jones", "Julie Fleur", "Anu Vias" ]: | |
print( name, '-' * len( name ) ) | print( name, '-' * len( name ) ) | ||
</source> | </source> | ||
Line 291: | Line 320: | ||
|- | |- | ||
| | | | ||
− | ===Challenge # | + | |
+ | ===Challenge #5=== | ||
|} | |} | ||
[[Image:QuestionMark6.jpg|right|100px]] | [[Image:QuestionMark6.jpg|right|100px]] | ||
− | * Make your program display the names with a bar made of minus signs under each name. The length of the bar should be the same as the length of the name. For example: | + | * Make your program display the names with a bar made of minus signs under each name. The length of the bar should be the same as the length of the name that is above it. For example: |
Lea Jones | Lea Jones | ||
Line 310: | Line 340: | ||
|- | |- | ||
| | | | ||
− | ===Challenge # | + | |
+ | ===Challenge #6=== | ||
|} | |} | ||
[[Image:QuestionMark7.png|right|100px]] | [[Image:QuestionMark7.png|right|100px]] | ||
Line 334: | Line 365: | ||
|- | |- | ||
| | | | ||
+ | |||
===Challenge of the Day=== | ===Challenge of the Day=== | ||
|} | |} | ||
[[Image:QuestionMark8.jpg|right|150px]] | [[Image:QuestionMark8.jpg|right|150px]] | ||
− | * Make your program display a box around each name | + | ====Step 1==== |
+ | <br /> | ||
+ | * Create a program with all the statements below. They contain the solution for this challenge. You will need to figure out how to reorganize them and modify them to create the answer for this challenge. So create this program and study carefully its code and its output. | ||
+ | <br /> | ||
+ | <source lang="python"> | ||
+ | name = "Julia Child" | ||
+ | nameLen = len( name ) | ||
+ | nameLen4 = len( name ) + 4 | ||
+ | nameLen2 = len( name ) + 2 | ||
+ | x = 10 | ||
+ | bar1 = "-" * x | ||
+ | bar2 = bar1 + "|" | ||
+ | bar3 = "<" + bar1 + ">" | ||
+ | bar4 = "-" * nameLen | ||
+ | |||
+ | print( "name = ", name ) | ||
+ | print( "nameLen = ", nameLen ) | ||
+ | print( "nameLen4 = ", nameLen4 ) | ||
+ | print( "nameLen2 = ", nameLen2 ) | ||
+ | print( "x = ", x ) | ||
+ | print( "bar1 = ", bar1 ) | ||
+ | print( "bar2 = ", bar2 ) | ||
+ | print( "bar3 = ", bar3 ) | ||
+ | print( "bar4 = ", bar4 ) | ||
+ | |||
+ | </source> | ||
+ | <br /> | ||
+ | * Change "Julia Child" for some other, longer, name. Run your program again and see how the different variables will change according to the length of the string you store in ''name''. | ||
+ | <br /> | ||
+ | ====Step 2==== | ||
+ | <br /> | ||
+ | * Modify your program as follows: | ||
+ | <br /> | ||
+ | <source lang="python"> | ||
+ | for name in [ "Julia Child", "Gloria Steinem" ]: | ||
+ | nameLen = len( name ) | ||
+ | nameLen4 = len( name ) + 4 | ||
+ | nameLen2 = len( name ) + 2 | ||
+ | x = 10 | ||
+ | bar1 = "-" * x | ||
+ | bar2 = bar1 + "|" | ||
+ | bar3 = "<" + bar1 + ">" | ||
+ | bar4 = "-" * nameLen | ||
+ | |||
+ | print( "nameLen = ", nameLen ) | ||
+ | print( "nameLen4 = ", nameLen4 ) | ||
+ | print( "nameLen2 = ", nameLen2 ) | ||
+ | print( "x = ", x ) | ||
+ | print( "name = ", name ) | ||
+ | print( "bar1 = ", bar1 ) | ||
+ | print( "bar2 = ", bar2 ) | ||
+ | print( "bar3 = ", bar3 ) | ||
+ | print( "bar4 = ", bar4 ) | ||
+ | print() | ||
+ | </source> | ||
+ | <br /> | ||
+ | |||
+ | * Carefully look at the output of your program. Because all the '''print''' statements are indented relative to the '''for''' statement, they are repeated for every name in the list. See how bar1 and bar2 do not change in length, but bar4 changes with each string. | ||
+ | <br /> | ||
+ | * Here is your challenge for today: Make your program use a for-statement and a list of 3 names, and make it display a box around each name. The challenge here is that the box must fit exactly the length of the name, without extra spaces around. | ||
------------- | ------------- | ||
Line 357: | Line 448: | ||
<br /> | <br /> | ||
− | * | + | =Submission of Lab 1 Program on Moodle= |
+ | <br /> | ||
+ | * For this lab you need to submit your solution program on Moodle. This submission is graded and the grade counts toward your lab grade for this class (15% of the total grade). | ||
+ | * Go to this [[CSC111 Submitting Programs to Moodle 2015| page]] and follow the directions for submitting your program. | ||
+ | <br /> | ||
+ | * '''Deadline''': Exceptionally, this week, the deadline is set to 2/6/05, to allow Add/Drop students to catch up. | ||
+ | * '''Grade''': | ||
+ | ** Program submitted with syntax errors: '''90/100''' | ||
+ | ** Program submitted without errors, but incorrect output: '''95/100''' | ||
+ | ** Program submitted ''with correct output'': '''100/100''' | ||
+ | <br /> | ||
+ | <br /> | ||
+ | |||
+ | {| style="width:100%; background:silver" | ||
+ | |- | ||
+ | | | ||
+ | |||
+ | = Optional Challenge = | ||
+ | |} | ||
+ | [[Image:QuestionMark8.jpg|right|150px]] | ||
+ | * This is not required today, but if you want some extra challenging coding to do, try making your program generate the output below. | ||
+-----------+ | +-----------+ | ||
Line 374: | Line 485: | ||
<br /> | <br /> | ||
<br /> | <br /> | ||
+ | * You do not have to submit this program. | ||
+ | |||
+ | |||
+ | <br /> | ||
+ | <br /> | ||
+ | <showafterdate after="20150207 00:00" before="20150601 00:00"> | ||
+ | <br /> | ||
+ | =Solution Programs= | ||
+ | <br /> | ||
+ | <source lang="python"> | ||
+ | # CSC111 Lab1 | ||
+ | # D. Thiebaut | ||
+ | # solution programs for Lab1 | ||
+ | |||
+ | |||
+ | # Challenge #1 | ||
+ | name = "Alex" | ||
+ | college = "Smith College" | ||
+ | age = 20 | ||
+ | year = 2015 | ||
+ | |||
+ | print( name, "goes to", college, "and is", age, "years old" ) | ||
+ | print( name, "was born in", year - age ) | ||
+ | print() | ||
+ | |||
+ | # Challenge #2 | ||
+ | word1 = "hello " | ||
+ | word2 = "there! " | ||
+ | |||
+ | print( word1 * 4, word2 ) | ||
+ | print( word1, word2*5, word1 ) | ||
+ | print() | ||
+ | |||
+ | # Challenge #3 | ||
+ | word1 = "hello" | ||
+ | word2 = "there!" | ||
+ | word3 = word1 + word2 | ||
+ | print( word3*5 ) | ||
+ | print( ("hello" + "there!") * 5 ) | ||
+ | print() | ||
+ | |||
+ | # Challenge #4 | ||
+ | for name in [ "Lea Jones", "Julie Fleur", "Anu Vias", | ||
+ | "Mickey", "Minnie", "Pluto" ]: | ||
+ | print( name ) | ||
+ | |||
+ | print() | ||
+ | |||
+ | # Challenge #5 | ||
+ | for name in [ "Lea Jones", "Julie Fleur", "Anu Vias", | ||
+ | "Mickey", "Minnie", "Pluto" ]: | ||
+ | print( name ) | ||
+ | print( "-" * len( name ) ) | ||
+ | |||
+ | print() | ||
+ | # Challenge #6 | ||
+ | for name in [ "Lea Jones", "Julie Fleur", "Anu Vias", | ||
+ | "Mickey", "Minnie", "Pluto" ]: | ||
+ | print( "-" * len( name ) ) | ||
+ | print( name ) | ||
+ | print( "-" * len( name ) ) | ||
+ | |||
+ | print() | ||
+ | # Challenge #7 | ||
+ | for name in [ "Lea Jones", "Julie Fleur", "Anu Vias", | ||
+ | "Mickey", "Minnie", "Pluto" ]: | ||
+ | name2 = "| " + name + " |" | ||
+ | bar = "-" * len( name2 ) | ||
+ | print( bar ) | ||
+ | print( name2 ) | ||
+ | print( bar ) | ||
+ | print() | ||
+ | print() | ||
+ | |||
+ | # Optional Challenge | ||
+ | for name in [ "Lea Jones", "Julie Fleur", "Anu Vias", | ||
+ | "Mickey", "Minnie", "Pluto" ]: | ||
+ | name2 = "| " + name + " |" | ||
+ | bar = "-" * len( name ) | ||
+ | bar = "+-" + bar + "-+" | ||
+ | print( bar ) | ||
+ | print( name2 ) | ||
+ | print( bar ) | ||
+ | print() | ||
+ | |||
+ | print() | ||
+ | |||
+ | |||
+ | </source> | ||
<br /> | <br /> | ||
+ | </showafterdate> | ||
<br /> | <br /> | ||
+ | <onlydft> | ||
+ | =VPL Module= | ||
+ | <br /> | ||
+ | <source lang="text"> | ||
+ | vpl_run.sh | ||
+ | 1 #! /bin/bash | ||
+ | 2 | ||
+ | 3 cat > vpl_execution <<EOF | ||
+ | 4 #! /bin/bash | ||
+ | 5 | ||
+ | 6 prog=lab1.py | ||
+ | 7 python=/usr/local/bin/python3.4 | ||
+ | 8 | ||
+ | 9 \$python \$prog | ||
+ | 10 | ||
+ | 11 EOF | ||
+ | 12 | ||
+ | 13 chmod +x vpl_execution | ||
+ | vpl_debug.sh | ||
+ | vpl_evaluate.sh | ||
+ | 1 #! /bin/bash | ||
+ | 2 | ||
+ | 3 cat > vpl_execution <<EOF | ||
+ | 4 #! /bin/bash | ||
+ | 5 | ||
+ | 6 prog=lab1.py | ||
+ | 7 python=/usr/local/bin/python3.4 | ||
+ | 8 #python=/usr/bin/python3.3 | ||
+ | 9 | ||
+ | 10 #--- grab program errors --- | ||
+ | 11 \$python \$prog 2>&1 >/dev/null | grep -B 10 -i "error" > error.out | ||
+ | 12 | ||
+ | 13 if [ -s error.out ] ; then | ||
+ | 14 echo "Comment :=>> Your program contains some errors:" | ||
+ | 15 echo "<|--" | ||
+ | 16 cat error.out | ||
+ | 17 echo "--|>" | ||
+ | 18 echo "Grade :=>> 90" | ||
+ | 19 exit | ||
+ | 20 fi | ||
+ | 21 | ||
+ | 22 #--- ensure that the program contains a for loop --- | ||
+ | 23 grep "^for" lab1.py > grepLines.out | ||
+ | 24 | ||
+ | 25 if [ -s grepLines.out ] ; then | ||
+ | 26 rm grepLines.out | ||
+ | 27 else | ||
+ | 28 echo "Comment :=>> Your program does not use a for loop." | ||
+ | 29 echo "Grade :=>> 95" | ||
+ | 30 exit | ||
+ | 31 fi | ||
+ | 32 | ||
+ | 33 # create expected output | ||
+ | 34 cat > expected1.out <<EOFEOF | ||
+ | 35 ------------- | ||
+ | 36 | Lea Jones | | ||
+ | 37 ------------- | ||
+ | 38 --------------- | ||
+ | 39 | Julie Fleur | | ||
+ | 40 --------------- | ||
+ | 41 ------------ | ||
+ | 42 | Anu Vias | | ||
+ | 43 ------------ | ||
+ | 44 EOFEOF | ||
+ | 45 | ||
+ | 46 #--- capture output of program | ||
+ | 47 \$python \$prog > user1.out | ||
+ | 48 | ||
+ | 49 #--- remove blank lines --- | ||
+ | 50 cat user1.out | sed '/^\s*$/d' > dummy.out | ||
+ | 51 mv dummy.out user1.out | ||
+ | 52 | ||
+ | 53 #--- add space in front of each line --- | ||
+ | 54 sed 's/^/ /' user1.out | ||
+ | 55 | ||
+ | 56 #--- compute difference --- | ||
+ | 57 diff -y -w user1.out expected1.out > diff.out | ||
+ | 58 | ||
+ | 59 #--- reject if different --- | ||
+ | 60 if ((\$? > 0)); then | ||
+ | 61 echo "Comment :=>>- Your output is incorrect." | ||
+ | 62 | ||
+ | 63 echo "Comment :=>> ---------------" | ||
+ | 64 echo "Comment :=>>- Your output:" | ||
+ | 65 echo "Comment :=>> ---------------" | ||
+ | 66 echo "<|--" | ||
+ | 67 cat user1.out | ||
+ | 68 echo "--|>" | ||
+ | 69 echo "" | ||
+ | 70 echo "Comment :=>> ---------------" | ||
+ | 71 echo "Comment :=>>- Expected output:" | ||
+ | 72 echo "Comment :=>> ---------------" | ||
+ | 73 echo "<|--" | ||
+ | 74 cat expected1.out | ||
+ | 75 echo "--|>" | ||
+ | 76 | ||
+ | 77 #--- consolation grade --- | ||
+ | 78 grade=95 | ||
+ | 79 | ||
+ | 80 # --------------------- REWARD IF CORRECT OUTPUT ----------------- | ||
+ | 81 else | ||
+ | 82 #--- good output --- | ||
+ | 83 echo "Comment :=>>- Congrats, your output is correct." | ||
+ | 84 echo "Comment :=>> --------------------------------." | ||
+ | 85 echo "<|--" | ||
+ | 86 cat user1.out | ||
+ | 87 echo "--|>" | ||
+ | 88 grade=100 | ||
+ | 89 fi | ||
+ | 90 | ||
+ | 91 #--- round down to 100 --- | ||
+ | 92 if (( grade > 100 )); then | ||
+ | 93 grade=100 | ||
+ | 94 fi | ||
+ | 95 | ||
+ | 96 #--- output final grade --- | ||
+ | 97 echo "Grade :=>> \$grade" | ||
+ | 98 EOF | ||
+ | 99 | ||
+ | 100 chmod +x vpl_execution | ||
+ | 101 | ||
+ | </source> | ||
<br /> | <br /> | ||
+ | </onlydft> | ||
<br /> | <br /> | ||
<br /> | <br /> |
Latest revision as of 15:33, 7 September 2015
--D. Thiebaut (talk) 16:21, 19 January 2015 (EST)
Contents
- 1 FUN WITH PYTHON
- 2 Option 1: Installing and Running Python on your Laptop
- 3 Option 2: Using Python on a Lab Computer
- 4 A Quick Video Overview
- 5 Editing Programs with the Idle Editor
- 5.1 Some Python Code to play with
- 5.1.1 Playing with numbers
- 5.1.2 Variation 1
- 5.1.3 Variation 2
- 5.1.4 Playing with Numbers and Strings
- 5.1.5 Challenge #1
- 5.1.6 Repeating Strings
- 5.1.7 Challenge #2
- 5.1.8 Challenge #3 (tricky)
- 5.1.9 Playing with Lists and Loops
- 5.1.10 Challenge #4
- 5.1.11 Repeating several statements
- 5.1.12 Challenge #5
- 5.1.13 Challenge #6
- 5.1.14 Challenge of the Day
- 5.1 Some Python Code to play with
- 6 Submission of Lab 1 Program on Moodle
- 7 Optional Challenge
- 8 Solution Programs
FUN WITH PYTHON
This lab is just an introduction to having fun with Python. It's purpose is to have you explore the different systems that are available to you, and get a sense of how to program in an "intuitive" fashion. Do not worry about the details. We haven't had time to fully understand what variables are, what strings are, or how the function print works. Today is just a day for you to explore the tools, rund some Python code in Idle, and submit a program on Moodle.
Option 1: Installing and Running Python on your Laptop
- If you want to work on your own laptop, you need to install Python 3. Open this page and follow the instructions for downloading Python 3 to your laptop.
Option 2: Using Python on a Lab Computer
- If your computer is not on, turn it ON, please!
- When prompted for booting Mac or Windows, select the Mac if you have the choice.
- Login using the Smith credentials (or the account that was given to you as a 5-college or high school student)
- Once you see the Desktop, click on the Finder (bottom left icon on the Desktop), and look in the Science Apps folder for Python 3.2.
- Start IDLE
Tip: if you can't find Idle, use the magnifying glass icon at the top right of the screen and search for idle and you should find it!
A Quick Video Overview
Here's a quick overview of how to use Idle to program in Python. You will notice that there are two distinct ways of typing in Python statements. The left window, in the video, is the console, where each line you type is interpreted by Python. The window on the right, however, allows you to write a collection of statements (a program), store the collection in a file, and then have Python execute the contents of the program.
Editing Programs with the Idle Editor
- Idle is the name of an editor one can use to create Python program on the computer.
- If the window you start with looks like this (see below), then use the top menu and click File, New Window to open an editing window.
- You should now have two windows on your screen, one the editing window, the other the shell window, where the program will send its output.
Some Python Code to play with
Playing with numbers
- Enter the following code in the Untitled window:
age = 20
year = 2015
print( age )
print( year )
print( year - age )
- Press F5 to run the program. When prompted to save the program, save it as lab1.py, and save it in a folder where you can easily retrieve it (you will need to submit this file for evaluation at the end of the lab).
- Look at the Console window. Some numbers appear... Do they make sense?
- change the numbers that are assigned to age, and year and see how your program reacts when you run it.
age, and year are variables. You can give variables any name you want, as long as you use letters or digits, and as long as the first character is a letter. R2d2 is a valid name for a variable. And so is alpha, or c3po. Variables are used to hold information, in our case, numbers.
Variation 1
- Modify your program slightly, as shown below:
age = 20
year = 2015
print( age, year )
- Run your program
- Notice the slightly different output.
Variation 2
- Try this:
age = 20
year = 2015
yearBorn = year - age
print( "you are", age )
print( "you were born in", yearBorn )
- Run your program. Change the year and the age. Run your program again and see if you can predict the output.
Playing with Numbers and Strings
- This time we add new variables that contain text. In computer science we refer to text as strings of characters, or strings for short.
name = "Alex"
college = "Smith College"
age = 20
year = 2015
print( name, "goes to", college )
print( name, "is", age, "years old" )
- Run your program a few times, each time changing the value of a variable. For example change "Alex" to "Monique", or year to 2015.
Challenge #1 |
- Write a new program that contains these variables:
name = "Alex"
college = "Smith College"
age = 20
year = 2015
- Figure out how to add new variables and write several print statements that will make your program output:
Alex goes to Smith College and is 20 years old Alex was born in 1995
- If your program is well written, and if you change the variable name to be "Francis", college to be "Umass", and age to be 18, then your program should automatically output:
Francis goes to Umass and is 18 years old Francis was born in 1997
- Verify that your program works correctly
Repeating Strings
- Write a new program:
word1 = "hello"
word2 = "there "
print( word1 * 2 )
print( word1 * 5 )
print( word2 * 10 )
- See how you can multiply a string?
- Try this:
print( word1 * 2, word2 * 4 )
- Run the program. Does the output make sense?
Challenge #2 |
- Modify your program (you are allowed to change the contents of the variables), and make it print the output shown below. Note the new exclamation point:
hello hello hello hello there! hello there! there! there! there! there! hello
Challenge #3 (tricky) |
- Make your program print the output below, using a string and the string multiplication operation:
hellothere!hellothere!hellothere!hellothere!hellothere!
Playing with Lists and Loops
- Create a new program in Idle with this new piece of code:
for name in [ "Lea Jones", "Julie Fleur", "Anu Vias" ]:
print( name )
- Press F5. Observe the output.
Challenge #4 |
- Add three new names to the list and make your program output them all one above the other.
Repeating several statements
- Play with these different code snippets. Make sure you understand how your program works before moving on to the next sections.
The purpose is not to go quickly through the lab, but to start acquiring an intuition for the logic of programming.
for name in [ "Lea Jones", "Julie Fleur", "Anu Vias" ]:
print( name )
print( "---------------" )
- and this one:
for name in [ "Lea Jones", "Julie Fleur", "Anu Vias" ]:
print( name, len( name ) )
- or:
for name in [ "Lea Jones", "Julie Fleur", "Anu Vias" ]:
print( name )
print ( len( name ) )
- Still some more
for name in [ "Lea Jones", "Julie Fleur", "Anu Vias" ]:
print( name, '-' * len( name ) )
Challenge #5 |
- Make your program display the names with a bar made of minus signs under each name. The length of the bar should be the same as the length of the name that is above it. For example:
Lea Jones --------- Julie Fleur ----------- Anu Vias --------
Challenge #6 |
- Similar question, but now like this:
--------- Lea Jones --------- ----------- Julie Fleur ----------- -------- Anu Vias --------
Challenge of the Day |
Step 1
- Create a program with all the statements below. They contain the solution for this challenge. You will need to figure out how to reorganize them and modify them to create the answer for this challenge. So create this program and study carefully its code and its output.
name = "Julia Child"
nameLen = len( name )
nameLen4 = len( name ) + 4
nameLen2 = len( name ) + 2
x = 10
bar1 = "-" * x
bar2 = bar1 + "|"
bar3 = "<" + bar1 + ">"
bar4 = "-" * nameLen
print( "name = ", name )
print( "nameLen = ", nameLen )
print( "nameLen4 = ", nameLen4 )
print( "nameLen2 = ", nameLen2 )
print( "x = ", x )
print( "bar1 = ", bar1 )
print( "bar2 = ", bar2 )
print( "bar3 = ", bar3 )
print( "bar4 = ", bar4 )
- Change "Julia Child" for some other, longer, name. Run your program again and see how the different variables will change according to the length of the string you store in name.
Step 2
- Modify your program as follows:
for name in [ "Julia Child", "Gloria Steinem" ]:
nameLen = len( name )
nameLen4 = len( name ) + 4
nameLen2 = len( name ) + 2
x = 10
bar1 = "-" * x
bar2 = bar1 + "|"
bar3 = "<" + bar1 + ">"
bar4 = "-" * nameLen
print( "nameLen = ", nameLen )
print( "nameLen4 = ", nameLen4 )
print( "nameLen2 = ", nameLen2 )
print( "x = ", x )
print( "name = ", name )
print( "bar1 = ", bar1 )
print( "bar2 = ", bar2 )
print( "bar3 = ", bar3 )
print( "bar4 = ", bar4 )
print()
- Carefully look at the output of your program. Because all the print statements are indented relative to the for statement, they are repeated for every name in the list. See how bar1 and bar2 do not change in length, but bar4 changes with each string.
- Here is your challenge for today: Make your program use a for-statement and a list of 3 names, and make it display a box around each name. The challenge here is that the box must fit exactly the length of the name, without extra spaces around.
------------- | Lea Jones | ------------- --------------- | Julie Fleur | --------------- ------------ | Anu Vias | ------------
Submission of Lab 1 Program on Moodle
- For this lab you need to submit your solution program on Moodle. This submission is graded and the grade counts toward your lab grade for this class (15% of the total grade).
- Go to this page and follow the directions for submitting your program.
- Deadline: Exceptionally, this week, the deadline is set to 2/6/05, to allow Add/Drop students to catch up.
- Grade:
- Program submitted with syntax errors: 90/100
- Program submitted without errors, but incorrect output: 95/100
- Program submitted with correct output: 100/100
Optional Challenge |
- This is not required today, but if you want some extra challenging coding to do, try making your program generate the output below.
+-----------+ | Lea Jones | +-----------+ +-------------+ | Julie Fleur | +-------------+ +----------+ | Anu Vias | +----------+
- You do not have to submit this program.
<showafterdate after="20150207 00:00" before="20150601 00:00">
Solution Programs
# CSC111 Lab1
# D. Thiebaut
# solution programs for Lab1
# Challenge #1
name = "Alex"
college = "Smith College"
age = 20
year = 2015
print( name, "goes to", college, "and is", age, "years old" )
print( name, "was born in", year - age )
print()
# Challenge #2
word1 = "hello "
word2 = "there! "
print( word1 * 4, word2 )
print( word1, word2*5, word1 )
print()
# Challenge #3
word1 = "hello"
word2 = "there!"
word3 = word1 + word2
print( word3*5 )
print( ("hello" + "there!") * 5 )
print()
# Challenge #4
for name in [ "Lea Jones", "Julie Fleur", "Anu Vias",
"Mickey", "Minnie", "Pluto" ]:
print( name )
print()
# Challenge #5
for name in [ "Lea Jones", "Julie Fleur", "Anu Vias",
"Mickey", "Minnie", "Pluto" ]:
print( name )
print( "-" * len( name ) )
print()
# Challenge #6
for name in [ "Lea Jones", "Julie Fleur", "Anu Vias",
"Mickey", "Minnie", "Pluto" ]:
print( "-" * len( name ) )
print( name )
print( "-" * len( name ) )
print()
# Challenge #7
for name in [ "Lea Jones", "Julie Fleur", "Anu Vias",
"Mickey", "Minnie", "Pluto" ]:
name2 = "| " + name + " |"
bar = "-" * len( name2 )
print( bar )
print( name2 )
print( bar )
print()
print()
# Optional Challenge
for name in [ "Lea Jones", "Julie Fleur", "Anu Vias",
"Mickey", "Minnie", "Pluto" ]:
name2 = "| " + name + " |"
bar = "-" * len( name )
bar = "+-" + bar + "-+"
print( bar )
print( name2 )
print( bar )
print()
print()
</showafterdate>