Presentation is loading. Please wait.

Presentation is loading. Please wait.

More about comments Review Single Line Comments The # sign is for comments. A comment is a line of text that Python won’t try to run as code. Its just.

Similar presentations


Presentation on theme: "More about comments Review Single Line Comments The # sign is for comments. A comment is a line of text that Python won’t try to run as code. Its just."— Presentation transcript:

1 More about comments Review Single Line Comments The # sign is for comments. A comment is a line of text that Python won’t try to run as code. Its just for humans to read. Comments make your program easier to understand. When you look back at your code or others want to collaborate with you, they can read your comments and easily figure out what your code does.

2 More about comments Multiple Line Comments
The # sign will only comment out a single line. While you could write a multi-line comment, starting each line with #, that can be a pain. Instead, for multi-line comments, you can include the whole block in a set of triple quotation marks Example: “””Sipping from your cup is like the Holy Grail””” The syntax highlighting for single line comments is red, and for multiple line comments is green

3 Chapter 2: Types Variables and Simple i/o
Using Quotes Inside Strings Can use ‘ ‘ or “ “ to create string values ‘Game Over’ = “Game Over” In the print(“Program ‘Game Over’ 2.0”) the single quotes are part of the string, double quotes are like bookends telling the computer where the string ends and begins. “ “ – can used as many single quotes as you want inside ‘ ‘ - can use as many double quotes as you want inside Once you have used one kind as a bookend, you can’t use it again in the string.

4 PRINTING MULTIPLE VALUES
You can print multiple values with a single call to the print function, just like multiple arguments separated by commas. Example: print(“same”, “message”, “as before”) This command will print: same message as before **Note each value is printed with a space separator – this is a default of the print function.

5 You can also start a new line after each comma separator.
Ex. print (“Just”, “a bit”, “bigger”) Prints -> Just a bit bigger

6 Specifying a Final String to Print
By default the print function prints a newline character as it’s final value, so if you use the print function a second time, it will print text on a new line. Function Parameter -> end = “ “ will print a _____ space as the final value (instead of a new line) so when you do print(“Here”, end = “ “) print (“it is…”) print -> Here it is… * This gives you greater flexibility in output*

7 Triple quoted strings “”” “””
-can span multiple lines and they print on the screen exactly the way you type them. Ex. “””GAME OVER “”” see page 20

8 Using Escape sequences with strings
Escape Sequences allow you to put special characters into your strings Gives you greater control and flexibility over text you display Consists of a backslash (\) followed by another character

9 Escaping characters There are some characters that cause problems.
For example: ‘There’s a snake in my boot’

10 Escaping Characters This code breaks because Python thinks the apostrophe in ‘There’s’ ends the string. We can use the backslash to fix the problem, like this: ‘There\’s a snake in my boot’ You try it: Fix the string in the editor: ‘There’s a snake in my boot’

11 Selected escape sequences
Description \\ Backslash. Prints one backslash \’ Single quote \” Double quote \a Bell. Sounds the system bell \n Newline. Moves cursor to beginning of the next line. \t Horizontal tab. Moves cursor forward one tab stop

12 Creating web apps, games, and search engines all involve storing and working with different types of data. They do so by using variables. A variable stores a piece of data and gives it a specific name. You try it: set my_variable equal to 10.

13 You just stored a number in a variable
You just stored a number in a variable. Numbers are one data type we use in programming. *You can also change the value of variables by “reassigning” it by using the same variable name but different values

14 You’ve been reassigned
Now you know how to use variables to store values. Say my_int = 7 You can changed the value of that variable by “reassigning” it, like this: my_int = 3 Try it and see! Change the value of my_int from 7 to 3 in script mode. Then print my_int

15 assigning variables to strings
Another useful data type is the string. A string can contain letters, numbers, and symbols. Thus far we have only been printing strings, but like numbers you can also assign them to variables. name = “Ryan” age = “19” food = “cheese”  In the above example, we create a variable name and set it to the string value “Ryan” We also set age to “19” and food to “cheese” Strings need to be within quotes. You try it: Create a new variable brian and assign it the string “Hello life!”

16 Printing variables Great! Now that we’ve printed strings, let’s print variables that stored strings YOU TRY IT: Declare a variable called the_machine_goes and assign it the string value “Ping!” Go ahead and print the_machine_goes in the next line

17 ANSWER: the_machine_goes="Ping!" print (the_machine_goes)

18 Concatenating & Repeating Strings, Line Continuations
Silly Strings! Concatenating & Repeating Strings, Line Continuations

19 String Concatenating There are things you can do with entire strings themselves like combine two separate strings into a larger one or repeat a single string as many times as you like.

20 print(“Life”+”of”+”Brian”) This will print Life of Brian The + operator between strings will ‘add’ them together, one after the other. Notice that there are spaces inside the quotation marks after Life and of so that we can make the combined string look like 3 words. Combining strings together like this is called concatenation. Let’s try concatenating a few strings together now!

21 Concatenating strings
Concantenating strings – means joining them together to create a while new string. Ex. “cup”+”cake”= cupcake *Note: exact values infused together with NO SPACE so if you want a space put it inside the quotes Ex. “You can _”+”see me” = You can see me

22 You try it Print the concatenated strings “Spam ”, “and ” and “eggs” *Make sure you include spaces after Spam and eggs

23 Line Continuation Character
Line continuation character - \ (backslash) Put it anywhere you would normally use a space (but not inside a string) to continue statement on next line (so it won’t start a new line). The computer doesn’t care how long a programming line is, but people do. If a line of your code feels too long, or would be more clear as several lines, use line-continuation character to split it up!

24 Repeating Strings You try it: print(“Pie”*10) What happens?

25 Repeating Strings This line creates a new string, “PiePiePiePiePiePiePiePiePiePie” and prints it out. That’s the string “Pie” repeated 10 times. To repeat strings, use * Ex: print (“Hi”*5)=“HiHiHiHiHi”

26 Code Practice Grab a book! Read and code pg 21-26


Download ppt "More about comments Review Single Line Comments The # sign is for comments. A comment is a line of text that Python won’t try to run as code. Its just."

Similar presentations


Ads by Google