Sunteți pe pagina 1din 4

Lab 02: Python for Programing: Variables and Operations

2.0 Introduction (Brief Understanding on Python terms)

I. Modules: consist of a set of Python commands. Sometimes modules is known as Python


libraries. e.g import module
II.In Python we have Statement and Expression only
III. Statement: No return value but perform some task in the system.
IV.Expression: Just return value on Python space, but value altered.
V. Whitespace are considered as: space and in horizontal space and return(next line of
statement/expression) and line feed/ form feed in vertical space.
VI.Indentation: Leading white space is an integral part python programing construct when come
control and function development (we will see later chapter)
VII. Continuation: Long statement or expression in a single line can separated using (\)
VIII. Comments: All comment are written with prefix (anything following) a # sign

2.1 Special Python Elements (tokens)

I. Keywords: special terms in Python than cannot be used for naming variable objects
II. Operators: special tokens or elements (sequence of characters) with special mathematical or
logical meaning to the Python Interpreter.
III. Delimiters: Used in different way for statement and expression.

2.2 Naming Python Objects

I. Must start with a letter or underscore (e.g. myX, _Xmy)


II. Do NOT start with a number
III. If it is multiple word, it can be combine using underscore characters _ (e.g. my_birth)
IV. After the first character, the object or variable name can proceed with numbers or underscore.
V. Do not name object with same name as keywords.
VI. Do not have delimiters, operators in naming Python objects
VII. Can be any length but must make a bit of sense
VIII. Uppercase and Lowercase are are different in representation (e.g. myname is different to
myName)
Python Shell
>>> import math
>>> import random # this is python module or library
>>> x = 10
>>> x
10
>>> y = x + 3 # this is called statements
>>> y # this is expression - you expect by to display value
13
>>> z = x+2
>>> z
12
>>> z = 4 + 5
>>> z
9
>>> z = 4 + y # white space are ignored
>>> z
17
>>> if x < 0:
... print y # you expect an error if you do no indent
File "<stdin>", line 2
print y # you expect an error if you do no indent
^
IndentationError: expected an indented block
>>> if x < 0:
... print z # now it is ok
... else:
... print y # indent again
...
13
NOTE: Indentation is a main strength of python programming and MUST be applied for control and
function construct.
>>> print " Helllo I am tooooo long \
... I can go to second line "
Helllo I am tooooo long I can go to second line

NOTE: Comments are import as doing a program, it serve as precursor to program documentation
>>> # Ignored by python interpreter: This python make be boring, I prefer cobra
... #
>>> X = 10 # ok that ok
>>> X2 = 20 # second value
>>> x_2 = 25 # underscore is accepted
>>> 2x = 22 # Can not start with a number
File "<stdin>", line 1
2x = 22 # Can not start with a number
^
SyntaxError: invalid syntax
>>> x-2 = 23 # This is another no no only underscore (as special character) is allowed
File "<stdin>", line 1
SyntaxError: can't assign to operator
>>> x!2 = 23 # This is another no no only underscore (as special character) is allowed
File "<stdin>", line 1
x!2 = 23 # This is another no no only underscore (as special character) is allowed
^
SyntaxError: invalid syntax
2.3 Variable Creation (with Assignment)
I. Variable is created as it is assign in a statement.
II. Creation can be in form of mathematical assignment as similar

2.4 Variable Types and Constructing New Variables


I. In Python there are few type of variable types
II. These are Python variable types are:
Integer, Float, Complex Numbers, Boolean, String, List, Dictionary and Sets
III. One of Python easy implementation is on constructing new variable types using
constructor. The constructor is made of (int, float and str) which can change to new variable
type.

2.5 Operators

2+4 Addition
3 -1 Subtraction
2*3 Multiplication
5/2 Quotient – integer
5 / 2.0 Division – floating point
5%2 Remainder – only apply to integer format
** Exponentiation

2.6 Oder of Precedence


2+4- 2 Left to Right
5 / 2+ 2 Same precedence (left to right) only for integer
3+ 2* 3 Multiplication given precedence then left to right
(3+2)* 3+2 Parentheses given priority
2 + 3*2 + 5**2 Exponents given priority then multiplication
-4+2 Negation given first priority

2.7 Short Cut Operators

ValInt += 2 ValInt = Val +2


ValInt -= 5 ValInt = Val -5
ValInt /= 3 ValInt = Val /3
ValInt *= 4 ValInt = Val *4

White-space in form return is Python end of statement or expression (no semi-colons)


Python Shells
>>> 4-2+4
6
>>> 4/3
1
>>> 4/3.0
1.3333333333333333
>>> 5/2+3+4%3
6
>>> (3+6)*3-(5%2)
26
>>> -5+3*2
1
>>> x = 10
>>> y = 20
>>> z = 30
>>> x *= 10
>>> x
100
>>> y %=3
>>> y
2
>>> z /=3
>>> z
10

2.8 Moving Forward


A. Control Statements
#!/usr/bin/python
x = 10
y = 20
if x < 0:
print x # indent
else:
print y # indent

B. Quick on Function
#!/usr/bin/python
def findX(t):
x = 3**t
return x
y = findX(4)
print y

S-ar putea să vă placă și