chapter_two Flashcards

1
Q

What does a name error in traceback indicate?

A

1) forgot to set the variable’s value

2) made a spelling mistake when entering the variable’s name.

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

How do you name variables?

A

1) can contain only letters, numbers and underscores

2) can’t start with a number
3) spaces are not allowed, use underscores instead

4) avoid Python keywords and function names
5) avoid “l” and “O” because of their similarities with “1” and “0”

6) short and desciptive naming ( n_l < name_of_persons_name < name_lenght)

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

What are variables and their properties?

A

1) Lables that you can assign to values
2) references a certain value.

3) The value can be changed any time

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

What does the “Python interpreter” do?

A

1) It interpretes the code you wrote

2) translates into a simpler language for the computer to understand.

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

> “Hello World!”

What kind of data type is this?

What defines that data type?

A

1) A “string”

2) defined as a series of characters
3) enclosed by a pair of quotation marks.
4) Either single ‘ ‘ or double “ “ quotes can be used.

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

> string = “hello world!”

How do you capitalize every first letter of this string?

Give a short description about the functionality of your code.

A

1) > string.title( )

2) The “.title( )” method appears after a variable.
3) It changes each word to title case, where each word begins with a capital letter.

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

What is a method?

How is it structured?

What is the functionality of said structure?

A

1) An action that Python can perform on a piece of data.

2) a dot “ . “ connects the method to a variable
3) a set of parentheses ends the method

4) the method acts on the variable it is connected to
5) the parentheses can be used to insert additional information a method might need

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

> message = “Hello World!”

Which code would you insert above to get the same output as shown below?

> > > hello world!
HELLO WORLD!

A

> message = “Hello World!”

1)
> print( message.lower( ) )

2)
> print( message.upper( ) )

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

> name = “Thomas”

How would you insert the variable above into a string?

What is the name for the code you used? short and long version.

What is the reason for its name?

A

> name = “Thomas”

1) f” “
2) {name}

> string = f”Hello, {name}!”

3) “f-string”.
4) The “f” stands for “format”
5) Python formats the string by replacing the name of any variable in braces with its value.

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

What is whitespace ?

Insert two variantes of whitespace in the string below.

> string = “Hello World!”

A

1) any nonprinting character
2) ex. spaces, tabs and end-of-line symbols.

3)
> string = “Hello\n\tWorld!”

4) \t - tab; \n - new line

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

> string = “ whitespace “

how would you remove the whitespace

  • to the left end
  • to the right end
  • on both ends
A

1)
> string.rstrip( )

2)
> string.lstrip( )

3)
> string.strip( )

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

What is a syntax error?

A

1) occurs when Python doesn’t recoginze a section of your program
2) as valid Python code.

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

How would you insert the following two sentences in a string?

  • One of Python’s strenghts is its diverse community. -
  • He said, “fishsticks”, and we all started laughing. -

Why do it that way?

A

1)
> “One of Python’s strenghts is its diverse community.”

2)
> ‘He said, “fishsticks”, and we all started laughing.’

3) You can’t use the quote your are using to enclose the string in the text or it will cause errors.

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

What are “integers” and what operators can be used in combindation with them?
How do you write exponents in Python?

A

1) Integers are a data type in Python consisting of whole numbers.
2) You can add (+), subtract (-), multipy (*) and divide (/) integers in Pyhon.
3) Python uses two multiplication symbols to represent exponents (**)

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

What are “floats”?

What are their irregularities?

A

1) Python calls any number with a decimal point a float

2) Floats can sometimes get an arbitrary number of decimal places in your answers.
3) Python tries to find a way to represent the result as precisely as possible
4) sometimes difficult given how computers have to represent numbers internally.

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

Which datatype will you get if you output the equations below?

> 2 + 3.0

> 9 / 3

In which cases does this happen?

A

1) float or float point number

2) If you mix an integer and a float in any other operation
3) When you divide any two numbers (even if they are integers that result in a whole number)

17
Q

Make the number below more readable for programmers.

> number = 10000000000

What happens to the output?

A

1)
> number = 10_000_000_000

2) Python ignores the underscores when storing these kinds of values.

18
Q

Use only one line to assign the variables below.

> one = 1
two = 2
three = 3

A

1)
> one, two, three = 1, 2, 3

+) Use this alot ! makes your program shorter and easier to read !

19
Q

What are constants?
Assign the value below to a constant.

> 3.14

A

1) A constant is variable whose value stays the same thoughout the life of a program.

2)
> PI = 3.14

+) Python doesn’t have built-in constant types
+) Python programmers use all capital letters to indicate a variable should be treated as a constant and never be changed.

20
Q

Write a comment to the code below.

> number = 3
power_2 = number * number
output( power_2 )

How do I write comments and what kind of comments should I write?

A
1)
> # the code below gives the 2nd power of a given number
> number = 3
> power_2 = number * number
> output( power_2 )
21
Q

What kind of comments should you write?

When should you write a comment?

A

1) describe your overall approach to the problem you’re solving
2) Explain what your code is supposed to do
3) and how you are making it work.
4) describtive, meaningfull, clear, concise

5) If you had to consider several approaches before coming up with a reasonable way to make something work.

22
Q

The Zen of Python (Part one)

A
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
23
Q

The Zen of Python (Part two)

A

Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one– and preferably only one –obvious way to do it.
Although that way may not be obvious at first unless you’re Dutch.
Now is better than never.
Although never is often better than right now.
If the implementation is hard to explain, it’s a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea – let’s do more of those!