Permalink
Show file tree
Hide file tree
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
4 changed files
with
80 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# example-9.py | ||
# Common evaluations and their outcomes | ||
variable_1 = 4 | ||
variable_2 = 2 | ||
variable_3 = 3 | ||
variable_4 = 5 | ||
|
||
# Simple math functionality | ||
print(variable_1 + variable_2) | ||
print(variable_1 - variable_2) | ||
print(variable_1 * variable_2) | ||
print(variable_1 / variable_2) # / always becomes a floating number | ||
print(variable_1 % variable_2) | ||
print(variable_1 // variable_2) | ||
|
||
# Let's do some type checking to prove it | ||
print(type(variable_1 + variable_2)) | ||
print(type(variable_1 - variable_2)) | ||
print(type(variable_1 * variable_2)) | ||
print(type(variable_1 / variable_2)) | ||
print(type(variable_1 % variable_2)) | ||
print(type(variable_1 // variable_2)) | ||
|
||
# String concatination | ||
# this uses the + in an interesting way | ||
string_1 = "hey" | ||
string_2 = "you" | ||
print(string_1 + " " + string_2 + "!") | ||
print(type(string_1 + " " + string_2 + "!")) | ||
|
||
# interesting evaluations that still happen from left to right | ||
# mostly... | ||
print(variable_1 + variable_2 * variable_3 + variable_4) | ||
print(variable_4 + variable_1 * variable_2 - variable_3) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,6 @@ | ||
# Functions | ||
Functions are self contained objects that are | ||
Functions are self contained objects that are used to create code that we often want to reuse. The most arbitrary use of a function is in this code: | ||
|
||
{{ code_from_file("conditionals/example-1.py", 1, 10, execute=True) }} | ||
|
||
As you can see, we have created our first function. To create a function you must first use the def keyword followed by the name for the function. The naming conventions for functions are the same as they are for variables. However, it is good practice to have descriptive names for your functions so that you can recognise what they do at a glance. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# example-1.py | ||
# Introducing functions with functions | ||
def our_first_function(x, y): | ||
total = x + y | ||
print(total) | ||
|
||
our_first_function(5, 6) | ||
our_first_function(20, 25) | ||
our_first_function(7, 11) |