Python - Week 3 - Lists, Tuples, Slicing, Math Functions

This week I tried another problem from github:

Write a program that calculates and prints the value according to the given formula:
Q = Square root of [(2 * C * D)/H]

Following are the fixed values of C and H:
C is 50. H is 30.
D is the variable whose values should be input to your program in a comma-separated sequence.

Example
Let us assume the following comma separated input sequence is given to the program:
100,150,180
The output of the program should be:
18,22,24


Some interesting findings from this program:

  1. The import in Python is done using 'from ... import ..' statement. This is used in our program to use round and sqrt functions.
  2. Tuple is an immutable sequence type. Functionally, they’re used to represent fixed collections of items.
  3. print function takes an argument which by default is '\n'. In our case, we are using a different end character ','
  4. Slicing is a way to extract an entire section (slice) in a single step.
  5. printSeries[0:-1] - is used here to slice the list starting from index 0 until one but last item. printSeries[-1] is the last item in list.

Comments

Popular posts from this blog

Python - Week 8 - Dictionaries, Tuples, Sorting and Filtering

Python - Week 5 - Comprehensions