Python Handbook For Beginners - стр. 7
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: