$ python3
Python 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:54:40) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> print("Hello world!")
Hello world!
>>> course = "pc204"
>>> print(course)
pc204
>>> pi = 3.14159265
>>> print(pi)
3.14159265
>>> exit()
$
#
# hello.py - a simple python program
# Author: Tom Ferrin
# Updated: Sep 25, 2018
#
print("Hello world!")
course = "pc204"
print(course)
pi = 3.14159265
print(pi)
$ python3 prog.py
Hello world!
pc204
3.14159265
$
fraction = 59 / 60
The value of fraction is 0. In Python 3, the result of integer division is of
type float, and thus the value of fraction would be 0.9833333.
fraction = 59. / 60
If you're using Python 3 and want floor division, then you need to use the // operator...
quotient = 59 // 60
1 + 1 ** 5 - 2 <== evaluates to 0
(1 + 1) ** (5 - 2) <== evaluates to 8
6 + 4 / 2 <== evaluates to 8
degrees / 2 * pi <== is different than...
degrees / (2 * pi)
Operators | Description |
---|---|
x or y | Logical ‘or’ (y evaluated only if x is false) |
x and y | Logical ‘and’ (y evaluated only if x is true) |
not x | Logical negation |
<, <=, >, >=, = =, <>, !=, is, is not, in, not in | Comparison operators, identity tests, sequence membership |
x | y | Bitwise or |
x ^ y | Bitwise exclusive or |
x & y | Bitwise and |
x<<y, x>>y | Shift x left or right by y bits |
x + y, x - y | Addition/concatenation, substraction |
x * y, x / y, x % y | Multiplication/repetition, division, remainder/format |
-x, +x, ~x | Unary negation, identity, bitwise complement |
x[i], x[i:j], x.y, x(...) | Indexing, slicing, qualification, function calls |
(...), [...], {...}, "..." | Tuple, list, dictionary, conversion to string |
a = 3.14159
b = 5
c = a + b
What numeric type is "c"?
def MyFunction():
print("hello world")
pi = 3.14159265
print(pi)
To call this function, you just refer to it by name (but you must include the parentheses)...
MyFunction()
Functions can take arguments, which are objects that are passed
to the function when it's called. And functions can return values
back to the caller.
def MyOtherFunction(arg):
ans = (arg + 7) * 10.0
return ans
x = MyOtherFunction(5)
print(x) <=== prints 120.0
def function_name(arg1, arg2, ... argN): # header line
statements # function body
statements # function body
return some_expression # end of function
def printProduct(i, j):
print(i * j, end=' ') # "end" replaces newline character with space
def printRow(n):
for i in range(1, 10): # this is a "for" loop (more about this in week 3)
printProduct(i, n) # call function printProduct 10 times
print() # start a new line
def printTable():
for i in range(1,10):
printRow(i)
printTable() # function printTable must be defined before here
Output:
1 2 3 4 5 6 7 8 9
2 4 6 8 10 12 14 16 18
3 6 9 12 15 18 21 24 27
4 8 12 16 20 24 28 32 36
5 10 15 20 25 30 35 40 45
6 12 18 24 30 36 42 48 54
7 14 21 28 35 42 49 56 63
8 16 24 32 40 48 56 64 72
9 18 27 36 45 54 63 72 81
(This output could be made to look much nicer using a formatted print statement
which we'll cover in week 3.)
print i * j
But in Python 3 print is a function call...
print(i * j)
def printProduct(i, j):
print i * j, # the comma suppresses a new line after each number
def printRow(n):
for i in range(1, 10): # this is a "for" loop (more about this in week 3)
printProduct(i, n) # call function printProduct 10 times
print # start a new line