Assignment 1: Getting started with Python
In order to get you started off on the right foot for Hackbright, we have a series of assignments focused on building basic Python skills so that you have a foundation of general Python knowledge, and you have access to resources when there are areas you need to review as you progress through the program.
Each assignment consists of
Reading and Interactive practice (1-2 hours)
Coding Homework (~30 minutes)
Please read carefully and try to learn something new during each assignment. While some or even many of the topics in prework will be a review for you, challenge yourself to attain fluency in each of the topics presented. Continue to practice these fundamental skills beyond each assignment, especially if the assignment did contain new material for you.
In this section, we’ll start at the very beginning, but we expect it to be at least partially a (useful) review for you.
Preparation
Sign up for Replit
Replit is an online integrated development environment (IDE) that allows users to code, collaborate, and experiment.
Replit is cloud based, so you can access it from any web browser, no software installation needed. As a development environment, it provides a workspace for you to write code, run it, and debug it. Replit supports a huge variety of programming languages, so you can code in pretty much anything you like (like Python). Additionally, you can easily share projects (called repls) with others and collaborate with them in real time.
Assuming you don’t already have an account, you can sign up for one here: replit.com/signup.
Setting Up a repl
Once logged in, click the grey + Create Repl button on the top left, then select Choose a Template. In the box under Template, type in and select Python. You can give the repl a name (like “Hackbright Prework”), then click the blue + Create Repl button.
This will take you to the screen on which you’ll be doing your work. First, close the pay-per-use AI assistant panel.
Replit tries to be helpful by using an autocomplete feature while you are coding. This can be useful sometimes, but more often gets in the way of our typing and learning. So we’re going to disable it!
Click the four boxes icon in the left side menu to view All Tools. Scroll down the menu or use the search box to find User Settings, then select it. Scroll down this section and the toggle the buttons to disable the following:
AI Code Repair
AI Code Completion
Code Intelligence
Now, close the User Settings panel, and click the folder icon on the left to expand your directory. You’ll now have a blank Python file on your screen called main.py.
Set Tab Size to 4 Spaces
Because whitespace is significant in Python, it is important that we standardize the size of our indents. If we have a mix of different indent sizes (e.g. 2-space indentation in some areas, 4-space indentations in others), Python can’t interpret the program. It is strongly conventional in Python to have 4-space indentations.
You can adjust your repl settings to always translate tabs to 4 spaces (which means that when you hit the tab key, it will automatically indent your line by 4 spaces.) To do this, click where it reads · Spaces: 2 in the bottom right of your screen.
Then, set the Indentation unit to 4 spaces, and click Save.
Hello, World!
It’s a tradition in programming that the first program you write in a new language displays “Hello, World!” So let’s do it! This will help us get comfortable with the different parts of the repl interface.
Click into line 1 of main.py and type the following (don’t copy/paste!):
def greeting():
print("Hello, World!")
greeting()
Click the green Run button at the top of the screen and you should see the following:
main.py is where all your code will be typed.
On the right side of the screen, you’ll see the Console—where Hello, World!
should have just appeared.
Reading and Exercises
Please read and complete the accompanying exercises for the following:
1. Think Like a Computer Scientist: General Introduction
Read the following sections in How to Think Like a Computer Scientist: General Intro:
1.1 to 1.5
1.11 to 1.12
2. Think Like a Computer Scientist: Python Data
Read the following sections in How to Think Like a Computer Scientist: Simple Python Data:
2.1 to 2.11
Then, complete the exercises in section 2.13
3. Think Like a Computer Scientist: Strings
Read the following sections in How to Think Like a Computer Scientist: Strings
9.1 to 9.10
9.13
Then, in section 9.22, complete exercises 1 and 2
4. Python String Formatting
Read the following sections in PyFormat:
Basic formatting
Value conversion
Padding and aligning strings
Truncating long strings
Combining truncating and padding
Numbers
Padding numbers
Then, read Python 3’s f-Strings: An Improved String Formatting Syntax (Guide).
Coding Practice
Complete your coding practice on Replit
Replit provides a consistent, predictable environment for your prework. If you are having any difficulty using it, please email your admissions counselor and we’ll be happy to help.
Directions
Using the repl you created above, create a program that accomplishes the following tasks.
1) Handle User Input
Ask a user to fill in some information about themselves (using input() to gather the information and assign it to variables). Use f-strings, input(), and variable assignment that you learned from this section’s readings.
Your program should ask and store the user’s answers to the following questions:
What’s your first name?
What’s your last name?
What’s your favorite color?
How many pets do you have?
And then have it print a greeting that includes those values once they are collected.
It should look similar to this when you run it in repl.it:
Tell us your first name: Balloonicorn Tell us your last name: Thegreat Tell us your favorite color: purple Tell us how many pets you have: 2000 Great! So your name is Balloonicorn Thegreat, your favorite color is purple, and you have 2000 pets. Nice to meet you!
2) Formatting text
Copy the following line of python into your repl.
paragraph = """An invitation to dinner was soon afterwards dispatched;
and already had Mrs. Bennet planned the courses that were to do credit
to her housekeeping, when an answer arrived which deferred it all. Mr.
Bingley was obliged to be in town the following day, and, consequently,
unable to accept the honour of their invitation, etc. Mrs. Bennet was
quite disconcerted."""
average_rating = 4.56789123
Use string formatting to print the following (bonus points if you use f-strings instead of str.format()!):
The first ten characters of the variable paragraph followed by an elipsis (
...
)The first 3 digits of the average_rating variable.
The desired output is:
An invitat...
4.57
3) Discussion Question
Using Python comment syntax (prepend the line with a #
) at the bottom of
your repl answer the following question:
Why is the following Python code invalid?
first_name = "Sally"
first_name[0] = "C"
Submitting Your Work
When you’ve finished your work, turn it in using the following steps:
Copy the URL from your browser address bar. It should look something like this:
https://replit.com/@username/hackbright-prework
Send it in an email to the admissions team at admissions@hackbrightacademy.com.
We may follow up with some questions or suggested study areas. Otherwise, we’ll see you on your first day of class!