In [ ]:
print('Hello, World!')
Choose File > New Window. An empty window will appear with Untitled in the menu bar. Enter the following code into the new shell window. Choose File > Save. Save as hello.py
, which is known as a python module
. Choose Run > Run module to run the file
In [ ]:
3*4
Division:
* Floating point `/`
* Integer `//`
Try these:
5/4
1/0
3//4
5//4
In [15]:
3//4
Out[15]:
In [16]:
# Exponents
2**3
Out[16]:
In [17]:
# Modulus
5%4
Out[17]:
Python reads left to right. Higher precedence operators are applied before lower precedence operators. Operators below are listed lowest precedence at the top.
Operator | Description |
---|---|
or | Boolean OR |
and | Boolean AND |
not | Boolean NOT |
in, not in, is, is not, < , <= , > , >= , != , == |
Comparison, including membership tests and identity tests |
+ , - |
Addition and Subtraction |
* , / , // , % |
Multiplication, division, integer division, remainder |
** |
Exponentiation |
Calculate the result of 5 + 1 * 4
.
We override the precendence using parenthesis which are evaluated from the innermost out.
Calculate the result of (5+1) * 4
.
Remember that multiplication and division always go before addition and subtraction, unless parentheses are used to control the order of operations.
In [19]:
(2 + 2) ** 3
Out[19]:
In [25]:
fred = 10 + 5
type(fred)
Out[25]:
In [26]:
fred = 10 / 5
type(fred)
Out[26]:
In [28]:
fred * 55 + fred
Out[28]:
In [29]:
joe = fred * 55
joe
Out[29]:
In [33]:
joe
Out[33]:
In [34]:
fred
Out[34]:
In [32]:
joe = fred
fred = joe
Variables begin with a letter followed by container letters, numbers and underscores
jim
other_jim
other_jim99
symbol$notallowed
5startswithnumber
Reserved words | ||||
---|---|---|---|---|
None | continue | for | lambda | try |
True | def | from | nonlocal | while |
and | del | global | not | with |
as | elif | if | or | yield |
break | except | in | raise |
You can use the _
variable to refer to the result of a previous calculation when working in the shell.
In [39]:
ends_with_9 = 9
a = 6
b = 4
In [35]:
my_var = 7
In [ ]:
In [ ]:
num_apples * 65
In [40]:
doesntexist
In [ ]:
name = input("What's your name? ")
print("Hi ", name)
'
or double quote "
. The general rule is to use the single quote unless you plan to use something called interpolationStrings support templating and formatting.
In [47]:
id("bar")
Out[47]:
In [48]:
fred = "bar"
id(fred)
Out[48]:
In [ ]:
"this string is %s" % ('formatted')
In [ ]:
"this string is also {message}. The message='{message}' can be used more than once".format(message='formatted')
In [ ]:
# Called string concatenation
"this string is "+ 'concatenated'
In [ ]:
## Conditionals
`if (condition):`
`elif (condition):`
`else (optional condition):`
In [ ]:
aa = False
if aa:
print('a is true')
else:
print ('aa is not true')
In [ ]:
aa = 'wasdf'
if aa == 'aa':
print('first condition')
elif aa == 'bb':
print('second condition')
else:
print('default condition')
In [ ]:
[(a, a*2) for a in range(10)]
We can use conditionals as well
In [ ]:
[(a, a*2) for a in range(10) if a < 8]
Additional topics
In [ ]: