Session 2
Session 2
• Input function
• Returns a string (always)
• Can be used to prompt for user input
Reserved Words
False def if raise
None del import return
True elif in try
and else is while
as except lambda with
assert finally nonlocal yield
break for not
class form or
continue global pass
Built-in function names are also reserved. E.g print, input etc.
Variables
•What are variables
•Variables created when value assigned; no declaration
•Variables can change types through a new assignment
•The type of a variable can be forced by casting the
RHS
•The simple assignment operator (=)
Variable Naming Conventions
•A variable name must start with a letter or the underscore character
•A variable name cannot start with a number
•A variable name can only contain alpha-numeric characters and
underscores
•(A-z, 0-9, and _ )
•Variable names are case-sensitive (age, Age and AGE are three different
variables)
•A variable name cannot be any of the Python keywords.
•Many values can be assigned to multiple variables: (bad idea)
• x, y, z = 10, "Banana", "Cherry"
Variable Data Types
• str
• int, float, complex
• list, tuple, dict
• set, frozenset
• bool
• bytes, bytearray, memoryview
• NoneType
Operators
•Arithmetic operators
•Assignment operators
•Comparison operators
•Logical operators
•Identity operators
•Membership operators
•Bitwise operators
Arithmetic operators
Addition +
Subtraction -
Multiplication *
Division /
Floor Division //
Modulus %
Exponentiation **
Assignment operators
=
+=
-=
*=
/=
//=
%=
**=
Expressions
• A combination of operators and operands that is
interpreted to produce another value
• E.g.
c = a+b
Comparison operators
• Equals: a == b
• Not Equals: a != b
ax² + bx + c = 0 a≠0
• Roots are
x = [-b±√(b2-4ac)]/2a