MCS 260 Fall 2020
Emily Dumas
print("Hello world!") # TODO: Choose a new greeting
The ignored text is a comment.
Comments should be added where they will make code easier to understand (for others, or for you in the future). They can also be reminders about known problems or future plans, if these are not recorded systematically elsewhere.
$\texttt{name} = \texttt{value}$
Example:
>>> side_length = 5
>>> side_length
5
>>> side_length**2
25
>>> side_length = 6
>>> side_length**2
36
"foo" = 50 # FAILS: can't assign to string
foo = 50 # Works
foo = thing # FAILS: thing is seen as variable name
foo = "thing" # Works
bar = "foo" # Works, bar is now "foo"
bar = foo # Works, bar is now "thing"
print(Hello world) # FAILS: Hello and world are unknown names
# and space between var names not allowed
x = 15
asks Python to remember three things:
$ \boxed{x} \longrightarrow \boxed{15} $
>>> old_semester_tuition = 4763
>>> semester_tuition = old_semester_tuition * (1 + 11.1/100)
>>> semester_tuition
5291.693
Spaces around $\texttt{=}$ are optional.
Variable name prohibitions:
False await else import pass
None break except in raise
True class finally is return
and continue for lambda try
as def from nonlocal while
assert del global not with
async elif if or yield
class_avg = 93.8 # Works
260avg = 93.8 # FAILS: starts with a number
secret code = 12345 # FAILS: spaces prohibited
secret_code = 12345 # Works
SecretCode = 12345 # Works, atypical style
测试成绩 = "great" # Works, not recommended
(The exact rules are complicated and refer to a number of other documents and standards. Ultimately, there are about 120,000 characters allowed.)
>>> s = input("Enter some text: ")
Enter some text: organizing heliotrope
>>> print("You entered:",s)
Your entered: organizing heliotrope
>>> input()
programming exercises
'programming exercises'
>>>
The script greeting.py will greet you by name.
# Greet the user by name
# MCS 260 Fall 2020 Lecture 3 - Emily Dumas
name = input("Enter your name: ")
print("Nice to meet you,", name)
Usage:
$ python greeting.py
Enter your name: Emily Dumas
Nice to meet you, Emily Dumas
$
We can't do arithmetic on input directly, because the input is always a string.
>>> 5 + input("Enter a number: ")
Enter a number: 10
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'int' and 'str'
Instead we need to convert input to a numeric type, using $\texttt{int()}$, $\texttt{float()}$ or $\texttt{complex()}$.
>>> 5 + int(input("Enter a number: "))
Input: 10
15
>>> float(42)
42.0
>>> int(12.9)
12
Supported conversions:
input type → | str | int | float | complex |
---|---|---|---|---|
int() | ✓ | ✓ | ✓integer part | ✗ |
float() | ✓ | ✓ | ✓ | ✗ |
complex() | ✓picky | ✓ | ✓ | ✓ |
complex("1+2j") # Works
complex("(1+2j)") # Works
complex("2j+1") # Fails
>>> float(9_007_199_254_740_992)
9007199254740992.0
>>> float(9_007_199_254_740_993)
9007199254740992.0
Here is a script sumprod.py that reads two floats from the user and prints their sum and product.
# Read two floats and print their sum and product
# MCS 260 Fall 2020 Lecture 3 - Emily Dumas
x = float(input("First number: "))
y = float(input("Second number: "))
print("Sum: ",x,"+",y,"=",x+y)
print("Product:",x,"*",y,"=",x*y)
Usage:
$ python sumprod.py
First number: 1.2
Second number: -3.45
Sum: 1.2 + -3.45 = -2.25
Product: 1.2 * -3.45 = -4.14