Notes 1
Notes 1
Functions
Functions are verbs or actions that the computer or computer language will already know
how to perform.
In your hello.py program, the print function knows how to print to the terminal
window.
The print function takes arguments. In this case, "hello, world" are the arguments
that the print function takes.
Bugs
Bugs are a natural part of coding. These are mistakes, problems for you to solve! Don’t get
discouraged! This is part of the process of becoming a great programmer.
Imagine in our hello.py program that accidentally typed print("hello, world" notice
that we missed the final ) required by the compiler. If I purposefully make this mistake,
you’ll the compiler will output an error in the terminal window!
Often, the error messages will inform you of your mistake and provide you clues on how
to fix them. However, there will be many times that the compiler is not this kind.
This edit alone, however, will not allow your program to output what your user inputs. For
that, we will need to introduce you to variables
Variables
A variable is just a container for a value within your own program.
In your program, you can introduce your own variable in your program by editing it to read
Notice that this equal = sign in the middle of name = input("What's your name? ")
has a special role in programming. This equal sign literally assigns what is on the right to
what is on the left. Therefore, the value returned by input("What's your name? ") is
assigned to name .
If you edit your code as follows, you will notice an error
name = input("What's your name? ")
print("hello, name")
The program will return hello, name in the terminal window regardless of what the user
types.
Further editing our code, you could type
Comments
Comments are a way for programmers to track what they are doing in their programs and
even inform others about their intentions for a block of code. In short, they are notes for
yourself and others that will see your code!
You can add comments to your program to be able to see what it is that your program is
doing. You might edit your code as follows:
Pseudocode
Pseudocode is an important type of comment that becomes a special type of to-do list,
especially when you don’t understand how to accomplish a coding task. For example, in
your code, you might edit your code to say:
# Print hello
print("hello,")
# Print the name inputted
print(name)
The output in the terminal, if we typed “Dev” we would be hello, Dev . Success.
Functions take arguments that influence their behavior. If we look at the documentation
for print (https://docs.python.org/3/library/functions.html#print) you’ll notice we can
learn a lot about the arguments that the print function takes.
Looking at this documentation, you’ll learn that the print function automatically include a
piece of code end='\n'. This \n indicates that the print function will
automatically create a line break when run. The print function takes an
argument called end` and the default is to create a new line.
However, we can technically provide an argument for end ourselves such that a new line
is not created!
We can modify our code as follows:
# Ask the user for their name
name = input("What's your name? ")
print("hello,", end="")
print(name)
By providing end="" we are over-writing the default value of end such that it never
creates a new line after this first print statement. Providing the name as “Dev”, the output
in the terminal window will be hello, Dev .
Parameters, therefore, are arguments that can be taken by a function.
You can learn more in Python’s documentation on print
(https://docs.python.org/3/library/functions.html#print).
Formatting Strings
Probably the most elegant way to use strings would be as follows:
More on Strings
You should never expect your user will cooperate as intended. Therefore, you will need to
ensure that the input of your user is corrected or checked.
It turns out that built into strings is the ability to remove whitespace from a string.
By utilizing the method strip on name as name = name.strip() , it will strip all the
whitespaces on the left and right of the users input. You can modify your code to be:
# Ask the user for their name
name = input("What's your name? ")
Rerunning this program, regardless of how many spaces you type before or after the name,
it will strip off all the whitespace.
Using the title method, it would title case the user’s name:
By this point, you might be very tired of typing python repeatedly in the terminal
window. You cause us the up arrow of your keyboard to recall the most recent terminal
commands you have made.
Notice that you can modify your code to be more efficient:
# Remove whitespace from the str and capitalize the first letter of each word
name = name.strip().title()
# Ask the user for their name, remove whitespace from the str and capitalize t
name = input("What's your name? ").strip().title()
x = 1
y = 2
z = x + y
print(z)
Naturally, when we run python calculator.py we get the result in the terminal window
of 3 . We can make this more interactive using the input function.
x = input("What's x? ")
y = input("What's y? ")
z = x + y
print(z)
Running this program, we discover that the output is incorrect as 12 . Why might this be?
Prior, we have seen how the + sign concatenates two strings. Because your input from
your keyboard on your computer comes into the compiler as text, it is treated a string. We,
therefore, need to convert this input from a string to an integer. We can do so as follows:
x = input("What's x? ")
y = input("What's y? ")
z = int(x) + int(y)
print(z)
The result is now correct. The use of int(x) , is called “casting” where a value is
temporarily changed from one type of variable (in this case a string) to another (here, an
integer).
We can further improve our program as follows:
x = int(input("What's x? "))
y = int(input("What's y? "))
print(x + y)
This illustrates that you can run functions on functions. The most inner function is run
first, and then the outer one is run. First, the input function is run. Then, the int
function.
You can learn more in Python’s Documenation of int
(https://docs.python.org/3/library/functions.html?highlight=float#int).
Readability Wins
When deciding on your approach to a coding task, remember that one could make a
reasonable argument for many approaches to the same problem.
Regardless of what approach you take to a programming task, remember that your code
must be readable. You should use comments to give yourself and others clues about what
your code is doing. Further, you should create code in a way that is readable.
Float Basics
A floating point value is a real number that has a decimal point in it, such as 0.52 .
You can change your code to support floats as follows:
x = float(input("What's x? "))
y = float(input("What's y? "))
print(x + y)
This change allows your user to enter 1.2 and 3.4 to present a total of 4.6 .
Let’s imagine, however, that you want to round the total to the nearest integer. Looking at
the Python documentation for round you’ll see that the available arguments are
round(number[n, ndigits]) . Those square brackets indicate that something optional
can be specified by the programmer. Therefore, you could do round(n) to round a digit to
its nearest integer. Alternatively, you could code as follows:
Though quite cryptic, that print(f"{z:,}") creates a scenario where the outputted z
will include commas where the result could look like 1,000 or 2,500 .
More on Floats
How can we round floating point values? First, modify your code as follows:
As we might expect, this will round the result to the nearest two decimal points.
We could also use fstring to format the output as follows:
This cryptic fstring code displays the same as our prior rounding strategy.
You can learn more in Python’s documentation of float
(https://docs.python.org/3/library/functions.html?highlight=float#float).
Summing Up
Through the work of this single lecture, you have learned abilities that you will use countless
times in your own programs. You have learned about…