MCS 260 Fall 2021
Emily Dumas
print("Hello world!") # TODO: Choose a new greeting
The ignored text is a comment. Comments are used to explanatory text for use by humans.
A comment can take up an entire line, and this is often used to add a header at the top of a script.
# Hello MCS 260 script by Emily Dumas
# Written on 2021-08-26
print("Hello world!")
To set the value of a variable we use an assignment statement. The basic syntax is
$\texttt{name} = \texttt{value}$
Example:
>>> side_length = 5
>>> side_length
5
>>> side_length**2
25
>>> side_length = 6
>>> side_length**2
36
"a" = 50 # FAILS: LHS is a value, not a variable name
a = 50 # Works
a = thing # FAILS: thing is an unknown variable name
a = "thing" # Works
b = "uic" # Works, b is now "uic"
b = a # Works, b is now "thing"
print(b) # The current value of variable b
# appears on the screen
>>> old_semester_tuition = 4763
>>> semester_tuition = old_semester_tuition * (1 + 11.1/100)
>>> semester_tuition
5291.693
Spaces around $\texttt{=}$ are optional.
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 for which characters can appear in variable names are rather complicated.)
Every object in Python (whether a variable or a literal) has a type. You can determine the type using the built-in function $\texttt{type()}$:
str means string, a sequence of characters
>>> type("Hello world!")
<class 'str'>
int means integer
>>> type(77)
<class 'int'>
float means floating-point number
>>> type(0.1)
<class 'float'>
In Python, you are free to change the type of a variable at any time.
Many languages don't allow this!
x = 5 # x is an int
x = 3.14159 # now it's a float
x = "umbrella" # now it's a string
The $\texttt{print()}$ function can accept any number of values, of any types, in a comma-separated list.
The basic syntax is $\texttt{print(val1, val2, val3, ...)}$.
>>> print("The decimal value of binary 1001 is",0b1001)
The decimal value of binary 1001 is 9
>>> print("The sum of",99,"and",0b10,"is",99+0b10)
The sum of 99 and 2 is 101
>>> print(1,1.0,1+0j)
1 1.0 (1+0j)
>>>
In the output, values are separated by spaces.
The $\texttt{input}()$ function waits for the user to type a line of text in the terminal, optionally showing a prompt.
Then, the place where $\texttt{input}()$ was called gets replaced with the string the user entered.
>>> s = input("Enter some text: ")
Enter some text: organizing heliotrope <--- keyboard input
>>> print("You entered:",s)
Your entered: organizing heliotrope
>>> input()
programming exercises <--- keyboard input
'programming exercises'
>>>
Let's write a program that will ask the user for their name, and then display a greeting.
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 | ✓ | ✓ | ✓ |
Let's write a script to compute the area and perimeter of a rectangle.
It will ask the user for the dimensions using $\texttt{input()}$ and then print the results.