0% found this document useful (0 votes)
13 views1 page

3 Interpreter: Key Terms: Reading: Severance 1 Exercise: Write A Program That Greets The User With A Hearty Salutation

Lecture 3 CS 131B

Uploaded by

ucdavispikachu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views1 page

3 Interpreter: Key Terms: Reading: Severance 1 Exercise: Write A Program That Greets The User With A Hearty Salutation

Lecture 3 CS 131B

Uploaded by

ucdavispikachu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

CS131B Instructor’s notes 2024

3 Interpreter
Key terms: python3 >>> .py # import

Reading: Severance 1

Exercise: Write a program that greets the user with a hearty


salutation.

The more common implementation of the Python language is itself written


in C and thus more specifically called CPython. On hills it is installed at
/usr/local/bin/python3 and can be invoked as python3. Plain python is no
longer present because the name used to refer to version 2.

The interpreter can be invoked in both an interactive and a batch mode and
you should expect to use both. The former, whose prompt is >>>, allows
iteratively composing and assembling expressions. The latter runs whole
programs that are stored on disk.

A vanilla installation of Python is composed of a base language and stan-


dard packages that are always available but not loaded by default. To
access those packages we import them. A great variety of useful packages
is available and accessible.7 Give your program a name different from all
the language keywords and modules you are using, or the interpreter may
favor your definition over its own, breaking imports.

>>> import time


>>> time.localtime().tm_year
2020
>>> exit()

In this course we create useful programs based on randomness, user input,


and resources on disk. To simply run a program, we can pass its pathname
to the Python virtual machine:

[yourname@hills ~]$ python3 estimate_earth_mass.py


According to my calculations, earth mass is about 6 x 10^27 grams

Often, we have to tell a program what data we want it to use. Programs


passed no arguments often have nothing to do. In this course our programs
are not interactive, and don’t prompt the user for input; they need to be
easy to test in peer review, so parameters are passed as command line
arguments.

[yourname@hills ~]$ python3 capitalize.py hello


HELLO

(It is possible to avoid specifying the interpreter every time, but you can
safely ignore this optional technique. When the hashbang line8 , #!/usr/local/bin/python3,
is atop the file, and the executable permission is set, and the program path
unambiguous, the shell will launch Python on your behalf:)

[yourname@hills ~]$ ./capitalize.py helloagain


HELLOAGAIN

Next, we will address the basic object types.

7 https://docs.python.org/3/library/index.html
8 https://en.wikipedia.org/wiki/Shebang_(Unix)

You might also like