Lecture 2 - Python Functions and Working with Files

1 Like

Hello,
No assignment for this week. The 2nd assignment will be up on 29th if I am not mistaken.

1 Like

but it’s not working.actually i am trying this from 1 week pls someone help me out

You can try the practice question in lecture 2 and try to make it as comprehensive as possible.

Yes everytime you run the file or a Notebook you need to import the module once

I’ve done the practice question and would like to see how others have done it! Made a forum thread: ZerotoPandas Lesson 2: Practice Question (Writing Reusable Code Using Functions)

The so-called CSV (Comma Separated Values) format is the most common import and export format for spreadsheets and databases.

more reference https://docs.python.org/3/library/csv.html

yeah it did i was trying it with forward slashes

thank you

Yes, Indentation is an integral part of Python coding. based on the indentation only the python interpreter understands how to execute. unlike in other programming languages where ‘;’ or other syntax is used, python uses indentation.

How to declare a multiple line comment in Python?

Yeah! you can create it in this way.

numbers = [1, 2, 3]
letters = [‘a’, ‘b’, ‘c’]

zip_1 = zip(numbers, letters)

dict_1 = dict(zip_1)

print(dict_1)

You can declare multiline comment with triple quotes.

ex:

“”"
Lists
Tuples
Sets
“”"

or

‘’’
Lists
Tuples
Sets
‘’’

3 Likes

Either convert the lists into dicts, or the dict into a list, then combine them and iterate over the whole thing. For example, turning the dict into a list and then iterating over the combination of all 4 lists:

l1 = [ 'a', 'b', 'c' ]
l2 = [ 1, 2, 3 ]
l3 = [ 3.14159, None, 0 ]
d1 = { 'name': "Fred", 'age': 99 }
for i in l1 + l2 + l3 + list(d1):
     print(i)

gets me:

a
b
c
1
2
3
3.14159
None
0
name
age

If you want to enter code samples here in the discussion, you can put <pre> before the code lines, and </pre>. The lines between those are in a fixed width font and will have indentation displayed correctly.

2 Likes

Yes module, package, library… all is similar

this is the code for the practice question in lecture 2
the code will describe , on visiting which city would cost least amount for 4,7,10,14 days…
i tried my best to shorten the code…
if there is any another way to shorten the code further plz let me know…

CODE :

 vacation = [{'city':'paris','flight':200,'hotel_per_day':200,'car_rent_week':200},
            {'city':'London', 'flight':250,'hotel_per_day':30,'car_rent_week':120},
            {'city':'dubai','flight':370,'hotel_per_day':15,'car_rent_week':80},
            {'city':'mumbai','flight':450,'hotel_per_day':10,'car_rent_week':70} ]
def trip(vacation,i):
    vacation_cost = dict() 
    for item in vacation :
        if i is 10 or 14 :
            value = item['flight'] + i*item['hotel_per_day'] + 2*item['car_rent_week']
        else :
            value = item['flight'] + i*item['hotel_per_day'] + item['car_rent_week']
        vacation_cost[item['city']] = value
    return vacation_cost 

def evaluation(dictonary,min_city,min_amount,days) :
    for key ,value in zip(dictonary.keys(),dictonary.values()) :
        if value < min_amount :
            min_amount = value
            min_city = key
    print(f'visiting {min_city} for {days} days would cost least amount of {min_amount} among all cities')    

for i in [4,7,10,14] :
    dicto = trip(vacation,i)
    min_city = 'paris'
    min_amount = dicto['paris']
    evaluation(dicto,min_city,min_amount,i)
 

OUTPUT :
visiting dubai for 4 days would cost least amount of 590 among all cities
visiting dubai for 7 days would cost least amount of 635 among all cities
visiting dubai for 10 days would cost least amount of 680 among all cities
visiting mumbai for 14 days would cost least amount of 730 among all cities

Hope this helps…
SUGGESTIONS ARE ALWAYS WELCOME:blush::blush::blush:
HAPPY LEARNING

1 Like

How to get a indentation exactly in markdown cell…
I had written some text in markdown cell, which. consists of new lines, numberings etc…
But when I run the cell all the text combines…
and there is no indentation that I specified in markdown cell before running. …
Plz help me…

Is there a downside to import things into the code using *

for example:

from math import *

how do i get the print statement as ,
The City Paris costs 540 for a week of stay.
(same for other cities too…)

The LEN function returns an integer indicating the number of characters in the input string . The LEN function returns the actual number of characters in multi-byte strings (or the number of elements in the list in this case ).
It has a return type of int which is not iterable

instead of

for i in len(city):
   # rest of the code

use

for i in range (len(city)):
    # rest of the code

Hope this helps