conda
¶ACTIVITY
(base)
at the beginning of the prompt$
, and other parts of the prompt (e.g. the path)conda create -n cs110 matplotlib pyqt==5.15.7 pytest
This creates a python environment named cs110 that has the libraries matplotlib
, pyqt
and pytest
included in it.
conda activate cs110
This activates the cs110 python environment we just created.
Note that (base)
has changed to (cs110)
.
Python is a programming language.
Humans can describe the things they want the computer to do using a language and style that is comprehensible to humans.
Python the program converts python code into instructions the computer understands (1's and 0's).
There are two ways to run python:
Open your terminal and run:
python
This will start the python console.
You'll know python is running because the terminal prompt will start with >>>
.
ACTIVITY
1 + 1
2 * 5
Among many other things, python can do math!
Python supports mathematical expressions.
1 + 1
9 - 6
2 * 4
6 / 3
The +
, -
, *
, and /
symbols are called operators.
Python has a few other mathematical operators you may be less familiar with (but can be quite handy in some programming scenarios). We'll learn more about them later.
Evaluate the following python expressions in your python console:
7 + 5
8 - 3
2 * 5
9 / 2
Python makes a distinction between integer numbers (int
s) and floating-point numbers (float
s).
An int
is a positive or negative whole number (including 0). e.g. 1
, 8
, 2000343052
, -34524
, 0
A float
is a real number with a decimal point. 1.0
, 8.4
, 2342234.2348902
, -256.9808
, 0.0
The distinction and purpose of these two kinds of numbers will become clearer over time.
For now, just be aware that both ideas exist.
4 * 4
4 * 4.0
A character is a single letter or symbol. Like A
, z
, $
, or (that's a space)
Strings are strings of characters.
Python uses double or single quotes ("
or '
) to indicate a string.
"hello"
'My name is Dr. Bean'
'1 + 1'
NOTE
print
¶Sometimes you want your program to display information.
You can use the print
function to print information to the screen.
print
sends information to standard out, very often written as stdout
.
A function is a block of code that you can run more than once.
It's like a recipe. The recipe has a series of instructions to follow. You can make the same recipe as many times as you want.
print("hello")
print(5)
print('Printing is boring...')
print('I love printing stuff!')
print('I can print all kinds of stuff.')
print('This is fun!')
Because the computer cannot handle the least degree of ambiguity, every instruction we provide must be precise and exact.
The rules about how our instructions are composed are called syntax.
When you violate these rules, you get a syntax error.
"Hello, my name is Inigo
A quick brown fox"
print(1 + 7
print"hello")
7 /// 5
8 *** 3
print 8
print('They said to say something intelligent")
The python console is a REPL (read-eval-print-loop).
It reads one line of instruction, evaluates that instruction, prints the result of that instruction, and does it again!
In addition to the python console, I can also write a python script.
A python script is a file with the extension .py
that contains python statements.
I can then run a python script from my terminal.
For example, if I make a file named hello.py
with the contents
print('Hello world!')
I can run or invoke the script from my terminal (assuming hello.py
is in my current directory):
python hello.py
DEMONSTRATION
This demonstrates one of the lab activities
hello.py
. Implement.cd
to the directory containing the file.python hello.py
THEN:
hello.py
(e.g. additional print
statements)print
statements create output.The console echos the value of the last expression.
Scripts don't do this.
If you want a python to print something in a script, use print
.