input Functionif Statementsif-else Statementsif Statementsif-elif-else StatementsI have posted homework 1 here.
It is due this coming Sunday at 11:59 PM.
Some you have not created a class directory and a Unix account.
All work for this course MUST be copied to your class directory
A directory is what you probably call a "folder".
Without a class directory you cannot submit assignments.
If you do not have a Unix account go to http://www.cs.umb.edu/~ghoffman/linux/cs_portal.html#creating_unix_account.
If you do have a Unix account you still need a class directory for this course.
Go to https://portal.cs.umb.edu/accounts/login/ , log in, click the check box for my section of this course, and click submit.
If you have difficulty creating an account must get help NOW!
You can see me after class or during office hours.
You can contact me on Zoom during office hours.
To see how to do this go here
Or you can contact the Class Assistant Thomas Siu at Thomas.Siu001@umb.edu.
Whatever it takes, you MUST create a Unix account and a class directory.
This class and the two classes next week will be review classes.
I will cover the material from IT 116 that you need to know for this class.
You should use this period to brush up on your knowledge of Python.
You may find it useful to look at my materials for IT 116.
I have a web site where I post most of the materials for my courses.
You will find the IT 116 materials at http://96.126.105.215/umb_classes_html/it116_html/class_page_it116.html
I have included this link on the class page for the course.
Go to the index for the Class Notes and search for a topic.
You will find the index here http://96.126.105.215/umb_classes_html/it116_html/class_notes_it116/class_notes_index_it116.html .
Are there any questions before I begin?
$ python3
$ python3
Python 3.8.10 (default, Jun 2 2021, 10:49:15)
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> name = "Glenn"
>>> print(name)
Glenn
>>>
python3
python instead you would get the
following error message
$ python
Command 'python' not found, did you mean:
command 'python3' from deb python3
command 'python' from deb python-is-python3
exit()
VARIABLE_NAME = EXPRESSION
name = "Glenn Hoffman"
int will try to convert its argument into as integer
>>> value = "5" >>> type(value) <class 'str'> >>> numb = int(value) >>> numb 5 >>> type(numb) <class 'int'>
1,2,3,4,5,...
..., -3,-2,-1,0,1,2,3, ...
0.0, 1.0, 1.5, 1.6, ...
>>> team = 'Red Sox' >>> name = "Glenn Hoffman"
rate = old_rate
rate = .04
rate = old_rate * 3
rate = int(input("Rate: "))
>>> print("Hello") # but ignore this text
Hello
rate rate_1 Rate_1 _rate
guns&roses # can't use symbols in variable names guns and roses # can't use spaces in variable names 1st_try # can't start a variable name with a digit
input Functioninput function gets a value from the userinput in a script it stops runninginput waits for the user to type some textinput returns this string to
the function call
>>> name = input("Name: ")
Name: Glenn
>>> name
'Glenn'
input is always a string int function
>>> number = int(number) >>> type(number) <class 'int'>
| Symbol | Operation | Description |
|---|---|---|
| + | Addition | Adds two numbers |
| − | Subtraction | Subtracts one number from another |
| * | Multiplication | Multiplies one number by another |
| / | Division | Divides one number by another and gives the result as a decimal number |
| // | Integer division | Divides one number by another and gives the result as an integer |
| % | Remainder | Gives the remainder when dividing one number by another |
| ** | Exponent | Raises a number to a power |
>>> a = 2 >>> b = 4 >>> a + b 6 >>> a - b -2 >>> a * b 8 >>> a / b 0.5
>>> 4 / 2 2.0 >>> 4 / 5 0.8
>>> 4 // 2 2 >>> 5 // 2 2
>>> -4 // 2 -2 >>> -5 // 2 -3
>>> 17 // 5 3
>>> 17 % 5 2
>>> 3 ** 2 9 >>> 3 ** 3 27 >>> 3 ** 4 81
2 + 3 * 5
>>> 2 + 3 * 5 17
| ** | Exponentiation |
| * / // % | Multiplication, division and remainder |
| + - | Addition and subtraction |
2 + 3 inside parentheses
>>> (2 + 3) * 5 25
>>> 3 * 5 15
>>> 3.0 * 5.0 15.0
>>> 3 * 5.0 15.0
int values, the result will be an intfloat values, the result will be a floatint and a float,
the int value will be temporarily converted to a float
and the result of the operation will be a float
>>> name = "Glenn Hoffman" >>> name 'Glenn Hoffman'
>>> name = "Glenn
File "<stdin>", line 1
name = "Glenn
^
SyntaxError: EOL while scanning string literal
| Escape Character | Effect |
|---|---|
| \n | Causes output to be advanced to the next line |
| \t | Causes output to skip over to the next horizontal tab position |
| \' | Causes a single quote mark to be printed |
| \" | Causes a double quote mark to be printed |
| \\ | Causes a backslash character to be printed |
>>> first = "Glenn" >>> last = "Hoffman" >>> space = " " >>> name = first + space + last >>> name 'Glenn Hoffman'
>>> "Hello" + 5
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: Can't convert 'int' object to str implicitly
boolTrueFalse| Operator | Meaning |
|---|---|
| > | Greater than |
| < | Less than |
| >= | Greater than or equal to |
| <= | Less than or equal to |
| == | Equal to |
| != | Not equal to |
>>> "mark" < "marker" True
>>> "abc2"> "abc1" True >>> "abc@" < "abc#" False
>>> "Sam" == "sam" False
if Statementsif statement
if BOOLEAN_EXPRESSION:
STATEMENT
STATEMENT
...
if-else Statementsif statement only provides one alternative path through
the code
if-else statements provide two alternative paths
if BOOLEAN_EXPRESSION:
STATEMENT
STATEMENT
...
else:
STATEMENT
STATEMENT
...
if Statementsif statement consists of one or more
statements
if statement is a valid Python statementif statement inside anotherif statement
if statement
$ cat it443.py
# determines whether a student can take IT 443
# demonstrates nested if statements
print("This program will tell you if you can take IT 443")
answer = input("Have you taken IT 244 (y/n)? ")
if answer == "y":
answer = input("Have you taken IT 341 (y/n)? ")
if answer == "y":
print("You can take IT 443")
else:
print("You must take IT 341 before you can take IT 443")
else:
print("You must take IT 244 before you can take IT 443")
$ python3 it443.py
This program will tell you if you can take IT 443
Have you taken IT 244 (y/n)? y
Have you taken IT 341 (y/n)? y
You can take IT 443
$ cat grade.py
# this programs turns a score into a letter grade
# it demonstrates using nested if statement
# to test for many possible conditions
score = int(input("What is your score? "))
print("Grade", end=" ")
if score >= 90:
print("A")
else:
if score >= 80:
print("B")
else:
if score >= 70:
print("C")
else:
if score >= 60:
print("D")
else:
print("F")
$ python3 grade.py
What is your score? 80
Grade B
$ python3 grade.py
What is your score? 60
Grade D
if-elif-else Statementsif statement for situations like thisif-elif-else statement
if BOOLEAN_EXPRESSION_1:
STATEMENT
...
elif BOOLEAN_EXPRESSION_2:
STATEMENT
...
elif BOOLEAN_EXPRESSION_3
STATEMENT
...
...
[else:
STATEMENT
...]
else clause means that it is optionalelif keyword is short for "else if"if and elif lines line up verticallyif-elif-else statement
$ cat grade2.py
# this program turns a score into a letter grade
# it demonstrates the if-elif-else statement
score = int(input("What is your score? "))
print("Grade", end=" ")
if score >= 90:
print("A")
elif score >= 80:
print("B")
elif score >= 70:
print("C")
elif score >= 60:
print("D")
else:
print("F")
$ python3 grade2.py
What is your score? 50
Grade F
and operator returns true if both its operands are true
>>> 5 > 4 and 4 > 2 True
and returns false
>>> 5 > 4 and 4 < 2 False
>>> 5 < 4 and 4 < 2 False
or operator returns true if either of its operands are true
>>> 5 > 4 or 4 < 2 True
>>> 5 < 4 or 4 < 2 False
not operator takes only one operand>>> not 5 > 4 False
>>> not 5 < 4 True
and
| p | q | p and q |
|---|---|---|
| true | true | true |
| true | false | false |
| false | true | false |
| false | false | false |
| p | q | p or q |
|---|---|---|
| true | true | true |
| true | false | true |
| false | true | true |
| false | false | false |
not
| p | ! p |
|---|---|
| true | false |
| false | true |
while loop keeps repeating as long as its boolean
expression is true
while BOOLEAN_EXPRESSION:
STATEMENT
STATEMENT
...
True, the statements in the code
block are executed