Week2

Week 2

Variables

del var1, var2

Types

  • String

  • Lists: in/append/len/remove/sort/split/max/min/sum/pop

  • Tuples: read-only lists/comma

  • Dictotionaries

Loops

for/while

else in for/while loop

break/continue

break: stop all iretations continue: skip rest and start next iretation

if-else/if-elif-else

Functions

Plotting

matplotlib: python plot package pyplot: a convenient interface to the matplotlib

plt.plot(color,linewidth,linestyle etc.) plt.xlim plt.xticks plt.yticks plt.ylim plt.show()

Exercise

1-Write a Python program to count the number of strings where the string length is 2 or more and the first and last character are same from a given list of strings.

2-Write a Python program to multiplies all the items in a list.

3-Write a Python program to get a list, sorted in increasing order by the last element in each tuple from a given list of non-empty tuples. Sample List : [(0, 5), (-1, 2), (4, 4), (2, 3), (3, 1)]

4-Write a Python program to print a specified list after removing the 0th, 4th and 5th elements.

5-Write a Python program which accepts a sequence of comma separated 4 digit binary numbers as its input and print the numbers that are divisible by 5 in a comma separated sequence.

6-Write a Python program to get the Fibonacci series between 0 to 50.

Note : The Fibonacci Sequence is the series of numbers : 0, 1, 1, 2, 3, 5, 8, 13, 21, .... Every next number is found by adding up the two numbers before it.

7-Write a Python program to check the validity of password input by users. Validation :

Note : PLEASE IMPORT package re and use function search • At least 1 letter between [a-z] and 1 letter between [A-Z]. • At least 1 number between [0-9]. • At least 1 character from [$#@]. • Minimum length 6 characters. • Maximum length 16 characters.

8-Write a (recursive) function which calculates the factorial of a given number. Use exception handling to raise an appropriate exception if the input parameter is not a positive integer, but allow the user to enter floats as long as they are whole numbers.

9-Make a program that plots the function g(y)=(e^−y)sin(4y) for y∈[0,4] using a red solid line.

Use 500 intervals for evaluating points in [0,4]. Store all coordinates and values in arrays. Set labels on the axis and use a title “Damped sine wave”. [HINT : from numpy import exp and sin both]

10-Add a to the above (two plots), a black dashed curve for the function h(y)=(e^−1.5y)sin(4y). Include a legend for each curve.

Last updated

Was this helpful?