Input, Processing, and Output
- Typically, computer performs three-step process
- Receive input
- Input: any data that the program receives while it is running
- Perform some process on the input
- Example: mathematical calculation
- Produce output
Statements
Statements in Python are made up of:
- reserved words - words that have pre-defined
meanings in the Java language
- identifiers - words that are created by programmers
for names of variables, functions, classes, etc.
- literals - literal values written in code, like
strings or numbers
- integer literal - an actual integer number written in
code (4, -10, 18)
- float literal - an actual decimal number written in code
(4.5, -12.9, 5.0)
- character literal - a character in single quotes: ('F',
'a', '\n')
- string literal - a string in double quotes: ("Hello",
"Bye", "Wow!\n")
- operators - special symbols that perform certain
actions on their operands
- A unary operator has one operand
- A binary operator has two operands
- A ternary operator has three operands (there's
only one of these)
- Calls to methods (functions)
Displaying Output with the print Function
- Function: piece of prewritten code that performs an operation
- print function: displays output on the screen
- Argument: data given to a function
- Example: data that is printed to screen
- Statements in a program execute in the order that they appear
Variables
A name that
represents a value stored in the computer memory
- Used to access and manipulate data stored in memory
- A variable references the value it represents
- Use assignment statement to create a variable and make it reference data
- General format: variable = expression
- Example: age = 29
- Assignment operator: the equal sign (=)
The structure of a variable:
- Name -- chosen by the programmer (aka identifier)
- Type -- derived from the declaration of the variable
- Size -- determined by the type
- Value -- the data stored in the variable's memory location
Identifiers
Identifiers are the names for things (variables, functions,
etc) in the language. Some identifiers are built-in, and others can
be created by the programmer.
- User-defined identifiers can consist of letters, digits,
underscores, and the dollar-sign $
- Must start with a non-digit
- Identifiers are case sensitive (count and Count
are different variables)
- Reserved words (keywords) cannot be used as
identifiers
- an identifier can be any length
Style-conventions (for indentifiers)
More Python naming conventions
Data Types
There are a small set of what are known as
primitives. These
are basic data types that are determined for you by the interpreter.
-
boolean
- has two possible values, True or False
- integer (int) types - for storage of integer values
- floating (float) point types - for storage of decimal numbers (i.e.
a fractional part after the decimal)
- string (str) type used to store strings in memory.
- list - are also refered to as arrays that can contain mixed datatypes.
- object - user defined types.
- Numeric literal determine the type: number written in a program
with no decimal point considered integer, otherwise, considered float.
- Some operations behave differently depending on data type
- A variable in Python can refer to items of any type
More on Python types.
- Printing variables: Python allows one to display multiple items with a single call to print
- Items are separated by commas when passed as arguments
- Arguments displayed in the order they are passed to the function
- Items are automatically separated by a space when displayed on screen
Strings and String Literals
- String: sequence of characters that is used as data
- String literal: string that appears in actual code of a program
- Must be enclosed in single (') or double (") quote marks
- String literal can be enclosed in triple quotes (' ' ' or " " ")
- Enclosed string can contain both single and double quotes and can have multiple lines
Comments
- Comments: notes of explanation within a program
- Ignored by Python interpreter, its intended for a person reading the program's code
- Begin with a # character
# This is a comment.
- End-line comment: appears at the end of a line of code
- Typically explains the purpose of that line
item = 100 # This is an end-line comment.
Operators