Python 3
Python 3
The assignment operators are operators that are used when assigning variables values, the most
commonly used operator being (=). Here is a list of examples:
Equal Sign: =
x = 5;
In this example, if you were to print out x, the value would be 5 because you assigned the variable x
equal to 5.
Add-Equal Sign: +=
x = 5;
x += 5;
In this example, if you were to print out x, the value would be 10 because you are adding the value of
5 onto the value of x. This statement is the same thing as saying x = x + 5 → x += 5.
Subtract-Equal Sign: -=
x = 5;
x -= 5;
In this example, if you were to print out x, the value would be 0 because you are subtracting the value
of 5 from the value of x. This statement is the same thing as saying x = x - 5 → x -= 5.
Multiplication-Equal Sign: *=
x = 5;
x *= 5;
In this example, if you were to print out x, the value would be 25 because you are multiplying the
value of 5 onto the value of x. This statement is the same thing as saying x = x * 5 → x *= 5.
Division-Equal Sign: /=
x = 5;
x /= 5;
In this example, if you were to print out x, the value would be 1 because you are dividing the value of
5 onto the value of x. This statement is the same thing as saying x = x / 5 → x /= 5.
Modules-Equal Sign: -=
x = 5;
x %= 5;
In this example, if you were to print out x, the value would be 0 because you are finding the remainder
in (5/5). This statement is the same thing as saying x = x % 5 → x %= 5.
Chapter 6
User Input
When writing code, you might run in the many times where you want the user to give the program a
value for the program to work with. So far, what we would do is just use a variable and change it
around, but the average user doesn’t have access to code, nor would they understand what to do with
it. That’s why you can have the user of the program input a value through the program. The input can
be any character and be used in any way that you wish. This can all be done with the input function. It
is used as follows.
varName = input("words")
In this case, varName is a variable, set to equal the keyword input, followed by the words that you
would like displayed in console. The result is simple.
Example:
print(age)
How this works is, when the code runs, it will display in console what you put inside the brackets of
input. After that is displayed you must write an input into the console and press enter. Whatever you
wrote in console, then becomes the value of age. This can be tested and witnessed with the above
code.
Example 2:
print(name)
Example 3:
print(names)
Input even works with lists. Input works with anything where the value you will input is a valid data
type for the variable.
Assignment
Create a program where you ask the user how many people they want to input. Then ask them the
name of each person and each person’s age. Then output all names and ages organized, the average
age, the youngest age, and the oldest age.
Solution
print("the average age of all the people is ", average / amount) # divides average by amount of people
to get real average
print("the oldest person is ", max(people.values())) # min function gets smallest number in values
print("the youngest person is ", min(people.values())) # max function gets largest number in values
Although we have already gone through strings in the variable section, there is still much more to talk
about the topic. Strings are quite commonly used in Python programming or any other programming
language for that matter. A string is a sequence of characters, or a line of text. A string is like an
object, meaning that it has properties that be called from it.
x = "A string!"
As you can see, we have surrounded the text with quotation marks to indicate to the Python interpreter
that it is a string and not an integer or any other value other than a string. Now let’s go over the
fundamental properties of a String object that can be used to gain extra information over the piece of
text or string.
String Length
A way to receive information on the amount of characters in a String is by doing the following:
test = "test"
amount = test.__len__()
print(amount)
The .__len__() function in a String object returns a value for the amount of characters within the
String object and assigns the value to the integer amount. As seen, the dot property is used to access
certain functions that are part of the test string object. The output in this sample code would be 4 since
“test” is 4 characters long. An alternate method of this could simply to surround the test variable with
the len function like so:
test = "test"
amount = len(test)
print(amount)
Concatenating Strings
Concatenating is the idea of joining two strings together. There are different ways of joining two
strings together. Here are a few examples:
Example 1:
text = "Hello"
text2 = " World"
print(text + text2)
Output: Hello World
Example 2:
text = "Hello"
text2 = "World"
print(text + " fun " + text2 + " of programming!")
Output: Hello fun world of programming!
As shown in the examples above, you are able to use the “+” operator to join two or more strings
together.
The value of “letter” contains the letter “H” because the inputted index of the string is 0 which is the
first letter of the string. The way the computer reads the string, is from 0 to the length of the string, not
1. To the computer, a string is a list of characters, so indicating a square bracket with an index is like
referring to a specific index of a character in a list of characters when really it´s a string variable.
text = "Hello"
doesItEqual = text is "Hello"
print(doesItEqual)
The value of doesItEqual is going to equal true in this case because the is keyword returns a bool
value (true or false) and the text does indeed equal to Hello. Later on, you will learn about “if”
statements, which can allow you to check whether or not something equals to something else. When
comparing two strings, it is recommended to always use the is keyword, but when comparing most
other data types you will use “==”. You will learn more about this later on.
Example 5 - string[x:y]
Since the position of the letter “W” is 6, if you say :6 - it will essentially divide that text starting from
that position. Therefore, the “justWorld” variable ends up containing the value of “World”. The colon
can be used to do many cool things. If you set the left side of the colon to a value and the right side as
a value, it will splice the string from the 6th index to another index depending on what you specified.
For example:
In this case, it will print out “Wo” because the position of W is 6 and the position of O is 8. You can
also use negative numbers which will make it go backwards starting from the end of the string instead
of the beginning. The technique used in the first example is checking all the values after the 6th index.
You can also do [:6] which will print out Hello instead.
Example 6 - strip():
As seen, the text variable has spaces in the beginning and the front. All the strip() method does is
simply delete any spaces on the left or right side of the string of text. Therefore, the trimmedText
would hold the value of “Hello World” instead of “ Hello World “.
There are many other String methods that can be used to manipulate a String and can be referenced
using the Python API. An API is simply a directory of all the methods/functionalities and
objects/classes of the Python platform that can be used to help the user program. It can be referenced
here:
https://docs.python.org/3/c-api/index.html