Introduction to Programming - 1 Flashcards
What are the 2 types of cells in Colab - (2)
- Text = double click and add notes
- Code = tells google server to do something
Debugging Exercise
Why does this code below not print “Hello World”
print(“Hello World)
Fix the code - (4)
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
Debugging Exericse
What error message will this piece of code produce?
print(“Hello World) - (2)
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
In this code,
print(“Hello World) you add another quote mark at the end like so:
print(“Hello World”) since
quotation marks have to match up in Python
On any computer everything is stored in a
‘file system’
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 single slash or ‘root directory’
(bottom of the directory system)
The directory structure of a computer refers to how
files and folders are organized within a file system , organised typically in hierarchical way
Example of directory structure at YNiC
/home/alex/Thesis.doc is an example of - (5)
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
In /home/alex/Thesis.doc you specificy where your file ‘Thesis.doc’ is by…
It is the path - (2)
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
In naming files in file system, you should not have….
example - (4)
- No ‘funny’ characters like spaces , question marks in any of file system names
No My Cat Pictures!.doc
Instead
My_Cat_Pictures.doc
Don’t put funny characters such as spaces and exclamation marks as - (2)
They have meaning to coders
Difficulty to access the files within code as they using special characters in file name
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
%cd
%pwd
%ls
Why do these have a ‘%’ sign infront of them?
%cd
%pwd
%ls
Because these are special commands (not Python) they get a % sign in front of them
What does %cd do?
The command used to change the current working directory
What does %cd / do?
The command used to change current working directory to the ‘root’ of the file system = top of directory structure
What does pwd in %pwd stand for?
‘Print Working Directory’
What does %pwd do? - (2)
Prints the current working directory giving an indication of where you are currently in the directory
Tells you where ‘Where Am i?’
What does %ls do?
This command lists all the contents in the current working directory and gives you all the file names
What does %ls - lt do? - (2)
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
What does
%cd /
%pwd
give as output?
/ - %cd
%cd /
%pwd
%ls
What will %ls do?
It will list all the contents in current working directory which is the ‘root’/top of the directory system
%cd /
%pwd
%ls
Example of what %ls will print
%cd /
%pwd
%ls
%ls - lt
What will %ls-lt give as output
Example of what %ls - lt will print
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
The directories exist in a
‘tree’
Branches of directory system are indicated by names separated by more
‘forward slash’
To ‘change directory’ you can use …. and give the place you want to go as…
To ‘change directory’ you use the ‘cd’ command and give the place you want to go as the ‘argument’.
To go to ‘home’ directory on Google Colab System, you type
Print current working directory
As well as list what is there
%cd / home
%pwd
%ls
How to go to /content/sample_data directory and then list it in long format? - (2)
%cd /content/sample_data
%ls - lt
Another magic command in Colab is history (%history) command in which generates
displays every commands and piece of code you’ve entered in the current session
What are examples of magic commands in Colab - 6
- %history
- %cd
-%pwd
-%ls
-%magic
-%mkdir
What are magic commands?
Things that are not Python but which do useful things in Colab notebook
Example of output that typing %history gives:
Example of typing in %magic into Python
If you need more help on magic commands it prints out a bunch of information on it
What does the magic command of %mkdir do?
is a magic command in IPython/Jupyter notebooks used to create a directory (folder) in the current working directory
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
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
Most of directories in Google Collab you can’t change but you can change most of them in
home directory and put things in there if I need to
What are #comments in Python? - (2)
- 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 to add comments in Python? - (2)
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
The thing to remember of comments in Python is that you - (2)
are commuicating with someone who is reading the script
Be professional and helpful and don’t write too much
Example of a helpful comment explaining why you have done something and what are you doing
Example of another helpful comment