Day 2 Flashcards

1
Q

Pretend you are a computer. What will you do with the following?

print(“Bonoboboude”[0])

A

B

The first of anything is always at position 0.

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

What is subscripting?

A

Pulling out a particular element from a string at the [x] position

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

What is an integer?

A

A whole number (without decimal places). Maximum size is: -2147483648 through 2147483647
(214 million)

e.g.
1234567

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

What is a float?

A

A Floating Point Number
A number with decimal places
e.g.
3.14259

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

What is a Boolean used for?

A

To test if something is True or False

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

How can you check the type of data?

A

print(type(example))

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

What is type conversion/type casting?

A

Change a piece of data from one type to another

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

In Python, what is used to

a) multiply
b) divide
c) get the exponent?

A

a) *
b) /
c) **

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

What is the order of priority of calculations?

A

PEMDAS

Parentheses ( )
Exponents **
Multiplication * Division /
Addition + Subtraction -

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

What is the output of this code:

print(len(12352))

A

TypeError: object of type ‘int’ has no len().

This happens because len() is a function that only takes a string input.

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

What is meant by declaring and initializing a variable?

A

The short answer is:
Declaring a variable means stating its name and data type. e.g: String NameOfString

Initializing means assigning a value to a variable. e.g:
NameOfString = “SomeNameValue”

Context:

Generally in programming declaring a variable means defining its type, and optionally, setting an initial value (initializing the variable). Variables do not have to be initialized (assigned a value) when they are declared, but it is often useful.

Python is a bit different.

Unlike in the C programming language, in Python a variable is not bound to a certain object. Rather, variables in Python work as pointers to objects. This means they’re not restricted to a specific type and can be assigned dynamically. For instance, you can assign a value of one data type to a variable, and to another data type directly afterward:

x = 5
x = "five"

This would throw an error in a statically typed language like C or Java. In those languages, a variable’s type has to be specified before it may be assigned to a value. This process is called variable declaration. But in Python, we don’t declare variables, we simply assign them. In short, we can think of a variable in Python as a name for an object.

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

What is meant by a statically typed language vs a dynamically typed language? which is Python?

A

Short Answer:

Static Type:
A language is statically typed if the type of a variable is known at compile time. For some languages this means that you as the programmer must specify what type each variable is;

Dynamic Type:
A language is dynamically typed if the type is associated with run-time values, and not named variables/fields/etc. This means that you as a programmer can write a little quicker because you do not have to specify types every time (unless using a statically-typed language with type inference).

Context:

Statically typed languages

A language is statically typed if the type of a variable is known at compile time. For some languages this means that you as the programmer must specify what type each variable is; other languages (e.g.: Java, C, C++) offer some form of type inference, the capability of the type system to deduce the type of a variable (e.g.: OCaml, Haskell, Scala, Kotlin).

The main advantage here is that all kinds of checking can be done by the compiler, and therefore a lot of trivial bugs are caught at a very early stage.

Examples: C, C++, Java, Rust, Go, Scala

Dynamically typed languages

A language is dynamically typed if the type is associated with run-time values, and not named variables/fields/etc. This means that you as a programmer can write a little quicker because you do not have to specify types every time (unless using a statically-typed language with type inference).

Examples: Perl, Ruby, Python, PHP, JavaScript, Erlang

Most scripting languages have this feature as there is no compiler to do static type-checking anyway, but you may find yourself searching for a bug that is due to the interpreter misinterpreting the type of a variable. Luckily, scripts tend to be small so bugs have not so many places to hide.

Most dynamically typed languages do allow you to provide type information, but do not require it. One language that is currently being developed, Rascal, takes a hybrid approach allowing dynamic typing within functions but enforcing static typing for the function signature.

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

How can you write a long number with decimals as an int in Python?

A

instead of 123,456,789 you can write 123_456_789. The python interpreter will take out the underscores and read it as 123456789.

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

What is a Function?

A

Its a piece of code that performs a specific operation. It can take an input like:

len(“test”) # “test” is the input and the len function calculates the length

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

How can you convert a int to a string?

A

str(123)

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

What datatype is the answer of a division? e.g. 6/3?

A

Float. 6/3 = 2.0.

Divisions always result in Floats.

If you want an int you need to cast it back to int with int(6/2).

17
Q

How do you round up values in python?

A

round(number, digits)

Digits - Optional. The number of decimals to use when rounding the number. Default is 0

18
Q

What is the floor division?

A

This is where you have // instead of / for a division. It just gives the int instead of decimal.

4//2 = 2 not 2.0

19
Q

What is the shorthand for incrementing or decreasing the variable’s value.

A

Instead of:
score = 0
score = score +1

You can write:

score += 1
or
score -= 1

20
Q

What is an F String?

A

Its a convenient way of combining strings and numbers or other variables.

Instead of converting everything into strings separately and concatenating all the values you can add the character “f” infront of a string. this allows you to add variables in a string.

e.g. print(f”your score is {score})”