Housekeeping Here are a few items.
- If you are off campus, install Global Protect. There is a link in the Canvas announcements.
- If you use Windoze, obtain
PuTTY
if you haven't already. - Reading Quiz: Read §§ 1-6 of
p0.pdf
, which you can download in Canvas. This covers the scalar Python types. - Shortly, you will get a communication on your login and
password for
cs.ncssm.edu
. We will go through the login process in class and you will learn how to change your password from the random gibberish I give you.
Two Mini-Challenges Print these to the screen.
* ** *** **** *****
* * * * * * * * * * * * * * * * * * * * * * * * *
Boss Statements: Functions
We wrote a couple of programs and they are BORING! What are we gonna do? We will begin to learn a little about boss statements; these are a remedy.
What is a boss statement? A boss statement is a statement that alters the flow of code. Boss statements have these characteristics.
- Grammatically they are incomplete clauses such as while x > 0, or if x == y,
- They MUST be followed by a block of code. This is one or more indented statements. This is the block of code the boss statement "owns."
Functions
We will learn how to define functions today. They are a way to divide code into logical bits you can use over and over again. You will get some practice writing functions today.
def This is a boss statement that means, "To define," The header of a function looks like this
def function_name(arg(s)): block
- The
def
keyword means, "To define" - We shall see that function names are just variable names, and that the rules for naming functions are the same as those of variables. Smart programmers choose a name that is evocative of what the function does.
- The item
arg(s)
is the argument list. It is a comma-separated list of variable names. It can consist of zero or more items. - The item
block
is an indented sequence of one or more statements that is executed when the function is invoked, or called. It is often called the body or implementation of the function.
What Stays in Vegas Python has function scope; varibles created inside of functions are invisible outside of them. You will quickly see that this is an advantage.
Importing A Module