Questions from Day 1?¶

Introduction to Python¶

Python Environments with conda¶

ACTIVITY

  • open terminal
    • terminal on mac
    • anaconda PS prompt on windows
  • note the (base) at the beginning of the prompt
  • what do the different parts of the prompt mean?
    • $, and other parts of the prompt (e.g. the path)
  • run the following commands (or copy them from the lab instructions)
  • a python environment gives us a clean way to have all the third-party code we want to use (called libraries or dependencies)
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 🐍¶

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:

  1. The python console
  2. Python scripts

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

  • Run 1 + 1
  • Run 2 * 5

Among many other things, python can do math!

Math¶

Python supports mathematical expressions.

In [ ]:
1 + 1
In [ ]:
9 - 6
In [ ]:
2 * 4
In [ ]:
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

Integers and Floats¶

Python makes a distinction between integer numbers (ints) and floating-point numbers (floats).

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.

In [ ]:
4 * 4
In [ ]:
4 * 4.0

Strings¶

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.

In [ ]:
"hello"
In [ ]:
'My name is Dr. Bean'
In [ ]:
'1 + 1'

NOTE

  • Make sure you understand the difference between an expression and a string
    • an expression tells Python to do something
    • a string is just a series of characters

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.

In [ ]:
print("hello")
In [ ]:
print(5)
In [ ]:
print('Printing is boring...')
print('I love printing stuff!')
print('I can print all kinds of stuff.')
print('This is fun!')

Syntax¶

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.

In [ ]:
"Hello, my name is Inigo
In [ ]:
A quick brown fox"
In [ ]:
print(1 + 7
In [ ]:
print"hello")
In [ ]:
7 /// 5
In [ ]:
8 *** 3
In [ ]:
print 8
In [ ]:
print('They said to say something intelligent")

REPL¶

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!

What does this look like in the console?¶

Scripts¶

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

PyCharm¶

DEMONSTRATION

This demonstrates one of the lab activities

  • open PyCharm
  • PyCharm is an editor for code, like Microsoft Word is an editor for documents
  • create a project
    • the very first time you use PyCharm, you need to configure which python environments it can use
    • configure the cs110 conda environment
  • tour: the file explorer on the left, code editor on the right
  • create a file named hello.py. Implement.
  • Open the terminal. cd to the directory containing the file.
  • run the script: python hello.py

THEN:

  • Add some other stuff to hello.py (e.g. additional print statements)
  • Note that the results of each line are not echoed. Only the 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.

Key Ideas¶

  • Python environments
  • Python console
  • Math
  • Strings
  • PyCharm
  • Scripts