Introduction to Programming - 1 Flashcards

1
Q

What are the 2 types of cells in Colab - (2)

A
  1. Text = double click and add notes
  2. Code = tells google server to do something
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Debugging Exercise

Why does this code below not print “Hello World”

print(“Hello World)

Fix the code - (4)

A

It has a syntax error

The print statement is missing a closing quotation mark

It should be:
print(“Hello World”)

With closing quotation mark added, the code will print “Hello World” as expected

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

Debugging Exericse

What error message will this piece of code produce?

print(“Hello World) - (2)

A

Syntax error: unterminated string literal detected at line 1 meaning there is a string in code ‘hello world’ that has not been properly terminating

Forgot to add closing quotation mark (“) which will ensure string literal is properly terminated and syntax error is resolved

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

In this code,

print(“Hello World) you add another quote mark at the end like so:

print(“Hello World”) since

A

quotation marks have to match up in Python

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
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
6
Q

The ‘file system’ us often thought as a ‘tree’

The trunk of tree is the ‘base’ of file system which computers often call - (2)

A

’/’ a single slash or ‘root directory’

(bottom of the directory system)

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

The directory structure of a computer refers to how

A

files and folders are organized within a file system , organised typically in hierarchical way

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

Example of directory structure at YNiC

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

/home/alex/Thesis.doc is an example of - (5)

A

location of file named “Thesis.doc” within a hierarchical directory structure

’/’ is root directory

‘home is subdirectory within root directory

‘alex’ is subdirectory within ‘home’ directory

‘Thesis.doc’ is a file within ‘alex’ directory

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

In /home/alex/Thesis.doc you specificy where your file ‘Thesis.doc’ is by…

It is the path - (2)

A

You specificy where your file is by putting directory names and then slashes and directory names and slashes etc…

Its the path to the file of Thesis.doc

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

In naming files in file system, you should not have….

example - (4)

A
  • No ‘funny’ characters like spaces , question marks in any of file system names

No My Cat Pictures!.doc

Instead

My_Cat_Pictures.doc

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

Don’t put funny characters such as spaces and exclamation marks as - (2)

A

They have meaning to coders

Difficulty to access the files within code as they using special characters in file name

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

Within the colab code itself, we can move around the directory tree and see whats going on in each branch by using - (3) in Python script

A

%cd
%pwd
%ls

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

Why do these have a ‘%’ sign infront of them?

%cd
%pwd
%ls

A

Because these are special commands (not Python) they get a % sign in front of them

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

What does %cd do?

A

The command used to change the current working directory

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

What does %cd / do?

A

The command used to change current working directory to the ‘root’ of the file system = top of directory structure

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

What does pwd in %pwd stand for?

A

‘Print Working Directory’

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

What does %pwd do? - (2)

A

Prints the current working directory giving an indication of where you are currently in the directory

Tells you where ‘Where Am i?’

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

What does %ls do?

A

This command lists all the contents in the current working directory and gives you all the file names

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

What does %ls - lt do? - (2)

A

The command lists all the contents in current directory in a ‘long’ way and sorts all the file names by ‘time’

gives more info of file names and order them by time

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

What does

%cd /
%pwd

give as output?

A

/ - %cd

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

%cd /
%pwd
%ls

What will %ls do?

A

It will list all the contents in current working directory which is the ‘root’/top of the directory system

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

%cd /
%pwd
%ls

Example of what %ls will print

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

%cd /
%pwd
%ls
%ls - lt

What will %ls-lt give as output

Example of what %ls - lt will print

A

It will list all the contents in current working directory which is ‘root’ and do it in ‘long’ way in which it will give more info of file names (e.g., how big the files are) in the ‘root’ as well as order them by time and date

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

The directories exist in a

A

‘tree’

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

Branches of directory system are indicated by names separated by more

A

‘forward slash’

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

To ‘change directory’ you can use …. and give the place you want to go as…

A

To ‘change directory’ you use the ‘cd’ command and give the place you want to go as the ‘argument’.

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

To go to ‘home’ directory on Google Colab System, you type

Print current working directory

As well as list what is there

A

%cd / home
%pwd
%ls

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

How to go to /content/sample_data directory and then list it in long format? - (2)

A

%cd /content/sample_data

%ls - lt

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

Another magic command in Colab is history (%history) command in which generates

A

displays every commands and piece of code you’ve entered in the current session

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

What are examples of magic commands in Colab - 6

A
  • %history
  • %cd
    -%pwd
    -%ls
    -%magic
    -%mkdir
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
32
Q

What are magic commands?

A

Things that are not Python but which do useful things in Colab notebook

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

Example of output that typing %history gives:

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

Example of typing in %magic into Python

A

If you need more help on magic commands it prints out a bunch of information on it

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

What does the magic command of %mkdir do?

A

is a magic command in IPython/Jupyter notebooks used to create a directory (folder) in the current working directory

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

Write a code to check and annotate and give output - (6)

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)

Go to the home directory and check it

Change to another directory (you may need to create another directory in your home directory using the command ‘mkdir’ to do this) and check that the present working directory changes

A

checks where we are and checks if we are at the home directory

%pwd
#checks what our present working directory is and makes a note of it

%cd/ home #changes current working directory to home

%pwd

%mkdir test # creates another directory called ‘test’ in home

%ls #produces a list of things currently in the working directory and checks there is something there called test

%cd test # this changes current working directory to test

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

Most of directories in Google Collab you can’t change but you can change most of them in

A

home directory and put things in there if I need to

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

What are #comments in Python? - (2)

A
  • Comments are part of your code that that the computer ignores
  • This allows you to add notes, explanations or documentation to your program in English so that the person reading the script will understand what is happening
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
39
Q

How to add comments in Python? - (2)

A

The comment character in Python is hash/hashtag ‘ # ‘

We can use the hash at any point in a line and rest of line will be ignored

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

The thing to remember of comments in Python is that you - (2)

A

are commuicating with someone who is reading the script

Be professional and helpful and don’t write too much

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

Example of a helpful comment explaining why you have done something and what are you doing

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

Example of another helpful comment

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

The print function in Python is how we

A

output things to the terminal

44
Q

The print function in Python is used to

A

display information to standard output device, typically console

45
Q

What output would this give?

print(“Hello World!”)

A

Hello World!

46
Q

In print function (e.g., print (“Hello World”) we give it a string which is

A

text we want to put onto the screen

47
Q

Strings in Python can be produced either with

A

double quote “ or single quote characters ‘

48
Q

What are strings in Python? - (2)

A

It is a sequence of characters enclosed within a single quote ( ‘ ‘ ) or double quotes (“ “)

Strings are immutable, meaning once they are created, their contents cannot be changed but can produce new strings from existing ones through various operations

49
Q

Strings can be created using either the double quote “ or single quote ‘ characters

Example in print function - (2)

A

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

Both give output of Hello World! twice

50
Q

Why can strings be produced with either single or double quote characters - why are they 2 variants?

A

Sometimes, you need to include quotes within a string

51
Q

The print function is typically used to display… among other… to standard output device such as the console

While it can accept…

A

typically used to display strings among other data types

While it can accept various types of arguments, including integers, floats, and other objects, it is commonly used to output textual information represented as strings.

52
Q

Python does not care if you use single or double quote marks for strings as long as they are

A

matched (as well as contents in string, doesn’t care if its English or other language)

53
Q

Example of using 2 variants of quote characters in string in print function

A

print(“Hello ‘World’ “)

54
Q

What would print(“Hello ‘World’”) give as output?

A

Hello ‘World’

55
Q

Exercise:

Write a code that outputs the following strings: - (2_

It’s the best!

“Hi” they said

A

print(“It’s the best!”)

print(‘ “Hi” they said’)

56
Q

How to include quotes in string? - (2)

A

Create a string using double quotes (“) and then use single quote in string (‘)

The same applies other way around

57
Q

What is string concatenation in Python? - (2)

A

Refers to the process of combining or joining two or more strings together to create a single string.

This operation is commonly performed using the + operator, which concatenates the strings on either side of it.

58
Q

Example of string concatenation in Python

A

print(“Hello” + “World”)

59
Q

What does this give as output?

print(“Hello” + “World”)

A

HelloWorld

60
Q

print(“Hello” + “World”)

gives as output:
HelloWorld

Why does Python not space them out? - (4)

A

Notice that there is no space between the words.

This is because we did not tell Python that it needed to include a space.

Does not know we are writing in English and writing a sentence

Computer programmes will always do exactly what you tell them.

61
Q

print(“Hello” + “World”)

gives as output:
HelloWorld

3 pieces of code to add space between the 2 words - (3):

A

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

62
Q

What does these 3 pieces of code give as output?

print(“Hello “ + “World”)
print(“Hello” + “ World”)
print(“Hello” + “ “ + “World”) - (4)

A

Hello World
Hello World
Hello World

Note all 3 gives same output

63
Q

Aside from ‘print’ing a string or concatenating some strings together, we can manipulate the data before outputting it, such as:

A

We can use ‘len’ on a string to find out how many characters are in it giving a length of a string

64
Q

What is len()

A

use ‘len’ on a string to find out how many characters are in it giving a length of a string

65
Q

Write a code and guess its output that prints how many characters are in Hello World! - (2)

A

print(len(“Hello World”))

gives output as 12

66
Q

In

print(len(“Hello World”) we have emebededd…

  • (2)
A

embedded one function inside another function

i.e., embedded len function inside the print function

It works from inside out and first code works out what is the length of ‘Hello World’ and then prints out to console

67
Q

2 examples of functions

A
  1. len
  2. print
68
Q

What is the idea behind functions?

A

takes arguments (sometimes known as parameters) and then gives you back (returns) something.

69
Q

Functions takes arguments (sometimes known as parameters) and then gives you back (returns) something.

In terms of len function, - (2)

A

function takes a single argument and works out what its length is

It then returns this length as a number which we can either print, put in a variable or otherwise use as appropriate

70
Q

In:

print(len(“Hello World”))

it, outputs

12

by.. - (4)

A

In this code, we passed string (“Hello World” to len function

It works from inside out

The len function first calculate the number of characters in string and then return it

This value will then be passed into print in which it will print number of characters in string to console

71
Q

Idea of this working inside out

print(len(“Hello World”)) written in a different way - (3)

A

Python evaluates (‘runs’) the inner most part of code

In this case, its len function and then ends up withe)

It will then evaluate print function call and print is clever enough to know to take 11 and turn into string

72
Q

Exercise- Write code:

Put your name into a string variable

Print the variable onto screen and calculate and print length of name - (3)

A

myName = “Gitanjali”
print(myName)
print(len(myName))

73
Q

Exercise- What is output?

myName = “Gitanjali”
print(myName)
print(len(myName))

A

Gitanjali
9

74
Q

What are variables?

A

A variable is a name for a value (or in Python, objects)

75
Q

What do variables serve as? - - (2)

A

Variables serve as storage containers /boxes for data.

They can hold different types of information, such as numbers, text, boolean values, or more complex data structures.

76
Q

Write me a code that assigns string “Hello World” to variable ‘msg’ and prints it

A

msg = “Hello World”
print(msg)

77
Q

Example of variables as storage containers/boxes

A

put string of ‘name’ into box called ‘my_Name’

78
Q

Example of using a variable

A

msg = “Hello World”
print(msg)

79
Q

What would this code give as output?

msg = “Hello World”
print(msg)

A

Hello World

80
Q

Explain this code:

msg = “Hello World”
print(msg) - (2)

A

We put our string “Hello World” into variable (box) called msg

We then pass the variable onto print function which gives Hello World to console

81
Q

variables are assigned using the assignment operator which is…

Remember… - (2)

A

’ = ‘ which is used to assign a value (e.g., a string) to a variable.

Remember it is a single ‘=’ but double ‘==’ as thats quite different

82
Q

Python variable naming rules - (5)

A
  • Variables can use letters, numbers and underscores
  • Names are case sensitive so msg, Msg, MSG are all different things
  • A name can have a number in it but cannot start with a number: myvar2 is okay but 2myvar is not
  • Avoid naming different variables after Python keywords and standard functions (e.g., list, dict, stre, file, len etc…)
  • Use common sense so its not a good idea to use ‘o’ or ‘O’ as variable name as might mix them up with 0. And in some fonts, ‘i’ and ‘I’ look a bit like ‘1’.
83
Q

What are the 2 ways of variable naming? (esp when you have long variable names like date of birth or student ID)

  • (2)
A
  • camelCase
  • use_underscores
84
Q

What is camel Case? - (3)

A

(So-called because it looks like a camel with humps)

When you use captial letters to show where in the variable names are e.g., myVariable

First lower case letter of word and upper case letter of second word to work out which word starts

85
Q

What is use_underscores - (2)

A

(sometimes called ‘snake case’)

where you use underscores between words in variable name e.g., my_variable

86
Q

With camelCase and under_scores styles, they tend to be used - (2)

A

or different parts of the code, there are a whole set of rules and guidelines about this called PEP8

Generally just stick to one style

87
Q

What will output in this code? - (2)

msg = “Hello World”
print(msg)

msg = “A new world”

print(msg)

A

Hello World
A new world

88
Q

What is happening in this code?

msg = “Hello World”
print(msg)

msg = “A new world”

print(msg)

A

We overwrite the variable ‘msg’ to change it to print “A new world” instead of “Hello World”

89
Q

we can overwrite variables with new values. Once we do that, there is no

A

way of going back in our script - we have lost the original value unless you do something to save and copy it

90
Q

In Colab, you can inspect variables ‘as we go’ using the ‘variable explorer’ on the left hand side of the colab window and values assigned to them in current session

it is dynamic as..

It may slow down… - (2)

A

it is dynamic and changes when you create more variables

May slow down your code the more variables you add, can also delete variables from {x}

91
Q

What output would this code give as output of console:

a = 2
b = 3

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

A

6
5

92
Q

At the end of the code, what can we write test that a and b are the same number?

a = 2
b = 3

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

A

print (a == b)

93
Q

a = 2
b = 3

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

print (a == b)

What output will it give? - (3)

A

6
5
False

94
Q

What is ‘==’ - (3)

A

to compare the equality of two operands. It checks if the values of two operands are equal or not.

If the values are equal, the result of the comparison is True; otherwise, it is False.

known as an equality comparison

95
Q

What is operarands?

A

Operands are the values or variables upon which operators act.

96
Q

What is !=? - (2)

A

another comparison operator in Python, used to determine if two operands are not equal.

It evaluates to True if the values of the operands are different, and False if they are equal.

97
Q

a = 2
b = 3

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

print (a != b)

What output will it give and what is != doing? - (4)

A

6
5
True

!= to test whether two variables are not the same

98
Q

a = 2
b = 3

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

print (a != b)

Why does it give True on !=

A

a is 3 and b is 2, they are not equal, so we get True as our response.

99
Q

a = 2
b = 3
a = b

print(a,b)

Write output and explain code - (4)

A

a = 2 means a is assigned to value 2

b = 3 being b is assigned value 3

a = b meaning a is reassigned to the value of b which is 3

so outputs will be:
3,3

100
Q

a = 2
b = 3
a = b

print(a,b)
print(a==b)

What is output? - (2)

A

3 3
True

101
Q

A way to delete variables in Python

A

Using del in built function

102
Q

Whats the benefit of deleting variables?

A

It cleans up the code as many variables take a lot of memory

103
Q

Write a code that has variable b which is = 10 , print b and then delete - (3)

A

b = 10
print(b)

del(b)

104
Q

b=10
print(b)

del(b)
print(b)

What output and error message will it appear? - (2)

A

10
Name error: name ‘b’ is not defined

105
Q

Coding exercise:

Set a and b to different values

Create a new variable containing a string

Delete one of the variables which you created

Give output as well - (10)

A

a = 10
b = 2
print(a,b)
cat = “Archie”
del(cat)
print(cat)

Output:
10 2
Error Message: name ‘cat’ is not defined

106
Q

Deleting variables using del in built function deletes it from

A

variable explorer {x}