https://tinyurl.com/Satyam-tutorial-03

Topics:

  1. Loops
  2. Functions

Pattern Questions¶

Hollow Square:
****
*  *
*  *
****
In [ ]:
n = 4

for row in range(n):
    for col in range(n):
        if row == 0 or row == n - 1:    # top or bottom row
            print("*", end="")
        else:                           # middle rows
            if col == 0 or col == n - 1:
                print("*", end="")
            else:
                print(" ", end="")
    print()
1.  *
    **
    ***
    ****
    *****
    ****
    ***
    **
    *

Hint:
    if (row_number > N):
       col = 2*N - row
    else:
       col = row
In [ ]:
n = 5
for row in range(2*n):
  col_number = 2*n - row if row>n else row
  for col in range(col_number):
    print("*", end="")
  print()
*
**
***
****
*****
****
***
**
*
2.       *
        * *
       * * *
      * * * *
     * * * * *
      * * * *
       * * *
        * *
         *


Hint: number_spaces = n - col_number
In [ ]:
n = 5
for row in range(2*n):
  col_number = 2*n - row if row>n else row
  number_of_spaces = n - col_number
  for spaces in range(number_of_spaces):
    print(" ", end="")
  for col in range(col_number):
    print("*", end=" ")
  print()
     
    * 
   * * 
  * * * 
 * * * * 
* * * * * 
 * * * * 
  * * * 
   * * 
    * 
Homework Question

3.          1
          2 1 2
        3 2 1 2 3
      4 3 2 1 2 3 4
    5 4 3 2 1 2 3 4 5

Hint: Try not to use zero index, when we have numbers
In [ ]:
n = 5
for row in range(1,n+1):
  for space in range(n-row):
    print(" ", end="")
  for col in range(row,0,-1):
    print(col, end="")
  for col in range(2,row+1):
    print(col, end="")
  print()
    1
   212
  32123
 4321234
543212345

More Loop Questions (Create all this in form of a function)¶

  1. Create a function that returns sum of integer numbers from 1 to n using a while loop.
In [ ]:
def sum_upto_n(n):
  sum = 0
  i = 0
  while i < n:
    sum += i
    i += 1
  return sum

sum_upto_n(10)
Out[ ]:
45
  1. Create a function that counts the number of vowels in a string (as an input) using a for loop.
In [ ]:
def count_vowels(s):
  vowels = "AEIOUaeiou"
  count = 0
  for c in s:
    if c in vowels:
      count += 1
  return count

count_vowels("Satyam Tiwari")
Out[ ]:
5
  1. Find the largest number in a list using a for loop
In [ ]:
def largest_number(l):
  maximum_number = l[0] # Assume first number is the smallest
  for n in l:
    if n > maximum_number:
      maximum_number = n
  return maximum_number

largest_number([4, 8, -5, 50, 6])
Out[ ]:
50
  1. Print numbers from 1 to n, except those divisble by 3 using a for loop and continue statement
In [ ]:
def print_numbers(n):
  for i in range(1,n+1):
    if i%3==0:
      continue
    if i==n:
      print(i)
      return
    print(i, end=",")

print_numbers(10)
1,2,4,5,7,8,10
  1. Print numbers in a list until a negative number is encountered using a while loop
In [ ]:
def print_upto_negative(l):
  for i in l:
    if i<0:
      return
    print(i, end=",")

print_upto_negative([4,5,6,8,5,-5,6])
4,5,6,8,5,
  1. Find the common elements in two lists using a for loop
In [ ]:
def common(l1, l2):
  common_elements = []
  for i in l1:
    if i in l2:
      common_elements.append(i)
  return common_elements

common([1,2,3,4,5],[1,2,3])
Out[ ]:
[1, 2, 3]
  1. Write a Python function to count the number of even and odd numbers in a list of numbers as input.
In [ ]:
def counting(l):
  n_e = 0
  n_o = 0
  for i in l:
    if i%2==0:
      n_e += 1
    else:
      n_o += 1
  return n_e, n_o

counting([1,2,5,3,5,6,5,0])
Out[ ]:
(3, 5)
In [ ]: