# Python Introduction

## The Overall "Logic" of All Programs

All computer programs follow the same basic 3-step process: input-process-output. The computer program asks for input data to use in the program, the program processes the data (does to the data what you want it to do), then the program produces the outputs that you want (screen, printer, disc, etc.).

In PYTHON (and many other languages) the keywords are actually prewritten code that is stored as part of the PYTHON language. You have a one word "function" that will tell the computer to perform some type of action. The pre-written code is "called" by the computer program. In this case - to "call" a function is simply to tell the computer to follow that functions pre-written code.

Note the >>> prompt in the examples below indicates that they are running in the PYTHON shell using the "interactive" mode.

## Our First Python Function

The first PYTHON function will be "print". The print function will display something on the computer screen for you to see. The syntax for the print function statement is:

>>> print ('what you want to print')

*Notice that you MUST put the letters in single quotation marks. This is part of the SYNTAX rules of PYTHON.

## Strings and Literals

Sequences of letters, etc. are called "strings". A "string literal" is what we have in the single quotation marks. You can also use the double quotation marks in PYTHON. When you need to use a quotation mark as part of your string literal - you use double quotation marks around the whole string and embed the single quotation mark where it needs to be in the string.

>>> print ("Don't forget to meet me at 1:00 today")

*You can use numbers inside any string literal and they are treated as "alphabetic types of numbers - not ones to do mathematical functions with".

## Comments

You MUST use comments throughout your PYTHON program to explain to others who might look at your computer program what you intended to do in this part of the program. The PYTHON interpreter ignores comments (doesn't "execute" them).

A comment starts with the # character.

>>>#This program prints out the date
>>>print ('January 30, 2017')
January 30, 2017

## Variables

A variable is a name that you create that will represent a value that will be stored in the computer's memory at the time the program is executing. Your program will use variables to use data that has previously been stored that you want to use for your program, or new data that you want to use for your program. REMEMBER - you come up with your own variable names for your program. Choose variable names that make sense. For example, if you have a number that represents someone's age - you would probably use "age" as your variable name.

We need to get actual data into the variable. Remember you just have the NAME of the variable - you need to put some data into it. This is called an Assignment statement - to put a value into a variable name.

variable = expression

>>>age = 25
>>>age
25

*You CAN'T change this order - 25 = age is a SYNTAX error.

A program example:

>>> # This is my first program

>>> width = 10 [press the enter key on your keyboard]

>>> length = 5 [press the enter key on your keyboard]

>>> print ('width') [press the enter key on your keyboard]

width [will be displayed on your screen]

>>> print (width) [press the enter key on your keyboard]

10

>>>

*There is NO quotation marks in >>> print (width) you are asking to print the value inside the variable WIDTH

## Variable Naming Rules

You must follow the rules when you create the names of your variables:

- You can't use one of PYTHON's key words
- A variable name cannot contain spaces
- The first letter MUST be one of the letters A-Z, a-z or the underscore (_) character
- After the first letter you may use the letters A-Z, a-z, digits 0-9 or the underscore
- Uppercase and lowercase characters are DIFFERENT. This means the variable name Age and age are NOT the same!!!

## Displaying Multiple Items with print Function

Within your parethesis for your print statement you can combine the literal part of the statement - things within quotation marks, and the actual name of the variable (so you get the value contained in the variable printed):

>>> #This program prints out my room number

>>> room = 1750

>>> print ('I am staying in room number', room)

I am staying in room 1750

## Reassigning Values to the Same Variable in Your Program

Within a single program - you can reassign a different value to your variable after assigning the original value to the variable.

## Numeric Data Types and Literals

In PYTHON we have several different "data types". These types of data include - int is used for integers, and float is used for real numbers (with decimals).

>>>room = 1750
>>>dollars = 5.76
>>>type(room)
< class 'int' >
>>>type(dollars)
< class 'float' >

We also have the str for our string variables.

>>>first_name = 'Barry'
>>>type(first_name)
< class 'str' >

*Notice that we used underscore character in the variable name. As a general rule of thumb and best practice for programming, NEVER USE SPACE in anything. This includes variables, function names, file names, etc.

## Reading Input from the Keyboard

We can get data "into" our programs by either typing is from the keyboard (direct entry), or from stored data.

The SYNTAX for reading data that is entered from the keyboard is: variable = input (prompt)

>>> age = input('What is your age? ') [press enter]

What is your age? 43 [displays on the screen, type in 43 and press Enter]

>>> print (age) [press Enter]

43 [displayed on the screen]

## Reading Numbers with Input Function

When data is typed from the screen as input it ALWAYS comes in as a string. Thus is we input the number 72 is actually comes in as the string '72'. If we wanted to do any type of math with this value, we need to first convert it to the integer or real number equivalent. There are two built in functions to accomplish this task.

int(item) will send the string value - this is called Passing an Argument to the function - and return the equivalent integer value

float (item) will send the string value - passing the argument to the function - and return the equivalent real number value

## Calculations

Python has many mathematical operators that can be used:

Operator | Function
--- | ---
+ | addition
- | subtraction
* | multiplication
/ | division gives result as a floating point value
// | integer division gives result as an integer value
% | remainder
** | exponent

## Mathematical Operator Precedence

You need to remember the precedence order for your math operators; these are the same that you learned in math classes.

1. exponentiation ** is done first
2. multiplication, division, and remainders *, /, //, % are done second
3. addition and subtraction done third

REMEMBER - you use parenthesis to change this order!!!!

## Math Formulas and Programming Statement

Remember that you have to follow the SYNTAX rules for everything. The normal way we write math formulas needs to be written so that the computer understands the order properly.

## Escape Characters

An escape character is a special character that is preceded with a backslash (\) within a string literal. These allow you to better control your output from your print function.

Escape Sequence | Meaning
--- | ---
\newline | Backslash and newline ignored
\\ | Backslash (\)
\' | Single quote (')
\" | Double quote (")
\a | ASCII Bell (BEL)
\b | ASCII Backspace (BS)
\f | ASCII Formfeed (FF)
\n | ASCII Linefeed (LF)
\r | ASCII Carriage Return (CR)
\t | ASCII Horizontal Tab (TAB)
\v | ASCII Vertical Tab (VT)

## Further Reading

- w3schools.com - Python Syntax
- w3schools.com - Python Comments
- w3schools.com - Python Variables
- w3schools.com - Python Data Types