Pythonintroin Your Cs0160Directory. It Should Contain Two
Pythonintroin Your Cs0160Directory. It Should Contain Two
Pythonintroin Your Cs0160Directory. It Should Contain Two
see what happens without an intermediate compiling step. This makes experimentation
and testing very simple.
Third, a feature of Python that you will grow to love and cherish is how easy it is
to
read. Instead of using a lot of
fpunctuation and other symbols, which are what makes code
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
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)
see what happens without an intermediate compiling step. This makes experimentation
and testing very simple.
Third, a feature of Python that you will grow to love and cherish is how easy it is
to
read. Instead of using a lot of
fpunctuation and other symbols, which are what makes code
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
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
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
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
s beauty.
Python is similar to Java in that it also handles your memory management, meaning
it
allocates memory and has a garbage collector to free up that memory once it is no
longer
needed, making your life a whole lot easier. If you're not already convinced that
Python
rules...
2 Why should you learn Python?
We'll be using Python throughout the semester to code up some of the algorithms
you'll be
learning. As a computer scientist you should get used to learning new languages
readily,
not only because it's an important part of the subject, but also because it's
useful (and
Introduction to Python - 2018
March 4, 2018 1
CS 16