(A) a list of formula
Exercise 1: write function called collatz (number); the functions implemented: the argument is an even number, the print number // 2; parameter is an odd number, the print 3 * number + 1
Resolution:
number = int(raw_input("please input integer:"))
print number // 2 if number % 2 == 0 else number * 3 +1
Exercise Two: use a for loop, to make changes to the type of list elements
Resolution:
s = '51 5000 10000'
k, a, b = [int (i) for i in s.split ( "")] ## dividing spaces
print a,b,c
Exercise three: find all the even numbered between 1 and 10, and returns a list (this contains an even number as the radius of the area of a circle)
Find all odd numbers between 1 and 10, and returns a list (converted to a string odd)
Resolution:
import math
li = [math.pi * r*r for r in range(2,11,2)]
li1 = [str(i) for i in range(1,10,2)]
print li, li1
Exercise Four: to find all the prime numbers between 1 and 100
Analysis: First, a definition of a function of the determined quality; return 0 for primes not a prime number, return represents
def isPrime(num):
for i in range(2,num):
if number % i == 0:
return 0
else:
return 1
li = [i for i in range(1,101) of isPrime(i)]
print it
(B) dictionary formula
Exercise 1: Suppose there are 20 students in grades between 60-100 students in selected grades of more than 90 points
Analysis: randomly generated and student achievement between 60-100; then select the output generated in the dictionary
import random
stuInfo = {'student' + str(i):random.randint(60,100) for i in range(20)}
print {name:score for name,score in stuInfo.items() if score > 90}
Exercise Two: The key value and the value of the exchange value of the dictionary
Resolution:
d = {'a':1,'b':2,'c':3}
print {v:k for k,v in d.items()}
print {k:k.upper() for k,v in d.items()}
Exercise three: the case of the merger, Key value eventually all lowercase
Resolution:
d = {'a':1,'b':2,'c':3,'A':5}
print {k.lower():d.get(k.upper(),0)+d.get(k.lower(),0) for k,v in d.items()}