-
and
- all conditions in a boolean expression must be met
- ex:
input:
output:
-
del
- output:
- [1, 2, 3, 4]
- [3, 4]
-
-
while
- controlling the flow of the program
- ex:
- input:
- numbers = [22, 34, 12, 32, 4]
- sum = 0
i = len(numbers)
- while (i != 0):
- i -= 1
- sum = sum + numbers[i]
print "The sum is: ", sum
-
continue
- used to interrupt the current cycle, without jumping out of the whole cycle.
- New cycle will begin.
- ex:
num = 0
- while (num < 10):
- num = num + 1
- if (num % 2) == 0:
- continue
- print num,
-
if
- used to determine, which statements are going to be executed.
- ex:
- if age > 18:
- print "Driving licence issued"
- else:
- print "Driving licence not permitted"
- output:
- Driving licence not permitted
-
elif
- stands for else if.If the first test evaluates to False,
- then it continues with the next one
- ex:
input:
output:
-
else
- is optional. The statement after the else keyword is executed,
- unless the condition is True
- ex:
input:
output:
-
is
- tests for object identity
- ex:
input:
output:
-
not
- negates a boolean value
- ex:
input:
grades = ["A", "B", "C", "D", "E", "F"]
grade = "L"
- if grade not in grades:
- print "unknown grade"
- output:
- unknown grade
-
and
- all conditions in a boolean expression must be met
- ex:
- if age < 55 and sex == "M":
- print "a young male"
-
or
at least one condition must be met.
ex:
- if ( name == "Robert" or name == "Frank" or name == "Jack"
- or name == "George" or name == "Luke"):
- print "This is a male"
-
import
- import other modules into a Python script
- ex:
- print math.pi
- output:
- 3.14159265359
-
as
- if we want to give a module a different alias
- ex:
- input:
- import random as rnd
- for i in range(10):
- print rnd.randint(1, 10),
- output:
- 1 2 5 10 10 8 2 9 7 2
-
from
- for importing a specific variable, class or a function from a module
- ex:
input:
from sys import version
- print version
- output:
- 2.7.6 (default, Nov 10 2013, 19:24:18) [MSC v.1500 32 bit (Intel)]
-
def
- used to create a new user defined function
- ex:
- input:
- def root(x):
- return x * x
-
return
- exits the function and returns a value
- ex:
- input:
- def root(x):
- return x * x
-
lambda
- creates a new anonymous function
- ex:
- input:
- for i in (1, 2, 3, 4, 5):
- a = lambda x: x * x
- print a(i),
-
global
- access variables defined outside functions
- ex:
input:
x = 15
- def function():
- global x
- x = 45
- function()
- print x
- output:
- 45
-
try
- specifies exception handlers
- ex:
- input:
- try:
- f = open('films', 'r')
- for i in f:
- print i,
- except IOError:
- print "Error reading file"
- finally:
- if f:
- f.close()
- output:
-
except
- catches the exception and executes codes
- ex:
input:
output:
-
finally
- is always executed in the end. Used to clean up resources.
- ex:
input:
output:
-
raise
- create a user defined exception
- ex:
- input:
- class YesNoException(Exception):
- def __init__(self):
- print 'Invalid value'
answer = 'y'
- if (answer != 'yes' and answer != 'no'):
- raise YesNoException
- else:
- print 'Correct value'
- output:
- Invalid value
- Traceback (most recent call last):
- File "./userexception.py", line 13, in <module>
- raise YesNoException
- __m ain__.YesNoException
-
assert
- used for debugging purposes
- ex:
- input:
- salary = 3500
- salary -= 3560 # a mistake was done
- assert salary > 0
- output:
- Traceback (most recent call last):
- File "./salary.py", line 9, in <module>
- assert salary > 0
- AssertionError
-
class
- used to create new user defined objects
- ex:
- input:
- class Square:
- def __init__(self, x):
- self.a = x
- def area(self):
- print self.a * self.a
- sq = Square(12)
- sq.area()
- output:
- 144
-
exec
- executes Python code dynamically
- ex:
- input:
- exec("for i in [1, 2, 3, 4, 5]: print i,")
- output:
- 1 2 3 4 5
-
print
- print to console
- ex:
- input:
- print 'you suck'
- output:
- you suck
-
break
- is used to interrupt the cycle, if needed
- example:
- input:
- import random
- while (True):
- val = random.randint(1, 30)
- print val,
- if (val == 22):
- break
- output:
- 15 9 7 23 14 3 11 11 28 4 10 19 19 12 27 4 27 22
-
for
- used to iterate over items of a collection in order that they appear in the container.
- ex:
- input:
- lyrics = """
- Are you really here or am I dreaming
- """
- for i in lyrics:
- print i,
- output:
- A r e y o u r e a l l y h e r e o r a m I d r e a m i n g
-
in
- ex:
- print 4 in (2, 3, 5, 6)
- for i in range(25):
- print i,
|
|