Skip to content

Commit

Permalink
new lessons, readme, and output checking
Browse files Browse the repository at this point in the history
  • Loading branch information
Latkecrszy committed Jul 5, 2021
1 parent 9cb377b commit 48c4685
Show file tree
Hide file tree
Showing 24 changed files with 505 additions and 26 deletions.
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,14 @@
# explorepython
# ExplorePython
## Learn Python, by writing Python.
ExplorePython is a fun, interactive way to learn and explore the Python language.
With bite sized mini lessons on topics from variables to Object Oriented Programming,
ExplorePython provides the tools that every budding developer needs to succeed.
After each lesson, you'll get the chance to test your knowledge by writing and running code relating to the topic you just learned.
You then have your solution evaluated on the spot before moving on.

### Link to Website
https://explorepython.dev
![Wesbite](static/images/website.png)

ExplorePython is run on a flask server.
To host locally, just clone the repository, install the requirements, and run `app.py`.
13 changes: 9 additions & 4 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,21 @@ def lesson():
if name == "null":
name = "intro"
content = markdown(open(f"lessons/{name}.md").read())
code = '\n'.join(json.load(open("resources/code.json"))[name])
expected_output = json.load(open("resources/expected_output.json"))[name]
return jsonify({"lesson": content, "code": code, "expected_output": expected_output, "name": name})
if name != "final":
code = '\n'.join(json.load(open("resources/code.json"))[name])
expected_output = json.load(open("resources/expected_output.json"))[name]
hints = json.load(open("resources/hints.json"))[name]
else:
code = ""
expected_output = {"output": [], "input": {"includes": [], "excludes": []}}
hints = []
return jsonify({"lesson": content, "code": code, "expected_output": expected_output, "name": name, "hints": hints})


@app.route("/arc-sw.js")
def arc():
return send_file("arc-sw.js")



if __name__ == '__main__':
app.run(port=5004)
10 changes: 5 additions & 5 deletions lessons/booleans.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
# Booleans
## What is a boolean?
A boolean is the simplest data type: it is a True or False value.
The only two values that it can have is True or False. Anything else, and it is no longer a boolean.
A boolean is the simplest data type: it is a true or false value.
The only two values that it can have are `True` and `False`. Anything else, and it is no longer a boolean.

## How do I make a boolean?
Booleans are unique in that they are created using a word, but not a string.
The representation of true in python is `True`, and false is `False`.
The representation of true in Python is `True`, and false is `False`.
Python is case sensitive, meaning that capitalization matters, so make sure to capitalize the `T` in `True` and the `F` in `False`.
So, to make a boolean all you need to do is make a variable with a value of either `True` or `False`.
Here's an example: `x = True` creates a boolean called `x` with a value of `True`. `x = False` would make it `False`.

## Why would I use booleans?
The use case of a boolean is usually in a loop or an if statement, both of which will be covered in later lessons.
In a nutshell, an if statement determines if a condition is met, and a loop runs until a condition is not met.
Booleans are useful in this case, because you can directly check if a value is true or false.
Booleans are useful in this case because you can directly check if a value is true or false.
We'll go into more detail about this in the next lesson.

## Can other types be booleans?
Expand All @@ -23,7 +23,7 @@ Just like we have `str()` for strings, `int()` for integers, and `float()` for f
`truthy` and `falsey` values. These are what determine if something is converted to `True` or `False`.
As a rule of thumb, if something has content (like a non-empty string or a number other than 0) then it is `truthy`,
and if it does not have content (like an empty string), then it is `falsey`. There are two exceptions to this, however:
`1` an `0`. Instead of being `truthy` or `falsey`, they are `True` or `False`!
`1` and `0`. Instead of being `truthy` or `falsey`, they are `True` or `False`!
If you type in `print(1 == True)` in the console (bottom right box), it will display `True`.
This is because `1` is exactly equal to `True`, and `0` is exactly equal to `False`.
We will make use of these `truthy` and `falsey` values when we get to making loops and if statements.
Expand Down
Empty file removed lessons/dictionaries.md
Empty file.
9 changes: 9 additions & 0 deletions lessons/final.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Congratulations!
You did it! You made it through all the lessons ExplorePython has to offer.
This is certainly an accomplishment, and you can give yourself a pat on the back for that.
Through these lessons, you've learned the fundamentals of the Python language, including how to make and use variables,
the data types strings, integers, floats, booleans, and lists, create your own functions, use if statements, and make
loops! We at ExplorePython are so proud of you. Of course, there's always more to learn about Python, and as such
more lessons are being added frequently, so don't forget to check back for more soon. You have shown mastery of the
basics of Python by reading through 11 lessons on different topics and writing code to show your knowledge for each.
The sky's the limit for you now, so go off and do some coding!
51 changes: 51 additions & 0 deletions lessons/for_loops.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# For Loops
## What are for loops?
In a previous lesson, we learned about while loops and determined that their main use case is to do things an
unknown number of times. For loops are for the opposite purpose: when you want to do something for a specific number
of times. If I want to print out `"Hello!"` 5 times, then the easiest way to do that would be with a for loop.
But for loops also have a different use case - looping through iterables. Iterables are anything that can be iterated,
or looped through. For example, lists are iterables because we can iterate through their items. Strings are also iterables,
which is why they can be turned into lists. With for loops, we can go through any iterable and run code on each item
individually. For example, if `x = ["item0", "item1", "item2"]`, then I could loop through `x` and run the same code on
all the different items, `"item0"`, `"item1"`, and `"item2"`. This will make more sense once you see how for loops are
written.

## How do I write a for loop?
The syntax of a for loop is a bit different from any we've seen so far. It introduces a new keyword that we will also
use later: `in`. `in` is used to check if something is in something else. For example, we could check if `"string"` was
in the bigger string `"I am a string"` by saying `if "string" in "I am a string":`. `in` is also used in for loops to
loop through all the items in an iterable. Here is an example of a for loop looping through a list:
```python
fruit_list = ["bananas", "apples", "pears", "watermelons", "oranges"]
for fruit in fruit_list:
print("I love eating " + fruit)
```
Let's break down this code:
- On line 1, we are creating a list called `fruit_list` with various fruits in it.
- On line 2, we are making our for loop to loop through all the fruits in `fruit_list`. You'll notice that we are also
creating a variable in the for loop called `fruit`. This will be the item that we are currently using in the list.
The first time the loop runs, `fruit` will be `"bananas"`, because that's first in `fruit_list`. Next, `fruit` will be
`"apples"`, etc. Starting with `for` indicates that this is a for loop, and we always end with a colon just like an
if statement or while loop.
- On line 3, we are printing out `"I love eating "`, and then whatever `fruit` is in that loop iteration. You may
remember that in a previous lesson, we saw that we can add strings together to make a larger string, and that's
exactly what we're doing here - just adding the fruit to the end of the string `"I love eating "`.

This is a lot to take in, but in a nutshell, the syntax to make a for loop is `for variable in list_name:`, and the loop content is
indented inside the loop.

## The range builtin
`range` is a builtin that creates a list-like object full of numbers that we can loop through. `range` is often used to
do something a certain number of times, even when you don't want to loop through a list of items. Going back to the
example in the first section, if I wanted to print out `"Hello!"` 5 times, I could do it with `range` like this:
```python
for x in range(5):
print("Hello!")
```
Notice that I put the number of times I want the loop to run in the parentheses after `range`. `x` in this case is just
a random variable name, and `print("Hello!")` will print `"Hello!"` every time the loop runs.

## You try!
Now, you get to try creating a list and looping through it. Make a list with at least 3 items and make a for loop to
print out the name and index of each item. Feel free to refer back to the previous lesson to remember how to get the
index of the item. Good luck!
52 changes: 52 additions & 0 deletions lessons/functions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Functions
## What are functions?
As mentioned previously, a function is a piece of code that takes input and returns output based on that input.
If you think that sounds daunting to use and write, remember that we've already been using some - all the builtins
we've been using are pre-made functions! In this lesson, we'll be learning how to make our own custom functions.
The syntax for making a function is pretty simple `def function_name(parameters):`. Take a look at this example of a
function to add two numbers and how we use it.
```python
def add_numbers(number1, number2):
return number1 + number2

total = add_numbers(5, 10)
print(total)
```
Let's break down this code.
- On line 1, we are creating our function. Notice how we start with `def`. `def` stands for define, and lets Python know
that we're making a function. Next, we have `add_numbers`. This is the name of the function, and it's what we'll use
to call it later. (Calling a function means using it.) Inside the parentheses are the function parameters.
Parameters are information that is passed into the function to be used inside it. In this case, our parameters are
two numbers that we add together inside the function. Finally, we end it all with a colon to let Python know we're done.

- On line 2, we return information from the function. We do this with a new keyword: `return`. `return` designates
which information is passed out of the function and back into the code. For example, when we use `str()`, that
is a function that returns a stringified version of whatever parameters we pass into the parentheses. In our function,
we're returning the sum of the two numbers that were passed in as parameters.

- On line 3, we're creating a variable called `total` and giving it the value of whatever our function returns. In this
case, its value will be `15` because we passed in `5` and `10` as the parameters, and their sum is being returned.
Notice how we separate our parameters with a comma. This is important, as otherwise Python won't know where to separate them.

- On line 4, we're printing out `total` to the console.

And that's it! That's all that you need to make a function. As you get more experience writing and using functions,
the process of doing so will become easier and faster each time.

## What is scope?
Scope in programming represents the places in which certain variables can be used. Here's an example: the main part of your code
is the global scope, meaning that the variables created there can be accessed and used anywhere. However, inside of functions is a
different scope. We can still access all the variables from the global scope, but the global scope cannot access variables
in a function's scope. This has an important impact when editing variables. If we have `x = 5` in the global scope
(not inside a function), and we want to change `x` to `7` inside of our function, then we have to return the new value
for x from our function (using the `return` keyword) and change `x` to `7` on the global scope. Otherwise, the variable
will be changed inside the function but not in the outside code, resulting in confusion and problems. To sum this all up,
editing or creating variables inside a function will not edit or create them in the rest of the code. To edit or create
them on the outside, you must return them from the function and change them in the global scope.

## You try!
It's your turn to try making a function of your own. Your function will be called `add_strings`, and it will take in
two strings as parameters. Inside the function, add the two strings together and return the result. In the outside code,
call your function (don't forget to pass in the two strings for parameters) and print the output. Keep in mind that
you must create your function before you use it. Trying to use the function and create it after will result in an error.
Good luck!
71 changes: 71 additions & 0 deletions lessons/if_statements.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# If Statements
## What is an if statement?
If statements are your first look at conditionals - statements that evaluate to booleans, `True` or `False`.
We took a look at booleans last time, and now you'll learn about a major application for them.
If we want to determine the value of something in our code, we do that with if statements.
For example, say we used `input()` to ask a user for their age. We then wanted to check how old they were,
to send a different message to each age group. To do that, we would use an if statement.
Here's how one such program could look:

```Python
age = input("How old are you? ")
age = int(age)
if age > 20:
print("Welcome, person over the age of 20!")
elif age < 20:
print("Welcome, person under the age of 20!")
else:
print("Welcome, 20 year old!")
```

Let's break this program down into small parts:
- On the first line, we define `age` as whatever the user responds with to the question `How old are you?`
- On the second line, we convert `age` to an integer, because `input()` always gives back a string, and we want their age to be a number.
- On the third line, we begin our if statement. `if age > 20` is our statement.
In this line, we have two parts: the required syntax (`if`), and the conditional (`age > 20`).
You may recognize `>` from math as being the greater-than sign. That's exactly what it means here.
This statement checks if `age` is greater than `20`.
Finally, we end the statement with a colon to let Python know we're done writing it.
- On the fourth line, we put content inside the if statement. Notice how it's indented
(has whitespace before it, usually made with the tab key) more than the other lines.
This is to tell Python that the line of code will **only** be run if the above if statement is true.
If `age` is not more than 20, then the line indented below it will not run.
- On line 5, we use the `elif` keyword instead of `if`. `elif` stands for `else if`, and must **always** follow an if statement.
Notice how it's not indented, meaning that it's not inside our first if statement - instead, it is the code following it.
The purpose of `elif` is to run if the first if statement is not true. If `age` is not greater than 20,
the code will check if it is less than 20, because `elif age < 20` has a conditional of `age < 20`.
However, if `age` is more than 20, and the first if statement (`if age > 20`) is true, then `elif age < 20` will never run.
- On line 6, we put content inside the elif statement. This works in the same way that the `if` does.
If the conditional (`age < 20`) is not true, the content indented inside it will not run.
- On line 7, we get to the final part of the if statement: `else`.
This part has no conditional on it because it means 'if nothing above was true, run this code'.
Basically, if all the previous conditionals were false, we'd do the code inside the `else`.
But if any statements above were true, the code in the `else` will never run.

Whew! That was a lot to take in. If statements may seem complicated at first, but you'll quickly get the hang of them.

## How do I check things other than greater than and less than?
The greater than (`>`) and less than (`<`) signs are just two examples of relational operators (operators used in if statements).
Here is a guide to all the operators you can use:
- `>` - Greater than. Example: `if x > 5:`
- `<` - Less than. Example: `if x < 5:`
- `>=` - Greater than or equal to. Example: `if x >= 5:`
- `<=` - Less than or equal to. Example: `if x <= 5:`
- `==` - Equal to. Example: `if name == "foo":`
- `!=` - Not equal to. Example: `if name != "bar"`

## Booleans and if statements
In the first section, it was mentioned that conditionals evaluate to true or false. For example, the expression
`5 == 5` would evaluate to true, while `5 == 4` would evaluate to false. But what if we just have a boolean?
Well, then we don't need any evaluation! Let's say `x = True`. If we want to check if x is True, we can just write
`if x:`, because it's already True. You can do something similar with variables that are false. If `x = False`, then
we can write `if not x` to check if it's false. You may remember that in the previous lesson, we learned about truthy and
falsey values. These can also be used with if statements - to check if a variable is truthy, you can do `if variable_name`,
and to check if it's false you can do `if not variable_name`.

## Your turn!
It's now your turn to make an if statement of your own. For this, you'll take user input and ask the user for their name.
Then, use an if statement to check if they put in your name. If they did, print out a message about it.
If they didn't, also print out a different message about it. Remember that all if statements have to end in a colon (`:`).
You'll want to use `else` to check if they put in any other name. Good luck, and read the examples above if you need help.

Loading

0 comments on commit 48c4685

Please sign in to comment.