or
or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up
Syntax | Example | Reference | |
---|---|---|---|
# Header | Header | 基本排版 | |
- Unordered List |
|
||
1. Ordered List |
|
||
- [ ] Todo List |
|
||
> Blockquote | Blockquote |
||
**Bold font** | Bold font | ||
*Italics font* | Italics font | ||
~~Strikethrough~~ | |||
19^th^ | 19th | ||
H~2~O | H2O | ||
++Inserted text++ | Inserted text | ||
==Marked text== | Marked text | ||
[link text](https:// "title") | Link | ||
![image alt](https:// "title") | Image | ||
`Code` | Code |
在筆記中貼入程式碼 | |
```javascript var i = 0; ``` |
|
||
:smile: | ![]() |
Emoji list | |
{%youtube youtube_id %} | Externals | ||
$L^aT_eX$ | LaTeX | ||
:::info This is a alert area. ::: |
This is a alert area. |
On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?
Please give us some advice and help us improve HackMD.
Syncing
xxxxxxxxxx
CS2 Lab 5 2024, Python I 🎰
<<< Back to main site
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →Background 🎰
Project/Lab Description 🎰
Lab Goals:
Downloading Python
The final part of the course will be using Python. If you do not have Python 3 installed, you can follow the instructions on this website to download the correct version for your device. If you believe you have already downloaded Python, you can check which version of Python you have installed by running
python3 --version
in your terminal / command prompt.Download the VSCode Python Extension
In addition to downloading the software, please download the Python extension on VSCode as well. Follow the steps below to download the extension:
Once you have that extension installed, you should be set to code in VSCode! When writing in Python, remember to have your file end in .py (
example.py
).Tasks 🎰
Task 1 Double click your
CS2
folder on your desktop, and enter your folder forPython
. Then, create a new folder inside calledPython Lab I
. All your files for the lab should live in this folder.Task 2 Download the stencil file for this lab, and put it in your
Python Lab I
folder you just created!Task 3 Open up your
Python Lab I
folder by dragging the folder into Visual Studio Code, or by opening up Visual Studio Code, clickingOpen
under theStart
header, and finding the folder in your file system.Once the folder has opened, you should see our stencil file,
CS2_2023_Lab5_Stencil.py
on the lefthand side of the screen. Double click the file, and you should see that it already contains some lines of code. Now we'll learn how to run that code!Task 4 In the file provided, we've written a line of code containing a print statement. Run the code by clicking the play button on the top right corner of the screen to see what happens.
This program uses the print method to print the string "hello, world!" in the terminal. The terminal should appear at the bottom of your VSCode window once you click run. Methods (also called functions) are blocks of organized, reusable code that perform a single action. You’ll write your own function later!
Math Operations Practices
In Python, text between quotation marks is known as a string. For example, 6 is an integer, but "6" is a string.
Here is a list of some basic math operations in Python. More information about math operations in Python can be found here.
+
addition-
subtraction*
multiplication/
division%
modulo/remainder after division()
parentheses/indicate order of operationsTask 5a Try running the following code by copy-pasting this line into line 5 of your stencil file.
Task 5b The code block on line 6 contains a python comment. Prepending a line with # produces a comment. Try removing the comment and printing out your own math equation, as above.
Concatenation Practice
The + operator can be used for addition, as above, but can also be used with strings. This operation is called concatenation.
Task 6 Try running the following line of code:
Because the + operator has different roles when used with numbers versus strings, it's important to always make sure you're only using it with just numbers or just strings, not both!
IMPORTANT NOTE: Please make sure that your quotation marks look straight (like in the code block above). If they appear angled or curly, delete them and retype them within VSCode.
Variables Practice
In Python, you can store data in variables. More information about variables in Python can be found here.
Task 7a Try running the code below. In this variables practice section, it is important to run all of the code blocks in order, as some depend on the ones above.
Task 7b Variables can also be used with other variables. To see this, use your stencil file to
List Practice
In Python you can make lists to group things together. More information about lists in Python can be found here.
The items inside of the square brackets can be any combination of strings, numbers, or other lists. Each item in your list must be separated by a comma.
Each item in a list has an associated list number, or index. List indexing starts at zero. In
casino_games
,"blackjack"
is at index zero,"roulette"
is at index 1, and"poker"
is at index 2. This is called zero indexing.To access items in your list, you use bracket syntax, like so:
where
my_list
is the name of your list andindex_number
is an integer and the index of the item you’re trying to access.Task 8 In your stencil file, write code to print the second index/third item in
casino_games
.Task 9 We can alter items in the list in a similar manner. Try running the code below.
To add an item to the end of your list you can use the append function as so:
Try adding your own casino game to the end of the list in your stencil file!
Conditionals Practice
Conditional expressions use logic to determine which path of action to take based upon conditions set by the programmer. You used them in JavaScript and they are pretty similar in Python, with slightly different syntax. More information about conditionals in Python can be found here.
Conditional statements are often set up in the following way:
Note that "elif" is short for "else if." Only the first if block is necessary in conditional statements; there can be none, one, or multiple elif blocks after an if block, and there can at most be one else block following an if block. This depends on what you’re trying to achieve in your code.
Task 10 Try running the following code with different input to see what happens.
Some important things to remember for conditional statements:
Alignment is VERY important in Python! Make sure that you follow the alignment (proper indentation) seen throughout the lab.
The following is a list of conditional operators:
<
less than<=
less than or equal to>
greater than>=
greater than or equal to==
equal to!=
not equal to (this is different from =, which is an assignment operator)A brief note about and, or, not:
Task 11 Try running the following examples:
Task 12 Try editing the variable values below so that the code prints "I love blackjack!":
Putting It All Together :)
Task 13
Zach has recruited 12 friends to shuffle decks of cards. Each friend can shuffle 4 decks per hour.
In the stencil file, write code to solve this problem:
a variable num_friends with value 12
a variable decks_per_hour with value 4
a variable total_time that stores how long it will take (in hours) for Zach's friends to shuffle 240 decks.
If you're having trouble, look back at Math Operations and Variables Practice!
Task 14
In the stencil file, add onto the code below in the following manner:
"clubs"
."clubs"
with a different suit.Optional: figure out how to remove items if you disagree with our list and print the ammended list.
If you're having trouble, look back to Lists Practice!
Task 15
In the stencil file, create a program that checks for how many cards a player has, from 1 to 52.
To do this, we must first assign the initial number of cards in the store. Write a value called total_cards and assign it a value from 1 to 52.
Now, we will write some conditional statements to print out a message based on the value of total_cards. Based on the availabe number of cards, several things can happen:
If you're having trouble, look back to Conditionals Practice!
Hand-In 🎰
To Hand-In Lab 5:
Come to hours and show your TA two things for checkoff:
Congrats! You just finished your second-to-last CS2 lab! 🎰
If you have any issues with completing this assignment, please reach out to the course HTAs: cs0020headtas@lists.brown.edu
If you need to request an extension, contact Professor Stanford directly: don.stanford@gmail.com