In [ ]:
ls
In [ ]:
pwd
In [ ]:
cd 2017oct04
We're in a new folder now, so issue commands in the next two cells to look at the folder content and list your current path:
In [ ]:
ls
In [ ]:
pwd
Now test out a few more things. In the blank cells below, try the following and discuss in your group what each does:
In [ ]:
ls M52*fit
In [ ]:
ls M52-001*fit
In [ ]:
ls *V*
What does the asterisk symbol * do?
Answer: Is a placeholder (wildcard) for some text in a file/folder name.
Now, return to where you started, by moving up a directory:
(one directory up from where you are is denoted with .., while the current directory is denoted with .)
In [ ]:
cd ..
mkdir to make a new directory:mkdir new_project_name
cp to copy a file:cp existingfile newfilename
or
cp existingfile newlocation
mv to move or rename a file:mv old_filename_oldlocation old_filename_newlocation
or
mv old_filename_oldlocation new_filename_oldlocation
rm to PERMANENTLY delete (remove) a file... (use with caution):rm file_I_will_never_see_again
(1) Make a new directory, called temporary
(2) Go into that new directory
(3) Move the file test_file.txt from the original directory above (../test_file.txt) into your current location using the .
(4) Create a copy of test_file.txt with a new, different filename of your choice.
(5) Delete the original test_file.txt
(6) Go back up into the original location where this notebook is located.
In [ ]:
# Make a new directory, "temporary"
In [ ]:
# Move into temporary
In [1]:
# Move the test_file.txt into this current location
In [ ]:
# Create a copy of the test_file.txt, name the copy however you like
In [ ]:
# Delete the original test_file.txt
In [ ]:
# Change directories to original location of notebook.
If all went according to plan, the following command should show three directories, a zip file, a .png file, this notebook, and the Lab6 notebook:
In [ ]:
ls
And the following command should show the contents of the temporary folder, so only your new text file (a copy of test_file.txt, which is now gone forever) within it:
In [ ]:
ls ./temporary/
In [ ]:
2 < 5
In [ ]:
3 > 7
In [ ]:
x = 11
In [ ]:
x > 10
In [ ]:
2 * x < x
In [ ]:
3.14 <= 3.14 # <= means less than or equal to; >= means greater than or equal to
In [ ]:
42 == 42
In [ ]:
3e8 != 3e9 # != means "not equal to"
In [ ]:
type(True)
You see that conditions are either True or False (with no quotes!) These are the only possible Boolean values (named after 19th century mathematician George Boole). In Python, the name Boolean is shortened to the type bool. It is the type of the results of true-false conditions or tests.
Now try executing the following two cells at least twice over, with inputs 50 and then 80.
In [ ]:
temperature = float(input('What is the temperature in Fahrenheit? '))
In [ ]:
if temperature > 70:
print('Wear shorts.')
else:
print('Wear long pants.')
The four lines in the previous cell are an if-else statement. There are two indented blocks: One comes right after the if heading in line 1 and is executed when the condition in the if heading is true. This is followed by an else: in line 3, followed by another indented block that is only execued when the original condition is false. In an if-else statement, exactly one of the two possible indented blocks is executed.
In [ ]:
names = ['Henrietta', 'Annie', 'Jocelyn', 'Vera']
In [ ]:
for n in names:
print('There are ' + str(len(n)) + ' letters in ' + n)
This is an example of a for loop. The way a for loop works is a follows. We start with a list of objects -- in this example a list of strings, but it could be anything -- and then we say for variable in list:, followed by a block of code. The code inside the block will be executed once for every item in the list, and when it is executed the variable will be set equal to the appropriate list item. In this example, the list names had four objects in it, each a string. Thus the print statement inside the loop was executed four times. The first time it was executed, the variable n was set equal to Henrietta. The second time n was set equal to Annie, then Jocelyn, then Vera.
One of the most common types of loop is where you want to loop over numbers: 0, 1, 2, 3, .... To handle loops of this sort, python provides a simple command to construct a list of numbers to iterate over, called range. The command range(n) produces a list of numbers from 0 to n-1. For example:
In [ ]:
for i in range(5):
print(i)
There are also other ways of iterating, which may be more convenient depending on what you're trying to do. A very common one is the while loop, which does exactly what it sounds like it should: it loops until some condition is met. For example:
In [ ]:
i = 0 # This starts the initial value off at zero
while i < 11:
print(i)
i = i + 3 # This adds three to the value of i, then goes back to the line #3 to check if the condition is met