Session 1 - Part I Flashcards
On any computer everything is stored in a
file system
What is a file system? - (2)
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.
What analogy is often used to describe the structure of a file system, and what is its ‘trunk’ equivalent?
A file system is often likened to a tree structure, where the ‘trunk’ represents the ‘base’ of file system.
The ‘base’ of the file systems is commonly denoted by computer as:
single slash ‘/’ character or known as ‘root directory’
What is a root directory? - (2)
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 (/)
Diagram of file system at YNiC
What is the path of the file “Thesis.doc” provided in the diagram?
The path of the file “Thesis.doc” is /home/alex/.
Why is it important to avoid certain ‘funny’ characters like spaces, question marks etc… in file system names?
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
Naming file in file system using ‘funny characters’ example
My Cat Pictures!.doc
What advice is given regarding file naming conventions in the notes, and why is it important to follow such conventions? - (2)
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.
Example of following conventions when naming files in file system
So instead of: My Cat Pictures!.doc
use…
My_Cat_Pictures.doc
What special character precedes commands executed within the Colab code cells, and what does it signify? - (2)
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
What command is used to change the current directory within a Colab code cell?
The ‘%cd’ command is employed to change the current directory within a Colab code cell
what argument does %cwd require?
It necessitates specifying the desired directory it wants to change the current directory to as its argument.
What does %cwd / mean?
Changes the current directory to the ‘root’ directory of the file system
What does the ‘%pwd’ command do within a Colab code cell?
The ‘%pwd’ command in a Colab code cell prints the present working directory, revealing the current location within the file system.
What does %pwd stand for?
‘Print Working Directory’ or ‘Where am I?’
What does %cd stand for?
Change Directory
What command is used to list the contents of the current directory within a Colab code cell?
The ‘%ls’ command
What does %ls represent in Colab code cell?
‘List’
What does %ls -lt represent in Colab code cell?
%ls -lt’ stands for ‘List - Long, sorted by time’
How can one list the contents of the current directory in a ‘long’ format sorted by ‘time’ within a Colab code cell?
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.
The directories exist in a
‘tree’
What will this code give you?
%cd /
%pwd
%ls
%ls - lt
- (3)
%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
What does the ‘%history’ command do in Colab notebooks?
The ‘%history’ command in Colab notebooks displays a history of codes and commands written
What does the ‘%magic’ command do in Colab notebooks?
The ‘%magic’ command in Colab notebooks provides information and documentation about available commands and their functionalities.
List all the special commands used in Colab notebooks - (7)
‘%cd’,
‘%pwd’, ‘
%ls’,
‘%ls -lt’,
‘%history’,
‘%magic’.
&mkdir
What does the ‘%mkdir’ command do
It creates a new directory within the current working directory.
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
%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
What character represents comments in Python?
The hashtag symbol (#) represents comments in Python.
Why are comments important in programming?
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
Comments are parts of your code that the
computer ignores
What should you consider when writing comments in your code? - (2)
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
Example of comment
This is a comment
Example of writing comment of explaiing why you are doing something as well as describing what the code is doing?
print(‘Hello Again! This is running in Colab!’) # Print out a nice message on the otput.
What is the output of this code?
print(‘Hello Again! This is running in Colab!’) # Print out a nice message on the otput.
Hello Again! This is running in Colab!
What does the following line of code do?
print(‘Hello Again! This is running in Colab!’) # Print out a nice message on the otput.
The line of code prints the message ‘Hello Again! This is running in Colab!’ to the output console.
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.
The comment provides a note or explanation about the code’s functionality without being executed by the interpreter
What does the following code do?
print(“Hello World!”)
The code prints the string “Hello World!” to the output console.
What is the purpose of the print() function in Python?
The print() function in Python is used to output text or variables to the output console/terminal
How do you use the print() function to display a message? - (2)
To display a message using the print() function, you provide the message inside parentheses as an argument. For example:
print(“Hello, World!”)
What does the print() function do when given multiple arguments? - (4)
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 can strings be created in Python? - (3)
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!”)
Why are there two variants for creating strings in Python? - (3)
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.
Example of using single quotes in a string:
print(“Hello ‘World’”)
Output:
Hello ‘World’
What is a string in Python?
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.
Print() function can take
a string as its argument and prints the content of the string to the standard output (usually the terminal or console)
What is the purpose of the print function in the given Python code?
print(“Hello World!”)
The purpose of the print function in the code is to output the string “Hello World!” to the screen
What happens when you execute the following Python code?
print(“jdkfjdlkfjdslfkjdslkf”) - (2)
Executing the code displays the string “jdkfjdlkfjdslfkjdslkf” on the screen.
The print function outputs the provided string of jdkfjdlkfjdslfkjdslkf to the output terminal/console
What observation is made regarding the usage of quotation marks in the provided Python code?
In the code snippet, both single quotes (‘ ‘) and double quotes (“ “) are used to create strings.
Identify the issue in the following Python code:
print(“Hello World!)
The issue in the code is the absence of a closing double quote, resulting in an “unterminated string literal” error.
What error message is encountered when executing the given Python code?
print(“Hello World!)
The error message encountered is “unterminated string literal”, indicating that the provided string is not properly terminated with a closing double quote.
How does the absence of a closing double quote affect the execution of the Python code?
print(“Hello World!)
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.
Exercise
In the code box below, print the following strings:
It’s the best!
“Hi!” they said - (2)
print(“It’s the best!”)
print(‘“Hi!” they said’)
What is the result of executing the following Python code?
print(“Hello” + “World”)
The result of executing the code is the concatenation of the two strings, resulting in the output “HelloWorld”.
Explain the purpose of the +
operator in the given Python code - (2)
print(“Hello” + “World”).
The +
operator is used for string concatenation in the provided Python code.
It combines the strings “Hello” and “World” into a single string, “HelloWorld”.
What action does the +
operator perform when used with strings in Python?
e.g., print(“Hello” + “World”)
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.
What is string concatenation in Python?
String concatenation involves joining two or more strings to form a single string, typically achieved using the +
operator in Python.
Why does Python not separate “Hello” and “World” in the code print(“Hello” + “World”)? - (2)
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’
What happens if we don’t include a space between the strings in the code print(“Hello” + “World”)?
If we don’t include a space between the strings, Python will concatenate them without adding a space, resulting in “HelloWorld”.
How can we include a space between the strings “Hello” and “World” in the code print(“Hello” + “World”)? - (5)
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
What does the len() function do when applied to a string?
The len() function returns the number of characters in a string.