The first time you login, you will likely want to change your randomly assigned password. Open a terminal window by clicking the terminal button on the bottom toolbar. At the $ prompt, type passwd, press enter and follow the instructions. Note that when you are typing your old or new password, nothing will be displayed to the screen (no *'s and certainly not your password). The program will not allow you to pick a password that it thinks is too short or too easy to guess.
Your student ID has an RFID tag that can allow you access to the building and the labs after hours. This year, you should have automatically have access to the lab space by enrolling in the course. If you are having trouble with the card readers, please let me know.
By default, the terminal starts in a linux shell with a prompt that looks (by default), something like
cumin[~]$The name cumin is the hostname or name of the computer, and is likely different for each student. This linux prompt indicates that the terminal is ready to accept linux commands. Lab 0 asks you to explore some of these linux commands. Additional help is available during the using linux sessions this week. Plus, we'll be practicing the commands in class.
One command python tells linux to start the python shell. This program changes the terminal prompt to
>>>and now the terminal is ready to accept python commands.
cumin[~]$ python Python 2.7.3 (default, Aug 1 2012, 05:14:39) [GCC 4.6.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>>print "welcome to comp. sci." welcome to comp. sci. >>> 2+2 4 >>>4*5 20 >>> quit() cumin[~]$In this mode, python commands are interpreted one line at a time and the result is printed to the screen. Typing quit() in the python shell quits the python interpreter and returns the user to the linux shell. Learn to recognize what program is running by inspecting the prompt.
Syntax describes the structure of a program and arrangement of symbols that a programming language understands. Semantics describes the meaning of the syntax. What happens when x=4 is typed?
When you quit the python shell, commands you entered are lost.
Start vim by typing vim at the linux shell:
cumin[~]$ vim
By default, vim starts in command mode. To actually type text, you need to type i first to enter insert mode. Then you can type python code, a letter to your mom, your top ten vacation destinations, this web page, etc. Press the Esc key to exit insert mode and return to command mode. Type :q! to quit without saving changes. Note we are returned to the linux prompt. Usually we want to save changes to a particular file. We can start vim with an a filename as an argument.
cumin[~]$ vim first.py
Enter insert mode (type i) and type the following:
print "My first saved python program"Press Esc to exit insert mode and :wq to save your program and quit. You can now run your program by typing python first.py at the linux prompt. If you run vim first.py again, you can edit your program, make changes, add features, etc.
You can learn more about vim by typing vimtutor at the linux prompt and following along. You can quit the tutor at any time and come back to it later if you forget something or get bored.
For a more graphical vim experience, you can also type gvim which gives you a menu and some buttons. It's like training wheels for vim. Vim reference cards are available in the lab and online. By no means do you need to memorize all of these commands. I would start with i, :q, :q!, :w, :wq and move on to dd, yy, x, p and others as needed. It sounds a bit insane at the beginning, but you'll get used to it, and feel free to ask if you have questions.
If update21 or handin21 does not work for you or it says you are not allowed to run these programs, email me. It is usually my fault, not yours. I may need to add you to the class roster, change handin to work for the next lab or change permissions on a directory. Trying to submit a lab assignment after the deadline and getting permission denied is one instance in which the error is not my fault. I'll leave it as an exercise for you to determine who is at fault in this case.
print "hello", "there" print "hello"+"there" x=4 x print "Python says x=",x n=input("Enter a number: ") print "You entered",n name=raw_input("Enter your name: ") print "Hello",name
licorice[~]$ cd licorice[~]$ ls Desktop/ Documents/ cs21/ licorice[~]$ cd cs21 licorice[cs21]$ ls examples/ inclass/ labs/ licorice[cs21]$ cd inclass/ licorice[inclass]$ ls licorice[inclass]$ mkdir w01-intro licorice[inclass]$ ls w01-intro/ licorice[inclass]$ cd w01-intro licorice[w01-intro]$ cp ~adanner/public/cs21/inclass/w01-intro/welcome.py . licorice[w01-intro]$ ls welcome.py licorice[w01-intro]$ vim welcome.pycomment in triple block quotes. main function definition (note parentheses). def keyword. indented body. calling or invoking the function.
licorice[w01-intro]$ python welcome.py
welcome to cs21
licorice[w01-intro]$
Open in vim. Edit to get/print name.
Steps to solving a problem computationally:
licorice[w01-intro]$ vim grad.py licorice[w01-intro]$ python grad.py Enter the current year: 2009 Enter your graduation year: 2012 You have 3 year(s) until graduation
#List types. A list of what? range(5) range(1,5) range(1,5,2)Range returns a list. Lists are enclosed in brackets [ ]. Values in a list are separated by commas. This is the syntax of a list.
for <var> in <sequence>: <body>Whitespace is significant. Loop body starts when indentation starts. Loop body ends when indentation ends.
for i in range(1,5,2): print i for i in range(3): print "Hello there"Tracing. Loop semantics. Your ready for lab 01.