python keywords

Card Set Information

Author:
etapia001
ID:
254755
Filename:
python keywords
Updated:
2014-01-02 02:41:27
Tags:
python keywords
Folders:

Description:
python keywords
Show Answers:

Home > Flashcards > Print Preview

The flashcards below were created by user etapia001 on FreezingBlue Flashcards. What would you like to do?


  1. and
    • all conditions in a boolean expression must be met
    • ex:

    input:


    output:
  2. del
    • deletes objects
    • ex:

    • input:
    • a = [1, 2, 3, 4]

    • print a
    • del a[:2]
    • print a

    • output:
    • [1, 2, 3, 4]
    • [3, 4]
  3. pass
    • does nothing
    • ex:

    • def function():
    • pass
  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

    • output:
    • The sum is: 104
  5. continue
    • used to interrupt the current cycle, without jumping out of the whole cycle.
    • New cycle will begin.
    • ex:

    • input:
    • import random

    num = 0

    • while (num < 10):
    •     num = num + 1
    •     if (num % 2) == 0:
    •         continue
    •     print num,

    • output:
    • 1 3 5 7 9
  6. if
    • used to determine, which statements are going to be executed.
    • ex:

    • input:
    • age = 17

    • if age > 18:
    •     print "Driving licence issued"
    • else:
    •     print "Driving licence not permitted"

    • output:
    • Driving licence not permitted
  7. elif
    • stands for else if.If the first test evaluates to False,
    • then it continues with the next one
    • ex:

    input:

    output:
  8. else
    • is optional. The statement after the else keyword is executed,
    • unless the condition is True
    • ex:

    input:

    output:
  9. is
    • tests for object identity
    • ex:

    input:

    output:
  10. 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
  11. and
    • all conditions in a boolean expression must be met
    • ex:

    • input:
    • sex = "M"
    • age = 26

    • if age < 55 and sex == "M":
    •     print "a young male"

    • output:
    • a young male
  12. or
    at least one condition must be met.

    ex:

    • input:
    • name = "Jack"

    • if ( name == "Robert" or name == "Frank" or name == "Jack"
    • or name == "George" or name == "Luke"):
    •     print "This is a male"

    • output:
    • This is male
  13. import
    • import other modules into a Python script
    • ex:

    • input:
    • import math

    • print math.pi
    • output:
    • 3.14159265359
  14. 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
  15. 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)]
  16. def
    • used to create a new user defined function
    • ex:

    • input:
    • def root(x):
    • return x * x

    • a = root(2)
    • b = root(15)

    • print a, b
    • output:
    • 4 225
  17. return
    • exits the function and returns a value
    • ex:

    • input:
    • def root(x):
    • return x * x

    • a = root(2)
    • b = root(15)

    • print a, b
    • output:
    • 4 225
  18. lambda
    • creates a new anonymous function
    • ex:

    • input:
    • for i in (1, 2, 3, 4, 5):
    • a = lambda x: x * x
    • print a(i),

    • output:
    • 1 4 9 16 25
  19. global
    • access variables defined outside functions
    • ex:

    input:

    x = 15

    • def function():
    •     global x
    •     x = 45

    • function()
    •     print x
    • output:
    • 45
  20. 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:
  21. except
    • catches the exception and executes codes
    • ex:

    input:

    output:
  22. finally
    • is always executed in the end. Used to clean up resources.
    • ex:

    input:

    output:
  23. 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
  24. 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
  25. 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
  26. exec
    • executes Python code dynamically
    • ex:

    • input:
    • exec("for i in [1, 2, 3, 4, 5]: print i,")
    • output:
    • 1 2 3 4 5
  27. print
    • print to console
    • ex:
    • input:
    • print 'you suck'
    • output:
    • you suck
  28. 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
  29. 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
  30. in
    • ex:
    • print 4 in (2, 3, 5, 6)

    • for i in range(25):
    •     print i,

What would you like to do?

Home > Flashcards > Print Preview