.
Чтение книги онлайн.
Читать онлайн книгу - страница 4
2) Then we went down one line, wrote the print function, and passed the name of our variable in its brackets;
3) When we ran the code, Python calculated the value of the variable by adding 2 + 2;
4) The calculation returned 4, and Python assigned this value to the variable "result";
5) Then Python saw that we want to display the result's value because of the print (result) and print it accordingly.
As you can see, numbers and variables work well together in Python.
Let's practice some more and create your examples with numbers and variables, similar to what we just saw. Here are some boilerplate examples for you:
___________
result = 2+2
print(result)
___________
result = 10-5
print(result)
___________
result = 5*5
print(result)
___________
result = 10/2
print(result)
___________
7 Wrapping Up Chapter Three
In chapter three, we have accomplished the following:
1) Refreshed on what are integers and fractional numbers;
2) Learned basic math operators used in Python;
3) Learned how to divide a number without remainder in Python;
4) Got to know the order of calculation in Python;
5) Combined numbers and variables in math examples.
8 Chapter Three Test
1. What types of numbers are there in Python?
1) There are three types of numbers in Python: Integers, almost whole numbers, and fractional.
2) There are two types of numbers in Python: Integers and half integers.
3) There are two types of numbers in Python: Integers – integer and fractional numbers.
2. How does Python divide 10 by 5? (Multiple selection)
1) 10:5
2) 10/5
3) 10-5
4) 10 * 5
5) 10 // 5
3. Arrange the parts of the code so that you get the integer 4.
five
10*2
//
4. This code should display 5. But what is missing?
result = 10/2
_____(result)
CHAPTER FOUR: STRINGS
1 Strings in Python
Do you remember the messages we displayed in chapter one, like "Hey! This is my first line of code!" or "Once upon a time.." They are all strings. In other words, a string is a data type that has a text format, or just a text, enclosed in quotes. Hence, to create a string, we have to type a text and enclose it in quotes.
2 Strings And Print Function
Let's write a code that would display the string – "Now I know what a string is in Python." You already know how to display strings in Python, right? Here is the code.
print("Now I know what a string is in Python.")
Write the above line into the console and hit run. Now it's your turn. Think of some different string, rewrite and run the above code.
3 Saving Strings in Variables
Do you remember our first variable: film = "Super Cat"? Now you know that the value of the variable – "Super Cat" had a string format, as we enclose it in quotes. Let's take our knowledge further, create a message in a string format, save it in a variable, and display the variable value on your screen using the print function. Let me do it first:
message = "Hello, Super Cat!"
print(message)
Run this code in the console. What do you see? Now it's your turn. Following the above example, think of any message. Right it down as a string. Save it in a variable and display the variable value using the print function. What you've got?
4 Strings Concatenation
We can combine strings. There is a method for that, called – concatenation. All we have to do to concatenate strings is to put addition operator + between them. Easy right? If so, let's get concatenating!
"Super Cat" + "saves the day!"
Now, let's display the two concatenated strings:
print("Super Cat" + "saves the day!")
Write this code into the console and hit run. Have you noticed something weird? Hmm. It looks like our lines stuck together. No worries. We can quickly fix that. There are several ways to do it but let's stick with the most simple – leave a space between the quote and string:
print("Super Cat" +" saves the day!")
Have you fixed that? Hit run again.
5 Strings Concatenation And Variables
Note that we can concatenate a string only with another string. Or a string-formatted value. For example, if we created a variable and assigned a string-formatted value to it, we can also concatenate such a variable with another string. In the example below, I created a variable (hero) and assigned a string-formatted value (Super Cat) to it. Then I displayed this value in concatenation with another string (VS Duper Dog).
hero = "Super Cat"
print(hero + " VS Duper Dog")
Try this code out. What message do you get? Now, change the code as you like. You can even concatenate more than two strings! Alter and run the code. See how the output changes!
6 String Formatting
Now let's talk about what string formatting is. We have already learned