Module 1 - Nurturing Session

print("one word") # bu default adds new line at end of print
print("second line")
one word
second line
text = "line one\nsecond line"
print(text)
line one
second line
for i in range(5):
    print(i) # every item will be printed on new line
0
1
2
3
4
for i in range(5):
    print(i, end=",") 
0,1,2,3,4,
name = "vikrant"
surname = "patil"
print(name , surname) # default seperator between the arguments is space
vikrant patil
print(name , surname, sep=",")
vikrant,patil
print(name , surname, sep="-")
vikrant-patil
print("print this", "and this", "also this third argument")
print this and this also this third argument

function return statement

%%file rev.py
import sys

def rev(n):
    b = []
    pass
    return b


n = int(sys.argv[1])
for i in rev(n):
    print(i)
Writing rev.py

import statement

import random
random.random()
0.4619361199579366
import sys
sys.argv # this is a variable that takes command line arguments from cmd and stares in this variables
['/opt/tljh/user/lib/python3.10/site-packages/ipykernel_launcher.py',
 '-f',
 '/home/jupyter-pipal/.local/share/jupyter/runtime/kernel-a8ab5199-2684-4627-843a-8eb1ba882b97.json']
%%file foo.py
import sys

print(sys.argv)
Writing foo.py
!python foo.py argument1 argument2 dkjsfhsdjk kdjfhjkasd 1 2 3
['foo.py', 'argument1', 'argument2', 'dkjsfhsdjk', 'kdjfhjkasd', '1', '2', '3']
!python foo.py 4
['foo.py', '4']
%%file printargs.py
import sys

for arg in sys.argv:
    print(arg)
Writing printargs.py
!python printargs.py hello let me test this script 1 2 3 4
printargs.py
hello
let
me
test
this
script
1
2
3
4
%%file printfirstarg.py
import sys

print(sys.argv[1])
Writing printfirstarg.py
!python printfirstarg.py 5
5
!python printfirstarg.py 10
10

Problems

%load_problem flatten
Problem: Flatten the list

Write a function flatten which combines nested lists into a single list. It takes a list of lists as an argument and returns a list of all elements from nested lists.

>>> flatten([[1,2,3],[4,5],[6,7,8,9]])
[1,2,3,4,5,6,7,8,9]

You can verify your solution using:

%verify_problem flatten

# your code here

def flatten(nestedlist):
    # your code
    return result
ones = [1, 1, 1]
twos = [2, 2, 2]
ones + twos
[1, 1, 1, 2, 2, 2]
ones.extend(twos) # this will modify the list in place
ones
[1, 1, 1, 2, 2, 2]
flattened_list = []
for eachlist in nested_list:
    flattened_list
%load_problem text-in-a-box
Problem: Text in a Box

Write a program box.py that takes word as a command-line argument and prints the word in a box as shown below.

$ python box.py python
+--------+
| python |
+--------+

Please note that there should be exactly one space on either side of the text in the box.

You can verify your solution using:

%verify_problem text-in-a-box

%%file box.py
# your code here

# how will you get the word? -> sys.argv[1]
# how many dashes?  len(word) + 2
# 
"-"*8
'--------'
print("+" + "-"*8 + "+")
+--------+
print("+","-"*8,"+", sep="")
+--------+
%load_problem unique
Problem: Unique Items

Write a function unique which will remove duplicates from a list and maintain the order of items as in original list.

>>> unique([1, 1, 2, 3, 1, 2, 3, 2, 4])
[1, 2, 3, 4]

You can verify your solution using:

%verify_problem unique

# your code here


# for loop on the list
#     if the item (loop variable ) is not in the results then add it

u = []

%load_problem mean
Problem: Mean

Write a function mean to compute the arthematic mean of a list of numbers.

The arthemetic mean of N numbers is computed by adding all the N numbers and dividing the total by N.

The function takes the list of numbers as argument and returns the mean.

>>> mean([1, 2, 3, 4, 5])
3.0

You can verify your solution using:

%verify_problem mean

# your code here


%load_problem evens
Problem: Evens

Write a function evens that takes a list of numbers as argument and returns a new list containing only the even numbers out of it.

>>> evens([1, 2, 3, 4, 5, 6])
[2, 4, 6]

You can verify your solution using:

%verify_problem evens

# your code here


r = []
for item in givenlist:
    if cond on item:
       r.append(do somthing with item)
5 % 2 #this means when divided by 2 remainder is 1
1
def is_even(x):
    return x%2==0
is_even(4)
True
if is_even(4):
    print("even")
else:
    print("odd")
even
%load_problem countdown
Problem: Countdown

Write a program countdown.py to print numbers from n to 1.

The program should take the number n as command-line argument.

$ python countdown.py 5
5
4
3
2
1

You can verify your solution using:

%verify_problem countdown

%%file countdown.py
# your code here


nums = list(range(1, 6))
nums
[1, 2, 3, 4, 5]
nums[::-1] # reverse the list
[5, 4, 3, 2, 1]
nums
[1, 2, 3, 4, 5]
nums.reverse()
nums # reversed in place
[5, 4, 3, 2, 1]
%load_problem numeric-value
Problem: Numeric Value

In financial terms a negative balance is represented with round barackets around the number instead of - sign. Write a function numeric_value which returns actual numeric value. For example a value "(1234)" should get -1234 as numeric value. while "34" will get value as 34

>>> numeric_value("(1234)")
-1234
>>> numeric_value("42")
42

You can verify your solution using:

%verify_problem numeric-value

# your code here


int("4354")
4354
"(3434)".startswith("(")
True
"(3434)".replace("(", "-")
'-3434)'
%load_problem reverse-words
Problem: Reverse Words

Write a function reverse_words that takes a sentence and returns a new sentence with all the words in the reserse order.

>>> reverse_words("joy of programming")
'programming of joy'

>>> reverse_words("less is more")
'more is less'

>>> reverse_words("road goes ever on and on")
'on and on ever goes road'

Please note that only the order of the words in the sentence is reversed, not the letters in each word.

You can verify your solution using:

%verify_problem reverse-words

# your code here


# from string -> words
# reverse the words list
# join the reversed words list