Whitespace
Whitespace just means characters which are used for spacing and have an “empty” representation. In the context of python, it means tabs and spaces. For example:
>>> name = 'John Smith'
There’s whitespace to the left and right of the equals operator. It’s not required, but it makes reading the code easier. The computer simply ignores whitespace when compiling the code. Within the string, however, the space is NOT whitespace, this is simply a “spacing” character.
TUESDAY EXERCISES
Variable Output: store the value 3 in a variable called “x” and the value 10 in a variable called “y”. save the result of x * y into a separate variable called “result”. Finally, output the information so it shows like the following:
>>> 3 + 10 = 13
Area Calculation: Calculate the area of a 245.54” x 13.66” rectangle. print out the result. hINT: Area is width multiplied by height.
Variables are used everywhere, and python makes it easy for us to incorporate them. Being able to store information is a key part of any program. Tomorrow we’ll look at how we can manipulate strings.
Wednesday: Working with Strings
It’s important to understand what you can do with string data types. The next two days cover working with and manipulating strings so that we may build a receipt printing program at the end of the week. We won’t worry about taking in user input but rather how to format strings, what a string index is, etc.
To follow along with this lesson, let’s continue from our previous notebook file
“Week_02” and simply add a markdown cell at the bottom that says, “Working with Strings.”
String Concatenation
When we talk about concatenating strings, I mean that we want to add one string to the end of another. This concept is just one of many ways to add string variables together to complete a larger string. For the first example, let’s add three separate strings together:
# using the addition operator without variables name = "John" + " " + "Smith" print(name)
Go ahead and run that cell below the markdown cell. The output we get is “John Smith”. We ended up adding two strings that were names and separated them with the use of a string with a space inside. Let’s go ahead and try to store the two names into variables first:
# using the addition operator with variables first_name = "John" last_name = "Smith" full_name = first_name + " " + last_name print(full_name)
Go ahead and run that cell. We get the exact same output as the previous cell; however, we used variables to store the information this time.
Do'stlaringiz bilan baham: |