?q=cats
, to pass along additional inputs to a server.Python is another programming language, but it is interpreted (run top to bottom by an interpreter, like JavaScript) and higher-level (including features and libraries that are more powerful).
For example, we can implement the entire resize
program in just a few lines with Python:
import sys
from PIL import Image
if len(sys.argv) != 4:
sys.exit("Usage: python resize.py n infile outfile")
n = int(sys.argv[1])
infile = sys.argv[2]
outfile = sys.argv[3]
inimage = Image.open(infile)
width, height = inimage.size
outimage = inimage.resize((width * n, height * n))
outimage.save(outfile)
import
(like include
) a sys
library (for command-line arguments) and an Image
library.len(sys.argv)
, and then create some variables n
, infile
, and outfile
, without having to specify their types.resize
function, and finally saving it to an output file.Let’s take a look at some new syntax. In Python, we can create variables with just counter = 0
. To increment a variable, we can use counter = counter + 1
or counter += 1
.
Conditions look like:
if x < y:
something
elif x == y:
something
else:
something
{
}
are used for blocks of code), the exact indentation of each line is what determines the level of nesting in Python.Boolean expressions are slightly different, too:
while True:
something
Loops can be created with another function, range
, that, in the example below, returns a range of numbers from 0, up to but not including 50:
for i in range(50):
something
In Python, we’ll start by looking at just a few data types:
bool
, True
or False
float
, real numbersint
, integersstr
, stringsdict
, a dictionary of key-value pairs, that act like hash tableslist
, like arrays, but can automatically resizerange
, range of valuesset
, a collection of unique thingstuple
, a group of two or more thingsIn Python, we can too include the CS50 library, but our syntax will be:
from cs50 import get_float, get_int, get_string
In Python, we can run our program without compiling it with python hello.py
(or whatever the name of our file is).
python
is name of the program that we’re actually running at the command line, and it is an interpreter which can read our source code (written in the language Python) and run it, one line at a time. (Technically, there is a compiler that turns our source code into something called bytecode that the interpreter actually runs, but that is abstracted away for us.)Our first hello.py
program is just:
print("hello, world")
main
function, or anything that we needed to import for the print
function. The print
function in Python also adds a new line for us automatically.python hello.py
.We can get strings from a user:
from cs50 import get_string
s = get_string("Name: ")
print("hello,", s)
s
, without specifying the type, and we can pass in multiple variables into the print
function, which will print them for us on the same line, separated by a space automatically.print(f"hello, {s}")
. Here, we’re saying that the string hello, {s}
is a formatted string, with the f
in front of the string, and so the variable s
will be substituted in the string. And we don’t need to worry about the variable type; we can just include them inside strings.We can do some math, too:
from cs50 import get_int
x = get_int("x: ")
y = get_int("y: ")
print(f"x + y = {x + y}")
print(f"x - y = {x - y}")
print(f"x * y = {x * y}")
print(f"x / y = {x / y}")
print(f"x mod y = {x % y}")
{x + y}
will be evaluated, or calculated, before it’s substituted into the string to be printed.//
operator.)We can experiment with floating-point values:
from cs50 import get_float
x = get_float("x: ")
y = get_float("y: ")
z = x / y
print(f"x / y = {z}")
We see the following when we run this program:
$ python floats.py
x: 1
y: 10
x / y = 0.1
We can print more decimal places with syntax like print(f"x / y = {z:.50f}")
:
x / y = 0.10000000000000000555111512312578270211815834045410
We can see if Python has integer overflow:
from time import sleep
i = 1
while True:
print(i)
i *= 2
sleep(1)
sleep
function to pause our program for one second, but double i
over and over. And it turns out that integers in Python can be as big as memory allows, so we won’t experience overflow for a much longer time.