Python 2 Flashcards

1
Q

What are the types of errors in Python?

A

-Syntax errors
-Semantics error
-Runtime errors

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

What is a syntax error and name cases where it might happen?

A

-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

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

What is semantic error?

A

Python will run successfully and the computer wont generate any error message however it will not do the thing you wanted it to do

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

What are runtime errors?

A

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

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

What are bugs?

A

Programming errors

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

What is debugging?

A

Process of tracking programming errors down

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

What is a Python interpret?

A

To execute a program in a hugh level language by translating it one line at the time

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

What is Python compile?

A

To translate a program written in a high-level
language into a low-level language all at once, in preparationfor later execution

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

What is interactive mode?

A

A way of using the Python interpreter by
typing commands and expressions at the prompt

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

What is script mode?

A

A way of using the Python interpreter to read and
execute statements in a script file.

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

What does script mode do?

A

-Write, edit, save, and run
-Word processor for your code
-Save your file using the “.py” extension

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

What does interactive mode in Python give you?

A

Gives you immediate feedback and it is not designed to create programs to save and run later

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

What is a string?

A

A sequence of characters surrounded by “ “ or ‘ ‘

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

What is the purpose of Triple Quotes?

A

Enclose strings containing both single and double quotes such that no escaping is needed.
* Enclose multi-line strings.

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

Why did this produce a syntax error?
»> “hello’ and what syntax error does it produce?

A

-It mixes the quotes “ and ‘
-File “<stdin>", line 1
"hello'
^SyntaxError: EOL while scanning string literal</stdin>

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

Why does this produce a syntax error?
»> ‘It’s a bad example’ and how to fix it?

A

-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

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

What is an escape sequence?

A

A special sequence of characters that provide
more functionality to the displayed text

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

What does \ do?

A

It is a backslash and it prints one backslash.

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

What does ' do?

A

Single quote, it prints one single quote
ex:&raquo_space;> ‘It/’s getting cold’
output: “It’s getting cold.”

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

What does " do?

A

Double quote; it prints a double quote.
ex:&raquo_space;> “It"s getting cold”
output: “It”s getting cold”

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

What does \a do?

A

Bell, sounds the system bell

19
Q

What does \b do?

A

Backspace, moves the cursor back one space.

20
Q

What does \n do?

A

New line, moves the cursor to the beginning of next line.

21
Q

What does \t do?

A

Horizontal tab, moves cursor forward one tab stop

22
Q

Why does this produce a syntax error?
»> print(‘She said, “Thank you! It’s mine.”’) and how to fix it?

A

-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.” ‘ ‘ ‘

23
Q

How to print Mult-line strings

A

-Adding a triple double quote

24
Q

How to get this output:
Hello
World
!

A

> > > print ( “ “ “Hello
World
!””””

25
Q

What is string concatenation? and what is its operator?

A

means to join two or more strings into a
single string and the + sign is its operator

26
Q

How do you get the output “Hello, how are you” using string concatenation.

A

print (“Hello, “ + “how “ + “are “ + “you “)

27
Q

How do you repeat strings? and show how to get the output “Are We There Yet? Are We There Yet? Are We There Yet?”

A

You use the multiplication sign (*)
print (“Are We There Yet? “ * 3)

28
Q

How do you count the number of characters in a string? and show an example.

A

-use the len() function
»>a = “Pen”
»> len (a)
3
»> b = “Hello World!”
»> len (b)
12

29
Q

What does the function str () do and use it?

A

str () is used to create a new string from a non-string type
»> a = 123
»> str (a)
‘123’

30
Q

What does title () do? and use it

A

It makes the first letter in every word capitalized
a = “This is a testing”
a.title()
‘This Is A Testing’

31
Q

What does upper () do? and use it

A

Makes every letter capitalzied
b = “This is a testing.”
b.upper()
“THIS IS A TESTING”

31
Q

What does lower () do? and use it

A

Makes every letter lower cased
c = “THIS IS A TESTING”
c.lower()
‘this is a testing’

31
Q

What does isdigit () do? and use it?

A

Gives a true or false answer if the string contains only digits
a = “Only 2 students”
a.isdigit ()
False

b = 3
b.isdigit ()
True

31
Q

What does islower () do? and use it

A

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

32
Q

What is string indexing? and use it

A

Strings can be indexed with the first character having index 0
»> word = “Python”
»> word [0]
‘p’

> > > word [-1]
‘n’
word [-4]
‘t’

32
Q

> > > word = “Python”
word[-0]
What would the answer be?

A

‘p’ in lower case
-0 is the same as 0 so negative indices start from -1

33
Q

How to use curly brackets as placeholders?

A

> > > 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

33
Q

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

A

> > > word = “Python”
word [:3]
‘Pyt’
word [1:5]
‘ytho’
word[4:]
‘on’
word [-2]
‘on’
word [:]
‘Python

33
Q

How to insert a variable into a string and show an example

A

-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

33
Q

-!=
-<
->
->=
<=

A

-Equal
-Not equal
-Less than
-Greater than
-Greater than or equal to
-Less than or equal to

33
Q

Use string concatenation to get the output: Jack is 30 years old.
Name and age are variables

A

age = 30
name = Jack
PRINT (name + ‘ is ‘ + str(age) + ‘ years old. ‘ )
Jack is 30 years old

34
Q

Use f strings to have the output: Jack is 30 years old. Age and name must be verifiable.

A

age = 30
name = “Jack”
print (f ‘ {name} is {age} years old. ‘ )

35
Q

Define these logical operators and give examples
-and
-or
-not

A

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

36
Q

What are the order of evaluations of Logical operators

A

Parentheses are evaluted first then followed by the not operators then the and operators then the or operators

37
Q

How to get user input? show an example and what happens when function is called

A

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

38
Q

How to display a prompt?

A

> > > name = input (‘What is your name?’)
What is your name? Arthur Clarke
print (name)
Arthur Clarke

39
Q

If you expect the user to type and integer what to do?

A

Convert the return value to int ()
ex:
»> prompt= “How old are you?\n”
»> age = input (age)
17
»> int (age)
17