Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Faiha HW #3
# python 3.5.2 # https://1drv.ms/f/s!ArsHV2FdZgQ1iQOclKKq2kqbxLdg # Loops - For Loops x = [1, 2, 7, 9, 10, 3, 14] y = [] # x and y are lists # Exercise 1 # ------------- # 1. Create a list of numbers. 2. Compute the squares for each number in the list. 3. Then print the list. q = [] m = [] for each_n in range(1, 20, 3): q.append(each_n) # my list of numbers m.append(each_n*each_n) # my list of numbers squared #print ("q = ", q) #print ("m = ", m) # Exercise 2 # ------------- # Create a list of 20 numbers - [1,20]. If the number is even, print "Even". If the number is odd, print "Odd". # [modulo operation] if number%2 == 0: then even, elif number%2 == 1, then odd. for each_i in range (20): if each_i%2 == 0: pass #print ( each_i, 'is even.') elif each_i%2 == 1: pass #print ( each_i, 'is odd.') # Exercise 3 # ------------- # while condition is true: # keep doing something # (have something that will change the condition later) count = 0 while count < 5: #print (count) count = count + 1 # count increased its value by 1 # Date: Oct 11, 2018, 9:15 pm # Exercise 4 # --------------- # Use a while/for loop to print this diagram. You will need to use if conditions also. # * # ** # *** # **** a = 'One' b = 'Two' #print (a+b) #print (a*3) #for i in range(1,5,1): #print (i, '*'*i) # Exercise 5 # ---------------- # Convert the above expression (for-loop) to while loop #a=1 #while a<5: # print (a,'*'*a) # a = a+1 # Exercise 6 # -------------- # 7-shaped diamond # * 3 blanks, 1 star i = 0*2+1 -> 1 # *** 2 blanks, 3 stars i = 1*2+1 -> 3 # ***** 1 blank, 5 stars i = 2*2+1 -> 5 # ******* 0 blank, 7 stars i = 3*2+1 -> 7 # ***** 1 blank, 5 stars i = 4 -> 3*2-1 -> 5 # *** 2 blank, 3 stars i = 5 -> 2*2-1 -> 3 # * 3 blanks, 1 star i = 6 -> 1*2-1 -> 1 # hint: # 5-shaped diamond # * # *** # ***** # *** # * # Exercise 7 # -------------- # N-shaped diamond #for N in range(3,22,2): #for i in range(N): # if i <= N//2: # print (i, ' '*((N//2)-i) + '*'*(i*2+1)) # elif i >= (N//2)+1: # print (i, ' '*(i-(N//2)) + '*'*((N-i)*2-1)) # print ('') # Homework: # 1. Convert the above expression to while loop - more important i=0 while i<7: i=i+1 if i <= 3: print (i, ' '*((7)-i) + '*'*(i*2-1)) if i >= 4: print (i, ' '*(i-1) + '*'*((7-i)*2+1)) # 2. By changing initial condition (using while loop) - do the second part after n=3 N=53 while n<N: n=n+2 i=0 while i<n: i=i+1 if i <= n//2: print (i, ' '*((n//2)-i) + '*'*(i*2-1)) if i >= (n//2)+1: print (i, ' '*(i-(n//2)) + '*'*((n-i)*2-1))
run
|
edit
|
history
|
help
0
sendi
Python3 - PDF PAGES SEPARATION
metods.py
5
abstractmethod 2
Никита
Считалка через массив
Fig
Assignment-2a
public,protec,pvt