python expressions and statements and examples of for and while circular exercises


Expressions and statements in Python and for and while loop exercises

  1. expression
 Common expression operators:
x + y, x - y
x * y, x / y, x // y, x % y

 Logical operation:
x or y, x and y, not x

 Membership operation:
x in y, x not in y

 Object instance test:
x is y, x not is y

 Comparison operation:
x < y, x > y, x <= y, x >= y, x == y, x != y

 Operation:
x | y, x & y, x ^ y, x << y, x >> y

1 Operation:
-x, +x, ~x:

 Power operation:
x ** y

 Index and sharding:
x[i], x[i:j], x[i:j:stride]

 Call:
x(...)

 Take attributes:
  x.attribute

 Tuples: (...)
 Sequence: [...]
 Dictionary: {...}

3 Meta selection expression: x if y else z

 Anonymous function: lambda args: expression

 Generator function sending protocol: yield x

  Operation priority:
(...), [...], {...}
s[i], s[i:j]
s.attribute
s(...)
+x, -x, ~x
x ** y
*, /, //, %
+, -
<<, >>
&
^
|
<, <=, >, >=, ==, !=
is, not is
in, not in
not
and
or
lambda
  1. statement:
 Assignment statement

   call
  print :   Print the object
  if/elif/else:  conditional
  for/else:  Sequence iteration
  while/else:  General circulation
  pass:  A placeholder
  break:
  continue
  def
  return
  yield
  global:  The namespace
  raise:  An exception
  import:
  from:  Module property access
  class:  class
  try/except/finally:  The catching
  del:  Delete the reference
  assert:  Debugging check
  with/as:  Environment manager

 Assignment statement:

   Implicit assignment: import, from, def, class, for,  Function parameters

   Tuple and list decomposition assignments: When assigning symbols (=) When the left side is a tuple or list, Python The object on the right and the target on the left are rounded from left to right by position 1 To pair; When the number is different, an exception will be triggered, which can be done in the way of slicing.

   Multiple target assignments

   Strengthen the assignment : +=, -=, *=, /=, //=, %=,

3)for cycle exercises

 practice 1 Chase: 1 Displays the specified dictionary separately d1 All elements in, like the following
k1 v1
k2 v2
...

  >>> d1 = { 'x':1,'y':2,'z':3,'m':4 }
  >>> for (k,v) in d1.items():
  print k,v
  y 2
  x 1
  z 3
  m 4

   practice 2 Chase: 1 Display list l1=["Sun","Mon","Tue","Wed","Thu","Fri","Sat"] Element whose index is odd;

  >>> l1 = ["Sun","Mon","Tue","Wed","Thu","Fri","Sat"]
  >>> for i in range(1,len(l1),2):
  print l1[i]

  Mon
  Wed
  Fri

   practice 3 : will belong to the list l1=["Sun","Mon","Tue","Wed","Thu","Fri","Sat"] But not on the list l2=["Sun","Mon","Tue","Thu","Sat"] Is defined as 1 A new list l3;

  >>> l1 = ["Sun","Mon","Tue","Wed","Thu","Fri","Sat"]
  >>> l2 = ["Sun","Mon","Tue","Thu","Sat"]
  >>> l3 = [ ]
  >>> for i in l1:
  if i not in l2:
l3.append(i)
  >>> l3
  ['Wed', 'Fri']

    practice 4 : Known list namelist=['stu1','stu2','stu3','stu4','stu5','stu6','stu7'] , delete the list removelist=['stu3', 'stu7', 'stu9'] ; Please will belong to removelist Each element in the list is from namelist Remove the ( Belong to removelist But not belonging to namelist Just ignore ) ;

  >>> namelist= ['stu1','stu2','stu3','stu4','stu5','stu6','stu7']
  >>> removelist = ['stu3', 'stu7', 'stu9']
  >>> for i in namelist:
  if i in removelist :
namelist.remove(i)
  >>> namelist
  ['stu1', 'stu2', 'stu4', 'stu5', 'stu6']

4)while cycle exercises

 practice 1 Chase: 1 Displays all elements in the specified list;

  >>> l1 = [1,2,3,4,5]
  >>> i = 0
  >>> while i < len(l1)
  print l1[i]
  i += 1

  1
  2
  3
  4
  5

  >>> l1 = [1,2,3,4,5]
  >>> while l1:
  print l1.pop(0)

  1
  2
  3
  4
  5

 practice 2 : o 100 The sum of all even Numbers within;

  >>> i = 0
  >>> sum = 0
  >>> while i < 101:
  sum += i
  i += 2
print sum

  2550

  >>> for i in range(0,101,2):
  sum+=i
 print sum

  2550

     practice 3 Chase: 1 Displays all keys for the specified dictionary; And indicate the total number of keys at the end of display;

  >>> d1 = {'x':1, 'y':23, 'z': 78}
  >>> i1 = d1.keys()
  >>> while i1:
  print i1.pop(0)
else:
  print len(d1)
  x
  y
  z
  3

     practice 4 : create 1 A contains 100 A list of all odd Numbers within;

  >>> d1 = [ ]
  >>> i = 1
  >>> while i < 101:
  d1.append(i)
  i+=2
  >>> print d1
  [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49, 51, 53, 55, 57, 59, 61, 63, 65, 67, 69, 71, 73, 75, 77, 79, 81, 83, 85, 87, 89, 91, 93, 95, 97, 99]

  >>> d1 = [ ]
  >>> for i in range(1,101,2)
  d1.append(i)
  >>> print d1
  [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49, 51, 53, 55, 57, 59, 61, 63, 65, 67, 69, 71, 73, 75, 77, 79, 81, 83, 85, 87, 89, 91, 93, 95, 97, 99]

 practice 5 List: l1=[0,1,2,3,4,5,6],  The list of l2=["Sun","Mon","Tue","Wed","Thu","Fri","Sat"] To the first 1 The element in the list is the key to the first 2 Elements in a list generate dictionaries for values d1 ;

  >>> l1 = [0,1,2,3,4,5,6]
  >>> l2 = ["Sun","Mon","Tue","Wed","Thu","Fri","Sat"]
  >>> d1 = {}
  >>> count = 0
  >>> if len(l1) == len(l2):
  while count < len(l1):
d1[l1[count]] = l2[count]
count += 1