Chapter 2 notes 2 Flashcards
Print function is what
A string-a sequence of characters enclosed in a single quotes (‘)
Text here is not preceded by
Out[1]
It also does not display quotes in strings
You can also enclose them with (“) but Python programmers generally prefer single quotes
Printing a comma-separated list of items
The print function can receive a comma-separated list of arguments as in:
In[3]: print(‘Welcome’, ‘to’, ‘Python!’)
Out[3] Welcome to Python!
What is an escape character?
When a backlash () appears in a string. The backlash and character immediately follow it and form an escape sequence. Ex: \n represents the newline character escape sequence which tells print to move the output cursor to the next line
In[4]: print(‘Welcome\nto\n\nPython!]
Out[4]: Welcome
To
Python!
What is a continuation character and what symbol does it use?
You use a continuation character to split a long string (or a long statement) over several lines by using the \ continuation character, as the last character or line to ignore the line break:
In[5]: print(‘this is a longer string, so we \
…: split it over two lines’)
Out[5]: this is a long string, so we split it over two lines
Calculations can be performed in print statements, provide example
In[6]: print(‘Sum is’, 7 + 3)
Out[6]: Sum is 10
The ________ function instructs the computer to display information on the screen
Values of the ________ data type contain a sequence of characters
String (type str)
Write a Python expression that displays the type of ‘word’
In[1]: Type(‘word’)
Out[1]: str
What does the following print statement display?
In[2]: print (‘int(5.2)’, ‘truncates 5.2 to’, int (5.2))
In[2]: print (‘int(5.2)’, ‘truncates 5.2 to’, int (5.2))
Out[2]: int(5.2) truncates 5.2 to 5
Triple-quoted strings
Begin and end with three double quotes (“ “ “) or three single quotes (‘ ‘ ‘) The style guide for Python code recommends three double quotes
What are the triple-quoted strings used to create?
Multiple strings
Strings containing single or double quotes
Docstrings, which are the recommended way to document the purposes of certain program components
When including quotes in strings, use double-quote characters and not single quotes, ex
In[1]: print(‘Display “hi” in quotes’)
Out[1]: Display “hi” in quotes
Not single quotes, view picture
Unless you use the \’ escape sequence:
In[2]: print(‘Display \’hi\’ in quotes’)
Out[2]: Display ‘hi’ in quotes
What is a syntax error?
A violation of Python’s language rules. In IPython, it will display information about the line of code that caused the syntax error and points to the error with a ^ symbol.
It also displays the message
SyntaxError: invalid syntax
How to avoid using \’ and \” inside strings?
Enclose such strings in triple quotes. Ex:
In[4]: print (“””Display ‘hi’ and ‘bye’ in quotes”””)
Out[4]: Display ‘hi’ and ‘bye’ in quotes
Explain the steps of the snippet that executed an input function for the a given name
In[1]: name = input (“What is your name?”)
Out[1]: What’s your name? ______
In[2]: name
Out[2]: Rebeca
In[3]: print(name)
Out[3]: Rebeca
Here the input displays its string argument-called a prompt-to tell the user what to type and wait for the user to respond. We typed Rebeca (without quotes) and pressed Enter
The function input then returns (gives back) those characters as a string that the program can use. We assigned that storing to a variable name