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
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:
… return list2
>>> myfilter([34,35,67,54,9], 50)
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 :-
n = 1
while n < 101:
if n % 3 == 0 and n % 5 == 0:
elif n % 3 == 0:
elif n % 5 == 0:
The output of the above code is as follows:-
$ python3 fizzbuzz.py
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 :-
- To list all countries.
>>> r = requests.get(“https://www.python.org”) # r is the response object
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”)
>>> result = r.json() #to convert the result to json format
>>> countries = result[‘countries’]
>>> for name in countries:
Antigua and Barbuda
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/”)
>>> result = r.json()
dict_keys([‘country’, ‘date’, ‘remaining_life_expectancy’, ‘age’, ‘sex’])
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. 🙂