Session 1 - Part I Flashcards

1
Q

On any computer everything is stored in a

A

file system

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

What is a file system? - (2)

A

A file system is the organizational structure employed by operating systems to manage and store data on storage devices.

It establishes a hierarchical arrangement for organizing files and directories, facilitating efficient data access and retrieval.

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

What analogy is often used to describe the structure of a file system, and what is its ‘trunk’ equivalent?

A

A file system is often likened to a tree structure, where the ‘trunk’ represents the ‘base’ of file system.

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

The ‘base’ of the file systems is commonly denoted by computer as:

A

single slash ‘/’ character or known as ‘root directory’

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

What is a root directory? - (2)

A

The root directory is the highest-level directory in a file system hierarchy, serving as the starting point of directory tree from which all other directories and files branch.

It is denoted by a single forward slash (/)

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

Diagram of file system at YNiC

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

What is the path of the file “Thesis.doc” provided in the diagram?

A

The path of the file “Thesis.doc” is /home/alex/.

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

Why is it important to avoid certain ‘funny’ characters like spaces, question marks etc… in file system names?

A

Spaces and exclamation marks have special meaning to coders so when using them to name files in file system it becomes very complicate to manage and navigate to files in file system using code as using special characters on the file

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

Naming file in file system using ‘funny characters’ example

A

My Cat Pictures!.doc

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

What advice is given regarding file naming conventions in the notes, and why is it important to follow such conventions? - (2)

A

The notes suggest avoiding ‘funny’ characters like spaces and question marks in file names, advising the use of underscores instead.

This practice is crucial to prevent complications in file management and ensure seamless navigation within the file system.

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

Example of following conventions when naming files in file system

So instead of: My Cat Pictures!.doc

use…

A

My_Cat_Pictures.doc

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

What special character precedes commands executed within the Colab code cells, and what does it signify? - (2)

A

Commands executed within Colab code cells are prefixed with the ‘%’ sign, indicating that they are special commands (not Python).

These commands allow users to perform various non-Python operations within the notebook environment

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

What command is used to change the current directory within a Colab code cell?

A

The ‘%cd’ command is employed to change the current directory within a Colab code cell

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

what argument does %cwd require?

A

It necessitates specifying the desired directory it wants to change the current directory to as its argument.

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

What does %cwd / mean?

A

Changes the current directory to the ‘root’ directory of the file system

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

What does the ‘%pwd’ command do within a Colab code cell?

A

The ‘%pwd’ command in a Colab code cell prints the present working directory, revealing the current location within the file system.

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

What does %pwd stand for?

A

‘Print Working Directory’ or ‘Where am I?’

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

What does %cd stand for?

A

Change Directory

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

What command is used to list the contents of the current directory within a Colab code cell?

A

The ‘%ls’ command

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

What does %ls represent in Colab code cell?

A

‘List’

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

What does %ls -lt represent in Colab code cell?

A

%ls -lt’ stands for ‘List - Long, sorted by time’

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

How can one list the contents of the current directory in a ‘long’ format sorted by ‘time’ within a Colab code cell?

A

To list the items in the current directory in a ‘long’ format sorted by ‘time’ within a Colab code cell, the ‘%ls -lt’ command is used.

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

The directories exist in a

A

‘tree’

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

What will this code give you?

%cd /
%pwd
%ls
%ls - lt
- (3)

A

%pwd = given /

%ls - lists all contents of current working directory (from bin@ to cuda-keyring)

%ls-lt (output starting from total 100) - lists all contents of current working directory by working out file size and sorts them by date

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

What does the ‘%history’ command do in Colab notebooks?

A

The ‘%history’ command in Colab notebooks displays a history of codes and commands written

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

What does the ‘%magic’ command do in Colab notebooks?

A

The ‘%magic’ command in Colab notebooks provides information and documentation about available commands and their functionalities.

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

List all the special commands used in Colab notebooks - (7)

A

‘%cd’,
‘%pwd’, ‘
%ls’,
‘%ls -lt’,
‘%history’,
‘%magic’.
&mkdir

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

What does the ‘%mkdir’ command do

A

It creates a new directory within the current working directory.

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

Quick Exercise
Practice typing commands in the code window below and run them.

Check what your present working directory is and make a note of it (this might be your home directory on the system on which you are working)

Change to another directory to home

Check where your present working directory

Produce a new directory called test

List contents in test

Change current working directory to test

A

%pwd
Output: /content/sample_data

%cd /home (root then place we want to go)

Output: /home

%pwd - check where we are
Output: ‘/home’

%mkdir test
No output

%ls
Output: test/

%cd test
Output: /home/test

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

What character represents comments in Python?

A

The hashtag symbol (#) represents comments in Python.

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

Why are comments important in programming?

A

Comments help programmers add notes, explanations, or documentation to their code/program in English, making it easier for person reading the script understanding what was happening

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

Comments are parts of your code that the

A

computer ignores

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

What should you consider when writing comments in your code? - (2)

A

When writing comments, it’s essential to be professional, helpful, and concise (don’t write too much).

Provide explanations for why certain actions are taken in your code, in addition to describing what the code is doing

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

Example of comment

A

This is a comment

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

Example of writing comment of explaiing why you are doing something as well as describing what the code is doing?

A

print(‘Hello Again! This is running in Colab!’) # Print out a nice message on the otput.

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

What is the output of this code?

print(‘Hello Again! This is running in Colab!’) # Print out a nice message on the otput.

A

Hello Again! This is running in Colab!

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

What does the following line of code do?

print(‘Hello Again! This is running in Colab!’) # Print out a nice message on the otput.

A

The line of code prints the message ‘Hello Again! This is running in Colab!’ to the output console.

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

What is the purpose of the comment in the following line of code?

print(‘Hello Again! This is running in Colab!’) # Print out a nice message on the otput.

A

The comment provides a note or explanation about the code’s functionality without being executed by the interpreter

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

What does the following code do?

print(“Hello World!”)

A

The code prints the string “Hello World!” to the output console.

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

What is the purpose of the print() function in Python?

A

The print() function in Python is used to output text or variables to the output console/terminal

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

How do you use the print() function to display a message? - (2)

A

To display a message using the print() function, you provide the message inside parentheses as an argument. For example:

print(“Hello, World!”)

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

What does the print() function do when given multiple arguments? - (4)

A

When provided with multiple arguments, the print() function outputs each argument separated by a space by default.

Example:
print(“Hello”, “World!”)

This would print:

Hello World!

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

How can strings be created in Python? - (3)

A

Strings in Python can be created using either double quotes (“) or single quotes (‘), providing flexibility in string declaration. For example:

print(‘Hello World!’)
OR
print(“Hello World!”)

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

Why are there two variants for creating strings in Python? - (3)

A

Two variants for creating strings in Python, using single quotes (‘’) and double quotes (“), exist to allow the inclusion of single quotes quotes within strings without escaping.

This enhances code readability and simplicity

using double quotes (“) - you can then use the single quote in the string.

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

Example of using single quotes in a string:

A

print(“Hello ‘World’”)

Output:
Hello ‘World’

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

What is a string in Python?

A

In Python, a string is a sequence of characters enclosed within either single quotes (‘’) or double quotes (“”). It can consist of letters, numbers, symbols, or spaces.

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

Print() function can take

A

a string as its argument and prints the content of the string to the standard output (usually the terminal or console)

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

What is the purpose of the print function in the given Python code?

print(“Hello World!”)

A

The purpose of the print function in the code is to output the string “Hello World!” to the screen

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

What happens when you execute the following Python code?

print(“jdkfjdlkfjdslfkjdslkf”) - (2)

A

Executing the code displays the string “jdkfjdlkfjdslfkjdslkf” on the screen.

The print function outputs the provided string of jdkfjdlkfjdslfkjdslkf to the output terminal/console

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

What observation is made regarding the usage of quotation marks in the provided Python code?

A

In the code snippet, both single quotes (‘ ‘) and double quotes (“ “) are used to create strings.

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

Identify the issue in the following Python code:

print(“Hello World!)

A

The issue in the code is the absence of a closing double quote, resulting in an “unterminated string literal” error.

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

What error message is encountered when executing the given Python code?

print(“Hello World!)

A

The error message encountered is “unterminated string literal”, indicating that the provided string is not properly terminated with a closing double quote.

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

How does the absence of a closing double quote affect the execution of the Python code?

print(“Hello World!)

A

The absence of a closing double quote results in an “unterminated string literal” error, halting the execution of the Python code due to a syntax error.

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

Exercise
In the code box below, print the following strings:

It’s the best!

“Hi!” they said - (2)

A

print(“It’s the best!”)

print(‘“Hi!” they said’)

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

What is the result of executing the following Python code?

print(“Hello” + “World”)

A

The result of executing the code is the concatenation of the two strings, resulting in the output “HelloWorld”.

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

Explain the purpose of the + operator in the given Python code - (2)

print(“Hello” + “World”).

A

The + operator is used for string concatenation in the provided Python code.

It combines the strings “Hello” and “World” into a single string, “HelloWorld”.

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

What action does the + operator perform when used with strings in Python?

e.g., print(“Hello” + “World”)

A

When used with strings in Python, the + operator concatenates or joins the strings together, producing a single string that contains the contents of both input strings.

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

What is string concatenation in Python?

A

String concatenation involves joining two or more strings to form a single string, typically achieved using the + operator in Python.

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

Why does Python not separate “Hello” and “World” in the code print(“Hello” + “World”)? - (2)

A

Python does not separate “Hello” and “World” because the + operator performs string concatenation, joining the two strings together into a single string.

Python does not know it is writing a sentence and does not know there should be a space between ‘Hello’ and ‘World’

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

What happens if we don’t include a space between the strings in the code print(“Hello” + “World”)?

A

If we don’t include a space between the strings, Python will concatenate them without adding a space, resulting in “HelloWorld”.

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

How can we include a space between the strings “Hello” and “World” in the code print(“Hello” + “World”)? - (5)

A

To include a space between the strings, we can add a space either in first string (adding space after the first word) , the second string (adding space before the second word), or as a third string with a space in between.

For example:

  • print(“Hello “ + “World”)
  • print(“Hello” + “ World”)
  • print(“Hello” + “ “ + “World”)

these all give the same output

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

What does the len() function do when applied to a string?

A

The len() function returns the number of characters in a string.

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

What is the output of this code? - (3)

  • print(“Hello “ + “World”)
  • print(“Hello” + “ World”)
  • print(“Hello” + “ “ + “World”)
A

Hello World
Hello World
Hello World

64
Q

What will the code print(len(“Hello World!”)) output?

A

The code will output the length of the string “Hello World!”, which is 12 (because of space and examination point)

65
Q

What arguments can you give to the len() function?

A

The len() function takes a single argument, which must be a sequence (such as a string, list, tuple, etc.), and returns the number of elements in that sequence.

66
Q

Can you give len() a variable as an argument?

A

Yes, you can pass a variable containing a sequence (such as a string, list, tuple, etc.) as an argument to the len() function.

67
Q

Functions take arguments (sometimes known as …. ) and gives you back (returns) something

A

parameters

68
Q

len() takes a single arguement and works out its length

It returns what length it is which we can either

A

print, put in a variable or otherwise use as appropriate.

69
Q

What does the following code snippet do: print(len(“Hello World!”))? - (2)

A

The len() function calculates the number of characters in the string “Hello World!”

This value is then passed to print() function which prints the result to the output console/terminal

70
Q

Explain the evaluation process of a nested function call like print(len(“Hello World”)) - (3)

A

Python evaluates the inner-most function call first, in this case, len(“Hello World”).

It calculates the length of the string “Hello World” and returns it.

Then, it evaluates the outer function call print(), which takes the returned value from the inner call and prints it to the screen.

71
Q

What is the output of the following code snippet?
myName = “Emma”
print(myName)
print(len(myName)) - (3)

A

The output is:
Emma
4

72
Q

What is a variable in programming?

A

A variable is a name for a value or an object in programming

73
Q

What do variables act like?

A

acts like a ‘box’ where data can be stored and manipulated

74
Q

How are variables assigned in Python?

A

Variables in Python are assigned using a single equals sign “=”.

75
Q

What is the output of the following code snippet?
msg = “Hello World”
print(msg) - (2)

A

The output is:
Hello World

76
Q

What does the following line of code do?
msg = “Hello World”
print(msg) - (2)

A

It assigns the string “Hello World” to the variable msg.

It prints the value stored in the variable msg.

77
Q

How is a variable defined in Python?

A

A variable is defined by using the assignment operator “=”, followed by the variable name and the value assigned to it.

78
Q

What are the rules for variable naming in Python? - (2)

A

Variable names can use letters, numbers, and underscores.

They are case sensitive (e.g., meaning that msg, Msg, and MSG are all considered different variables. ) and cannot start with a number.

79
Q

Can a variable name have a number in it in Python? - (2)

A

Yes, a variable name can have a number in it, but it cannot start with a number.

For example, myvar2 is a valid variable name, but 2myvar is not.

80
Q

Why should you avoid naming variables after Python keywords and standard functions?

A

Naming variables after Python keywords and standard functions can lead to confusion and errors in your code.

81
Q

What are some common sense guidelines for variable naming in Python? - (2)

A

Avoid using variable names that resemble numbers, such as ‘o’ or ‘O’, to prevent confusion with digits.

Additionally, be cautious when using ‘i’ and ‘l’ as they can be mistaken for ‘1’ in certain fonts.

82
Q

What are the two common ways of variable naming? - (2)

A
  1. camelCase
  2. use_underscores (sometimes called snake case)
83
Q

What is camelCase and how is it used in variable naming? - (2)

A

camelCase uses capital letters to indicate word boundaries in variable names/ use captial letters to show where variable names are:

Example: myVariable

84
Q

What is use_underscores (snake case) and how is it used in variable naming? - (2)

A

use_underscores, or snake case, involves using underscores between words in variable names.

Example: my_variable

85
Q

What happens when a variable is overwritten with a new value?

A

The original value of the variable is lost, and it holds the new assigned value.

86
Q

Can you retrieve the original value of a variable after it has been overwritten?

A

No, once a variable is overwritten with a new value, the original value is lost and cannot be retrieved.

87
Q

What is PEP8 and why is it important in Python programming? - (2)

A

PEP8 is a set of rules and guidelines for Python code style.

It helps ensure consistency and readability in Python code.

88
Q

What is the sequence of output when the following code is executed? - (3)

msg = “Hello World”
print(msg)

msg = “A new world”
print(msg)

A

The sequence of output is:

“Hello World”
“A new world”

89
Q

What happens when the following code is executed? - (4)

msg = “Hello World”
print(msg)

msg = “A new world”
print(msg)

A

The code first assigns the string “Hello World” to the variable msg and prints it.

Then, it assigns the string “A new world” to the same variable msg and prints it again. Therefore, the output will be:

Hello World
A new world

90
Q

What does the following code snippet do? - (4)

a = 2
b = 3

print(a * b)
print(a + b)

A

The code snippet assigns the integer value 2 to the variable a and the integer value 3 to the variable b.

Then, it prints the result of multiplying a and b, followed by printing the result of adding a and b. The output will be:

6
5

91
Q

What does the double equals sign (==) do in Python?

a) Assigns a value to a variable
b) Checks if two values are the same
c) Performs arithmetic addition
d) Evaluates an expression

A

b) Checks if two values are the same

92
Q

What does the following code snippet do? - (2)

a = 2
b = 3

print(a != b)

A

The code snippet prints True if the value of a is not equal to the value of b, and False otherwise.

Since a is 2 and b is 3, value of a is not equal to value of b thus returns False to output terminal/console

93
Q

What does the following code snippet do? - (5)

a = 2
b = 3

print(a * b)
print(a + b)

A

The code snippet assigns the integer value 2 to the variable a and the integer value 3 to the variable b.

Then, it prints the result of multiplying a and b, followed by printing the result of adding a and b.

The output will be:

6
5

94
Q

What does the del keyword do in Python?

a) Assigns a value to a variable
b) Deletes a variable from memory
c) Checks if two values are the same
d) Performs arithmetic addition

A

b) Deletes a variable from memory

95
Q

What does the following code snippet do? - (3)

b = 10
print(b)
del(b)
print(b)

A

The code snippet assigns the value 10 to the variable b and prints it.

Then, it deletes the variable b using the del keyword.

Attempting to print b after it has been deleted will result in a NameError because the variable no longer exists.

96
Q

As long as you are consistent with variable naming of camelCase and use_underscores it is

A

fine

97
Q

What does the == operator do in Python?

a) Assigns a value to a variable
b) Compares two values for equality
c) Deletes a variable from memory
d) Performs arithmetic addition

A

b) Compares two values for equality

98
Q

What is the purpose of the != operator in Python?

a) Assigns a value to a variable
b) Checks if two values are the same
c) Deletes a variable from memory
d) Compares two values for inequality

A

d) Compares two values for inequality

99
Q

Variables can be of

A

different types

100
Q

What are the important types of variables in Python?

a) int, str, bool, None
b) int, float, str, bool, None
c) int, float, str, bool
d) str, bool, None

A

b) int, float, str, bool, None

101
Q

What does the int type represent in Python? - (2)

a) Decimal numbers
b) Floating point numbers
c) Whole numbers
d) True or False values

A

c) Whole numbers

e.g., – can be positive or negative: … -2, -1, 0, 1, 2 …

102
Q

What are number variable types?

A

int and float

103
Q

What does the str type represent in Python? - (2)

a) Whole numbers
b) Decimal numbers
c) Floating point numbers
d) Zero or more characters

A

d) Zero or more characters

e.g., “Hello World”

104
Q

What does the bool type represent in Python?

a) Whole numbers
b) Decimal numbers
c) Floating point numbers
d) Boolean values - True or False values

A

d) Boolean values -True or False values

105
Q

What does the None type represent in Python?

a) Whole numbers
b) Floating point numbers
c) True or False values
d) Nothingness or absence of a value

A

d) Nothingness or absence of a value

106
Q

What does the type() function do in Python?

A

The type() function in Python is used to determine the data type of a variable or a value.

107
Q

What does the type() function return when given the argument 3?

A

The type() function returns <class ‘int’> when given the argument 3, indicating that 3 is an integer.

108
Q

What does the type() function return when given the argument 3.0?

A

The type() function returns <class ‘float’> when given the argument 3.0, indicating that 3.0 is a floating-point number.

109
Q

What does the type() function return when given the argument “3.0”?

A

The type() function returns <class ‘str’> when given the argument “3.0”, indicating that “3.0” is a string.

110
Q

What does the type() function return when given the argument None?

A

The type() function returns <class ‘NoneType’> when given the argument None, indicating that None represents the absence of a value.

111
Q

What does the type() function return when given the argument True or False?

A

The type() function returns <class ‘bool’> when given the arguments True or False, indicating that they are boolean values representing True or False, respectively.

112
Q

Instead of writing code like this with type() function, we can repeat same exercise but use variables like:

A
113
Q

Explain the behavior of type(float(3)) in Python.

A

The expression type(float(3)) converts the integer value 3 to a float and then determines its data type using the type() function.

114
Q

What is the purpose of type(int(3.25)) in Python?

A

The expression type(int(3.25)) converts the floating point number 3.25 to an integer and then checks its data type using the type() function.

115
Q

Describe the outcome of int(3.25) in Python.

A

The expression int(3.25) converts the floating point number 3.25 to an integer, resulting in the value 3.

116
Q

What is the result of int(3.999) in Python?

A

The expression int(3.999) converts the floating point number 3.999 to an integer, resulting in the value 3, as the conversion truncates the decimal part.

117
Q

What arithmetic operations can be performed on numbers in Python?

A

Arithmetic operations such as addition (+), subtraction (-), multiplication (*), division (/), integer division (//), exponentiation (**), and remainder (%) can be performed on numbers in Python

118
Q

Best to stick with one … data type in code

A

number data type like integers or floats

119
Q

Number data type of integer can be used for

A

no of people in group/particular exp condition or number of types someone blinked

120
Q

Number data type of float can be used for

A

reaction time e.g., 24.32 milliseconds (ms)

121
Q

What happens when executing print(3.0 + 3) in Python? - (3)

A

When running print(3.0 + 3), Python adds a floating point number (3.0) and an integer (3).

Python automatically converts the integer to a floating point number before performing the addition.

Forces the numbers to be same data type before performing addition.

122
Q

What is the output of print(3.999 + 3) in Python?

A

The output of print(3.999 + 3) is 6.999, as it performs addition between a floating point number (3.999) and an integer (3)

123
Q

Describe the outcome of print(3 - 2) in Python.

A

The outcome of print(3 - 2) is 1, which is the result of subtracting 2 from 3

124
Q

Note that python makes numbers

A

int by default unless they have a decimal point in them.

125
Q

An alternative of adding .0 at end of numbers, is - (2)

A

pass it through the float function.

This function takes a value as input and returns the corresponding floating point representation of that value.

126
Q

Describe the behavior of converting a floating point number to an integer using the int() function in Python. - (2)

A

When a floating point number is converted to an integer using the int() function in Python, the decimal part of the number is truncated, resulting in the loss of any fractional part.

This conversion does not round the number properly; instead, it simply removes the decimal part.

127
Q

What caution should be taken when converting floating point numbers to integers using the int() function in Python? - (2)

A

When converting floating point numbers to integers using the int() function in Python, it is important to note that the conversion truncates the decimal part, potentially leading to unexpected results.

This behavior can be a common source of bugs for new Python programmers, so caution should be exercised when using this method.

128
Q

Adding float to integer it will make it

A

float

129
Q

If you keep everything the same datatype when doing arithmetic operations in print statement e.g., print(3 + 2) it will remain the

A

same data type - integer in this case

130
Q

If you mix data types when doing arithmetic operations in print statement e.g., print(3.0 + 2) it will

A

typically become floating point values

131
Q

What is the difference between the two multiplication operations demonstrated in the code snippet? - (2)

print(3 * 2)

print(3.0 * 2)

A

The first multiplication operation (print(3 * 2)) involves multiplying two integers, resulting in an integer value.

In contrast, the second multiplication operation (print(3.0 * 2)) involves multiplying an integer and a floating point number, resulting in a floating point value.

132
Q

What is the output of print(3.0 * 2) in Python?

A

The output is 6.0.

133
Q

What is the output of print(3 * 2) in Python?

A

The output is 6.

134
Q

Explain the behavior of print(3.0 ** 3) in Python.

A

It computes the result of raising 3.0 to the power of 3, which equals 27.0.

135
Q

What does the expression print(9.0 ** 0.5) compute in Python?

A

It computes the square root of 9.0, which equals 3.0.

136
Q

How does the power operator () behave in Python? - (2)

A

The power operator () can be used to compute both “normal” power values, such as squaring, and roots.

For example, raising a number to 1/2 (0.5) computes the square root.

137
Q

How does division work in Python? - (2)

A

In Python, division is performed using the / operator.

It automatically converts integers to floating-point numbers ‘where necessary’.

138
Q

What is the result of print(3 / 2) in Python?

A

The expression print(3 / 2) evaluates to 1.5 because it performs floating-point division, resulting in a floating-point number.

139
Q

What does the // operator do in Python?

A

The // operator in Python performs integer division, discarding any fractional part and returning the integer result.

140
Q

What is the result of print(3//2) in Python?

A

The expression print(3//2) evaluates to 1 because it performs integer division, resulting in an integer quotient - has not rounded it (chopped off decimal part)

141
Q

What does the % (modulus) operator do in Python?

A

The % operator in Python returns the remainder of the division of the first number by the second number.

142
Q

What is the result of print(8 % 3) in Python?

A

The expression print(8 % 3) evaluates to 2 because when 8 is divided by 3, the remainder is 2.

143
Q

The ‘/’ allows integers to turn into

A

floating point numbers ‘where necessary’ (in other words if answer is not an integer)

144
Q

The ‘//’ does not allow

A

integers to turn to floating point numbers ‘where necessary’ (in other words if answer is not an integer)

145
Q

When using the integer division operator on floating point numbers, the division will be performed as if numbers were integer but result will be - (2)

A

floating point

e.g., print(3.0//2) in Python outputs the result of dividing 3.0 by 2 using integer division, resulting in 1.0.

146
Q

What happens when you try to add a string and an integer in Python

print(3 + “dlfkjdofd”),? - (2)

A

When you try to add a string and an integer in Python, you get a exception (error) TypeError because Python does not support adding different types together.

In the given example, print(3 + “dlfkjdofd”), Python raises an exception because you cannot concatenate a string and an integer directly.

147
Q

What type of error do you encounter when trying to add a string and an integer in Python?

A

When trying to add a string and an integer in Python, you encounter a TypeError.

148
Q

Question: What does the following code snippet do?

age_in_years = 21.5
days_in_year = 365
print(“My age in years is:”, age_in_years)
print(“My age in days in:”, age_in_years * days_in_year)

A

This code snippet sets up variables for age in years and days per year, then prints out the age in years followed by the age in days calculated by multiplying the age in years by the number of days in a year.

149
Q

What is it’s output? - (2)

age_in_years = 21.5
days_in_year = 365
print(“My age in years is:”, age_in_years)
print(“My age in days in:”, age_in_years * days_in_year)

A

My age in years is: 21.5
My age in days in: 7857.5

150
Q

What are the rules for precedence of mathematical operations in Python? - (3)

A

The rules for precedence of mathematical operations in Python follow roughly the same rules as those in mathematics.

Multiplication and division have higher precedence than addition and subtraction - BODMAS

e.g.., So, for instance: 1 + 2 * 3 will be evaluated as 1 + (2 * 3) giving a result of 7, because multiplication is higher precedence than addition.

151
Q

How can you ensure correct precedence in Python expressions or make code clearer?

A

You can use brackets to ensure correct precedence in Python expressions, similar to how you would in algebra.

152
Q

Give an example of using brackets to ensure correct precedence and making code clearer in Python.

A

res = (1 + 2) * 3
print(res)

153
Q

Can brackets be nested in Python expressions?

A

Yes, brackets can be nested in Python expressions for complex calculations.

154
Q

Provide an example of nested brackets in a Python expression.

A

res = 1 + (2 * (3 + 1))
print(res)

155
Q

res = (1 + 2) * 3
print(res)

Output and why? - (3)

A

9

In the first expression (1 + 2) * 3, the addition inside the parentheses is evaluated first, resulting in 3.

Then, the multiplication operation is performed, yielding 9.

156
Q

res = 1 + (2 * (3 + 1))
print(res)

output and why? - (3)

A

In the second expression 1 + (2 * (3 + 1)), the innermost parentheses (3 + 1) are evaluated first, resulting in 4.

Then, the multiplication operation 2 * 4 is evaluated, resulting in 8.

Finally, the outer addition 1 + 8 is performed, resulting in 9.