Professional Documents
Culture Documents
5453
5453
in most programming languages look daunting and scary, Python uses English keywords
and natural-sounding syntax. For example, this is a declaration and an if-condition
in
Python:
x = 1
if x > 0:
4.1 Setting up
Runcs0160install pythonIntrofrom the command line. This will install a folder
pythonIntroin your cs0160directory. It should contain two
lesprimePrinter.pyand
sectionOne.py.
Before we begin programming, we need to con
gure the editor you will use to write
your Python code. While you are free to use any editor of your choice, we recommend
you use Atom. If you are working from a computer in the Sunlab or MSlab, complete
Section 4.2, which will tell you how to con
gure Atom universally for Python code, and
skip over Section 4.3. If you have Atom locally on your personal computer, you can
follow
the same instructions to con
gure it, although some of the instructions may be a little
di#erent depending on how you work locally. If you are working remotely, jump right
to
section 4.3 to learn how to set up Gedit, which performs better over SSH. Either
way, be
sure to begin again at Section 4.4, where you will write your
rst Python program!
4.2 Working from the Sunlab or MSlab
Open the text editor Atom, by typingatom &in your terminal. First we will make
the .py
le you will write your program in.
1. Create a new
le: File>New File
2. Save this
le, File>Save, naming ithelloWorld.py. The .pyis very important!!
Make sure the
le is saved in your~/course/cs0160/pythonIntrodirectory.
We now need to con
gure Atom to work best with Python:
1. From the menu bar, select Edit>Preferences. This should open up a new tab in the
editor. Scroll down to theEditor Settingssection. This is where you can con
gure
di#erent preferences for your Atom. Take a look at some of the options and feel
free
to play around with them.
Introduction to Python - 2018 March 4, 2018 2
CS 16
Introduction to Python - 2018
Introduction to Algorithms and Data Structures
2. Change theTab Lengthto be4and make sure theTab Typeis set to soft.
3. Close this tab and you're ready to go!
4.3 Working romotely over SSH
Gedit performs much better over SSH, so you should use this program to work on the
lab
if you are not on a CS department computer.
Typegedit &into a terminal and press Enter to open Gedit.
First we will make the.py
le you will write your program in.
1. Save the current (blank) new
le: File>Save as...
2. Name the
lehelloWorld.py. The .pyis very important!! Make sure the
le is saved
in your ~/course/cs0160/pythonIntrodirectory.
Next, we have to con
gure Gedit to work well with Python.
1. Go toEdit->Preferences
2. Click on theEditortab
3. Ensure thatTab widthis set to 4
4. Check the box that saysInsert spaces instead of tabs
Close out of the preferences window. You're all set!
4.4 Let's get to coding!
From CS15, you are probably familiar with using these text editors to write Java
(.java)
code. We'll be using them to write Python (.py)
les in CS16.
It's important you have con
gured your editor as speci
ed above because Python uses
whitespace indentation to delimit your code (more on this later). For the sake of
conve-nience, we insist that you use 4 spaces to indent your code. It will make
your code look
consistent across machines and prevent inconsistencies between spaces and hard
tabs.
Now, let's begin! Type:
print `Hello world!'
and save your
le. Now go back to your terminal, make sure you are in thepythonIntro
directory and typepython helloWorld.pyto run the program. It will printHello world!
to your terminal.
Hold on, do you really have to typepython yourProgramName.pyevery time you want
to run a program? (Or for the especially lazy, scroll through your commands until
you
nd
the last time you typed it?) Heck no! Go back to your editor and type:
Introduction to Python - 2018 March 4, 2018 3
CS 16
Introduction to Python - 2018
Introduction to Algorithms and Data Structures
#! /usr/bin/python
at the top of yourhelloWorld.py
le. This tells your machine to use Python to interpret
the
le when executed. Then save the
le, go back to your terminal, and type chmod +x
helloWorld.pyto make the
le an executable. (If you haven't used chmodbefore, it's a
terminal command used to change
le permissions, in this case to make your Python
le
executable. The +xargument adds executability for the owner of the
le, you!) Now if
you type./helloWorld.pyinto your terminal your program prints Hello world! to the
terminal. From now on, all of your Python
les should start with#! /usr/bin/python.
5 Python Syntax
Let's say that instead of wanting to write a program that just prints \Hello
world!" and
then ends, you wanted to write a program with a function that takes in a string
with your
name as the parameter, and prints \Hello<name>!" Following the CS16 Python coding
conventions, the function would look like this:
def say_hello(name):
"""say_hello: string -> nothing
Purpose: prints a greeting of the form "Hello <name>!"
Example: say_hello("Seny") -> "Hello Seny!"
"""
print "Hello " + name + "!" #this is the function body
When you de
ne a function in Python, you simply writedef(which is short for de
ne),
followed by the name of the function, with all words lowercase and separated by
underscores,
then the parameters in parentheses, and lastly a colon. Note that you do not need
to
specify the type of your parameters in Python! Next, document your function with a
block