Chapter 2 notes 2 Flashcards

1
Q

Print function is what

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Printing a comma-separated list of items

A

The print function can receive a comma-separated list of arguments as in:
In[3]: print(‘Welcome’, ‘to’, ‘Python!’)
Out[3] Welcome to Python!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is an escape character?

A

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!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is a continuation character and what symbol does it use?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Calculations can be performed in print statements, provide example

A

In[6]: print(‘Sum is’, 7 + 3)
Out[6]: Sum is 10

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

The ________ function instructs the computer to display information on the screen

A

Print

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Values of the ________ data type contain a sequence of characters

A

String (type str)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Write a Python expression that displays the type of ‘word’

A

In[1]: Type(‘word’)
Out[1]: str

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What does the following print statement display?
In[2]: print (‘int(5.2)’, ‘truncates 5.2 to’, int (5.2))

A

In[2]: print (‘int(5.2)’, ‘truncates 5.2 to’, int (5.2))
Out[2]: int(5.2) truncates 5.2 to 5

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Triple-quoted strings

A

Begin and end with three double quotes (“ “ “) or three single quotes (‘ ‘ ‘) The style guide for Python code recommends three double quotes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What are the triple-quoted strings used to create?

A

Multiple strings
Strings containing single or double quotes
Docstrings, which are the recommended way to document the purposes of certain program components

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

When including quotes in strings, use double-quote characters and not single quotes, ex

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is a syntax error?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How to avoid using \’ and \” inside strings?

A

Enclose such strings in triple quotes. Ex:
In[4]: print (“””Display ‘hi’ and ‘bye’ in quotes”””)
Out[4]: Display ‘hi’ and ‘bye’ in quotes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Explain the steps of the snippet that executed an input function for the a given name

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

String concatenation

A

In[7]: value1 = input (‘Enter first number: ‘)
Out[7]: Enter first number: 7

In[8]: value2 = input (‘Enter second number: ‘)
Out[8]: Enter second number: 3

In[9]: value1 + value2
Out[9]: ‘73’

Rather than adding the integers, Python “adds” the string values ‘7’ and ‘3’, producing the string ‘73’
It creates a new string containing the left operand’s value followed by the right operand’s value

17
Q

Getting an integer from the user
If you need an integer, convert the string to an integer using the built-in int function
Ex

A

In[10]: value = input (‘Enter an integer: ‘)
Out[10]: Enter an integer __7__

In[11]: value = int(value)

In[12]: value
Out[12]: 7

18
Q

How to combine the int function into snippets

A

Do this instead, but if the other one was performed then change the name to another_value
In[13]: value = int (input(‘Enter another integer: ‘))
Out[13]: Enter another integer: 13

In[14]: value
Out[14]: 13

19
Q

Using the integer function is good for change values into ______

A

Integers
This is good when you want to add them up instead of getting ‘73’, you can get an actual answer of ‘10’ like in the previous numerical examples

20
Q

What happens when a string cannot be converted to an integer?

A

A ValueError message occurs

21
Q

T/F The function int also can convert a floating-point value to an integer

A

True
In[17]: int(10.5)
Out[17]: 10

22
Q

How to convert strings into floating-point numbers?

A

Uses the built in float function
In[18]: float (‘6.2) * 3.3
Out[18]: 20.46

23
Q

The built-in __________ function converts a floating-point value to an integer value or converts a string representation of an integer to an integer value

A

Int

24
Q

T/F Built-in function get_input requests and obtains input from the user

A

False. The built-in function’s name is input

25
Q

What is a Boolean expression?

A

A condition with the value True or False

26
Q

Give 2 examples of a true/false Boolean expression from the book

A

In[1]: 7 > 4
Out[1]: True

In[2]: 7 < 4
Out[2]: False

27
Q

What are Python keywords?

A

Words like True or False, language that Python reserves for these features. Using keywords as an identifier causes a SyntaxError
True and False are each capitalized

28
Q

What is the meaning for these conditions?
1. X > y
2. X < y
3. X >= y

A
  1. X is greater than y
  2. X is less than y
  3. X is greater than or equal to y
29
Q

What is the meaning for these conditions?
1. X <= y
2. X == y
3. X != y

A
  1. X is less than or equal to y
  2. X is equal to y
  3. X is not equal to yW
30
Q

What happens when there are spaces between its pair of symbols?
Ex:
In[3]: 7 > = 4

A

Syntax error occurs, this also occurs if you reverse the symbol in the operators by writing != as =! Instead

31
Q
A