PyLadies December Meetup

This meetup was going to be a Python 101 session as decided in the last one. The PyLadies had to pick up any topic of interest from the list posted by Nisha on the Pyladies google group. I had picked up the topic Tuples, Sets and Dictionaries. As most of us had our exams going in the last month, we didn’t get much time to prepare and therefore, the session was taken by Kushal.

Kushal began with telling us the basics of Python for e.g. unlike other languages, there is no need to mention the data type in Python, we don’t have curly braces to mark the beginning and end of a block of code, everything we write has to be properly indented and various other points to be kept in mind while coding in Python. We began with writing very basic code like printing numbers from 1 to 10, printing the sum of numbers from 1 to 10 and printing various patterns using “*”. After learning about how to use the loops ( while and for loop) and learning about the conditional statements (if…else), Kushal taught us about the Lists. We then learnt about the methods like append(), insert(), sort(), max(), remove() and pop() that are used with the list objects and wrote some functions like :-

> def add(a, b):
…     return a + b

>>> add(5,4)

>> add(9)
Traceback (most recent call last):
File “<stdin>”, line 1, in <module>
TypeError: add() missing 1 required positional argument: ‘b’

This function simply adds up two numbers passed as arguments to the function and returns the sum. If we do not pass arguments to the function correctly, we get a TypeError as mentioned above.

>> def myfilter(numbers, n):
…     list2 = []
…     for i in numbers:
…         if i > n:
…             list2.append(i)
…     return list2

>>> myfilter([34,35,67,54,9], 50)
[67, 54]

This function takes a list (called numbers) and an integer(n) as arguments and returns a list (list2 in the above code) of numbers that are greater than n.

Kushal also gave us a problem to solve which was called “fizzbuzz”. The problem statement was:- Print the numbers upto n. If the number is divisible by 3, print fizz, if the number is divisible by 5, print buzz and if the number is divisible by both 3 and 5, print fizzbuzz.

Here is the solution to the problem :-

#!/usr/bin/env python3
n = 1
while n < 101:
    if n % 3 == 0 and n % 5 == 0:
        print(“fizz buzz”)
    elif n % 3 == 0:
        print(“fizz”)
    elif n % 5 == 0:
        print(“buzz”)
    else:
        print(n)
    n +=1

The output of the above code is as follows:-

$ python3 fizzbuzz.py
1
2
fizz
4
buzz
fizz
7
8
fizz
buzz
11
fizz
13
14
fizz buzz
16
………

After learning about the lists and writing some basic codes, we had a lunch and coffee break. 😀 When we returned, we learnt how to use the Api to get various kinds of information. We used the World Population Api, to list the countries all over the world and to find the life expectancy of a person, which was real fun. 😀

This is how we did it :-

  1. To list all countries.

import requests
>>> r = requests.get(“https://www.python.org&#8221;)  # r is the response object
>>> r.status_code
200                                                                                        #response code 200 means OK
>>> print(r.text)                                                                # to see the html of the website
>>> r = requests.get(“http://api.population.io/1.0/countries&#8221;)
>>> r.status_code
200
>>> result = r.json()  #to convert the result to json format
>>> type(result)
<class ‘dict’>
>>> result.keys()
dict_keys([‘countries’])
>>> countries = result[‘countries’]
>>> type(countries)
<class ‘list’>
>>> for name in countries:
…     print(name)

Afghanistan
Albania
Algeria
Angola
Antigua and Barbuda
Azerbaijan
Argentina
Australia
Austria
The Bahamas
Bahrain

……………..

 

2. To find the life expectancy of a person.

>> r = requests.get(“http://api.population.io:80/1.0/life-expectancy/remaining/female/India/2016-12-03/19/&#8221;)
>>> r.status_code
200
>>> result = r.json()
>>> result.keys()
dict_keys([‘country’, ‘date’, ‘remaining_life_expectancy’, ‘age’, ‘sex’])
>>> result[‘remaining_life_expectancy’]
76.4208673796145

In the end, we had a discussion about the next meetup. Anwesha told us that we would be having a session on PyCharm (an IDE used mainly for Python) in the next month.

The Python 101 session was really helpful for all of us as it helped us recall the concepts from the very beginning.

I really enjoyed this meetup and look forward to the next meetup in January. 🙂

img_20161203_161409

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s