Python 2 Flashcards
What are the types of errors in Python?
-Syntax errors
-Semantics error
-Runtime errors
What is a syntax error and name cases where it might happen?
-When the computer does not recognize the statement to be executed, a syntax error is generated.
-A misspelled code in programming language
-Python is case sensitive so capital letters can cause a syntax error, ex: Print and not print
What is semantic error?
Python will run successfully and the computer wont generate any error message however it will not do the thing you wanted it to do
What are runtime errors?
Type of error that do not appear after the program has started running.
-They are also called exceptions because they indicate something exceptional and bad has happened
-It is very rare
What are bugs?
Programming errors
What is debugging?
Process of tracking programming errors down
What is a Python interpret?
To execute a program in a hugh level language by translating it one line at the time
What is Python compile?
To translate a program written in a high-level
language into a low-level language all at once, in preparationfor later execution
What is interactive mode?
A way of using the Python interpreter by
typing commands and expressions at the prompt
What is script mode?
A way of using the Python interpreter to read and
execute statements in a script file.
What does script mode do?
-Write, edit, save, and run
-Word processor for your code
-Save your file using the “.py” extension
What does interactive mode in Python give you?
Gives you immediate feedback and it is not designed to create programs to save and run later
What is a string?
A sequence of characters surrounded by “ “ or ‘ ‘
What is the purpose of Triple Quotes?
Enclose strings containing both single and double quotes such that no escaping is needed.
* Enclose multi-line strings.
Why did this produce a syntax error?
»> “hello’ and what syntax error does it produce?
-It mixes the quotes “ and ‘
-File “<stdin>", line 1
"hello'
^SyntaxError: EOL while scanning string literal</stdin>
Why does this produce a syntax error?
»> ‘It’s a bad example’ and how to fix it?
-The quote ‘ is used three times
-You can add a backslash before the quote
ex: ‘ It\ ‘ s a good example ‘
-You can use a double quote as the enclosing quote
What is an escape sequence?
A special sequence of characters that provide
more functionality to the displayed text
What does \ do?
It is a backslash and it prints one backslash.
What does ' do?
Single quote, it prints one single quote
ex:»_space;> ‘It/’s getting cold’
output: “It’s getting cold.”
What does " do?
Double quote; it prints a double quote.
ex:»_space;> “It"s getting cold”
output: “It”s getting cold”
What does \a do?
Bell, sounds the system bell
What does \b do?
Backspace, moves the cursor back one space.
What does \n do?
New line, moves the cursor to the beginning of next line.
What does \t do?
Horizontal tab, moves cursor forward one tab stop
Why does this produce a syntax error?
»> print(‘She said, “Thank you! It’s mine.”’) and how to fix it?
-There were 3 single quotes
You can fix it by:
-Adding a backslash before the second single quote (It's)
=Adding a triple quote
ex: ‘ ‘ ‘ She said, “Thank you! It’s mine.” ‘ ‘ ‘
How to print Mult-line strings
-Adding a triple double quote
How to get this output:
Hello
World
!
> > > print ( “ “ “Hello
World
!””””
What is string concatenation? and what is its operator?
means to join two or more strings into a
single string and the + sign is its operator
How do you get the output “Hello, how are you” using string concatenation.
print (“Hello, “ + “how “ + “are “ + “you “)
How do you repeat strings? and show how to get the output “Are We There Yet? Are We There Yet? Are We There Yet?”
You use the multiplication sign (*)
print (“Are We There Yet? “ * 3)
How do you count the number of characters in a string? and show an example.
-use the len() function
»>a = “Pen”
»> len (a)
3
»> b = “Hello World!”
»> len (b)
12
What does the function str () do and use it?
str () is used to create a new string from a non-string type
»> a = 123
»> str (a)
‘123’
What does title () do? and use it
It makes the first letter in every word capitalized
a = “This is a testing”
a.title()
‘This Is A Testing’
What does upper () do? and use it
Makes every letter capitalzied
b = “This is a testing.”
b.upper()
“THIS IS A TESTING”
What does lower () do? and use it
Makes every letter lower cased
c = “THIS IS A TESTING”
c.lower()
‘this is a testing’
What does isdigit () do? and use it?
Gives a true or false answer if the string contains only digits
a = “Only 2 students”
a.isdigit ()
False
b = 3
b.isdigit ()
True
What does islower () do? and use it
Gives a true or false answer if the string contains all lower case letters
d = “hello world!”
d.islower ()
True
e =Hello world!”
e.islower ()
Fakse
What is string indexing? and use it
Strings can be indexed with the first character having index 0
»> word = “Python”
»> word [0]
‘p’
> > > word [-1]
‘n’
word [-4]
‘t’
> > > word = “Python”
word[-0]
What would the answer be?
‘p’ in lower case
-0 is the same as 0 so negative indices start from -1
How to use curly brackets as placeholders?
> > > print (“{} needs {} cups of coffee”.format (“Ben”,3))
Ben needs 3 cups of coffee
print (“{1} needs {0} cips of coffee”.format(“Ben”,3))
3 needs Ben cups of coffee
print (“{name} needs {numbers} cups of coffee”.format (name=”Ben”, number = 3))
Ben needs 3 cups of coffee
How to slice a string?
-Slice it to the first 3 characters
-From 1-5
-Characters 4 and after
-The last 2 letters
-The full word
> > > word = “Python”
word [:3]
‘Pyt’
word [1:5]
‘ytho’
word[4:]
‘on’
word [-2]
‘on’
word [:]
‘Python
How to insert a variable into a string and show an example
-Using a comma
»> name = “Ben’
»> age = 25
»> print (“My name is”, name)
My name is Ben
»> print (“My name is”, name, “and I am”, age, “years old”)
My name is Ben and I am 25 years old
-!=
-<
->
->=
<=
-Equal
-Not equal
-Less than
-Greater than
-Greater than or equal to
-Less than or equal to
Use string concatenation to get the output: Jack is 30 years old.
Name and age are variables
age = 30
name = Jack
PRINT (name + ‘ is ‘ + str(age) + ‘ years old. ‘ )
Jack is 30 years old
Use f strings to have the output: Jack is 30 years old. Age and name must be verifiable.
age = 30
name = “Jack”
print (f ‘ {name} is {age} years old. ‘ )
Define these logical operators and give examples
-and
-or
-not
and: Determines whether both operands are true
ex: True and True is True
False and True is False
or: Determines when one of the two operands is true
ex: True and True is True
True and False is True
False and False is False
not: negates the truth value of a single operand
ex: not True is False
not False is True
What are the order of evaluations of Logical operators
Parentheses are evaluted first then followed by the not operators then the and operators then the or operators
How to get user input? show an example and what happens when function is called
Use the fucntion input ()
ex:
»> text = input ()
What are you waiting for?
»> print (text)
What are you waiting for
The program stops and waits for user to type something
When the user presses enter the program resumes and input returns what the user typed as a string
How to display a prompt?
> > > name = input (‘What is your name?’)
What is your name? Arthur Clarke
print (name)
Arthur Clarke
If you expect the user to type and integer what to do?
Convert the return value to int ()
ex:
»> prompt= “How old are you?\n”
»> age = input (age)
17
»> int (age)
17