Unsuccessful attempt.

Yesterday i was wondering about how to decide which project to work on, so i asked the same on dgplug and kushal asked me whether i use wordpress to write blogs and i said yes. So he gave me a project to work on that is to make a html file of your blog in your local machine and then post it on wordpress using the xmlrpc module.

Firstly I searched for this module, and learned few of its methods. So i pounced on writing a snippet of code to give it a try but ghosh i met a error that module doesn’t exist and i gave all my 3 hours trying to download the xmlrpc module but all in vain :-(. I can’t even download a module, still i am trying different things searching and asking. Hope i figure out the error soon, will be very happy after completing this project. 🙂

On the mode to try few more things! Hope soon i will be posting in a different way.

 

Advertisements

List slices tricks

So as i had already written about slices in one of my blog that they are used to deal with a part of a list let us see what are more different magics that a slice can do to a list!

1) Slice List with negative index – Using a negative number as an index in python say n returns the nth element from the right-hand side of the list.
Example – You have a list of 11 numbers and you want to print the last five numbers.

 >>> a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> a[-5:]
[6, 7, 8, 9, 10]

a[index:] is the same as a[index : len(a)] and will return a list of all the elements from the list starting at index and going till the end.

2) Slice a list with steps – This is a simple one, as we write the start and the end as index here we also mention the no. by which we want to skip. syntax – list[start:end:step]
Examples – take a list of 15 numbers.

>>> a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
>>> a[::2]
[0, 2, 4, 6, 8, 10, 12, 14]
>>> a[2:10:2]
[2, 4, 6, 8]

 

3) Slice a list with negative steps – To get a list in a backward order steps this can be used. It has same syntax as the previous one just we give step as a negative value.
Examples – take a list of 15 numbers and print the same list in a reverse order.

a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
>>> a[::-1]
[14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
>>> a[::-2]
[14, 12, 10, 8, 6, 4, 2, 0]

To print a reverse list we have to give step as -1.

Small python project

Yesterday in the dgplug session we were asked to show any project that we have done in the last two days, any simple project will do. But as my exams are going on and I was too stuck with learning GitHub I haven’t done anything worth showing i guess. Because the only code i had written in the last two days was of extracting files of certain extension from a predefined folder and storing that in other folder, that too i did in python2.

Our mentors gave us time to do some small project so i tried to do the same with python3. I was stuck with a very little problem that i was giving the location of the folder in a tuple. I asked the doubt in the dgplug session and one of them explained me that        a = (b) is not equal to a = “b”. So that worked for me removing the path from a tuple.

You can find my code here .

Web development

So today i got shortlisted for one of the internship for web development and i was asked to research on two questions those were –

1. Explain five points which a developer should keep in mind while designing a responsive website which would serve customers across multiple devices with different screen resolution.

SolutionResponsive web designing is very important as per the need of the users. As the number of smartphone users increasing day by day it can’t be predicted easily about the medium of website viewers. These can be done by keeping in mind the CSS, flexible images, Google analytics, fluid grids and employing different versions.

    1. Employing at least three different versions – keeping different browser widths for desktop, mobile and tablet can help us to scale the content freely. Scaling down the contents to fit in a smaller page will make it unreadable so navigation and gestures can be used in mobile version to give user a better UI solution.
    2. Flexible images – This is one of the important key feature in every website design. We need to select for images on the basis of how they will scale properly.
    3. Google Analytics – To test our website in different browsers and applications Google’s resizer helps us to quickly preview our web design on multiple devices and browsers.
    4. Fluid grids – This helps to design layout in percentage values rather than setting in pixels. The elements in the layout re-size according to one another.
    5. CSS – This plays a major role in designing all the above mentioned fluid grids, flexible images and media queries all can be done under CSS. Media queries can be implemented so that our web design automatically changes as per the size of the screen.

2. Explain different measures to reduce the loading time of a website?

Solution – Loading time of a website is a important key feature in reducing its bounce rates and making an effective impact on its users. There are few things which can be used to reduce loading time-

  • Optimize images – An image should always be scaled properly and then uploaded because scaling down with CSS forces our users to upload the full resolution image first and then scale it.
  • Optimizing the CSS – The CSS loads before user can see our site. So minimizing CSS can remove extra spaces from our code and make files to the smallest size which enables user to see the site in less time.
  • Scripts page can load after contents – java script files can load after rest of the page so they should be kept at the end of the content, before the body tag. Defer and async methods can also be used by adding them in code.
  • Optimize Caches – Local memory should be used to cache resources to avoid unnecessary server requests. AJAX can be applied which helps to fetch the data from web server without page refreshing the updated content is loaded automatically while user browses through the page.
  • Other methods like avoiding redirects, minimizing style sheets, avoid using java script heavy plugins can also be used to reduce loading time.

This was my research on these questions. would like to know more tips for more knowledge.

And i hope i get selected for this internship by this research. (fingers crossed) 🙂

Learned GitHub etiquette

In the last dgplug session everyone was discussing various things on IRC about their projects and games made by them so i gave link of one of my repos that was a game a ship war game, so a member from the dgplug session told me that he found the same game on a crash course, and that was true as i am learning things from all the three editions of crash course with python books so i implemented the same game for practice and did few changes. I also thought those changes are worth because the actual game on the book was not having any win situation or like showing the user that they win so i added that. As GitHub is quite new to me and recently learnt how to push our own code into a new repository so i started pushing all different things as new repo! But that’s where i did a mistake like i never thought that way.

Also i was aware of the concept of sending pr if i think my changes worth but at the same time i feel of making a fool of myself by giving a lame pr to a big project. 😦  But that day on IRC after the session, #bhavin192(one of the member in the session) gave me a example that if i write a code to do a stuff investing my all the time and someone else takes it and puts it on their GitHub account so that’s where the owner will feel bad. And yeah he also told me to always mention the name or give credits whosoever first thought of the project or have made it.

I think of the same in a big manner now like credits are most important that’s why the concept of fork is. I think there are many things to learn and this was a big thing.

Looping

Today at dgplug session we learned about looping in python. We learned the while loop it’s syntax and how to deal with infinite loop.

A question was given and we have to do that once as it is and other time we have to use the infinite loop and to come out of the loop we used break statement after providing a if condition in the while loop.

 

Stone Paper Scissor with python.

Remember the all time hit game of school kids stone paper scissor, Ever wanted to return to your childhood and play the game? I wanted to play the game 2 weeks ago when i saw shichan (my favorite cartoon)  playing it to win and become leader of the group. So i asked my friend to play with me but she denied saying she is busy! 😦

So i thought of playing it with my best companion laptop and i searched for how to make a game which gives you random input so, that there will be a fair-play between me and laptop and bravo i read about the Random module in python.

The point that attracted me was this module let’s us generate random numbers. Function for that is Randint – If we wanted a random integer, we can use the randint function. Randint accepts two parameters: a lowest and a highest number.

So finally i made the game using random module  and played it. You can too see the use of random module in my game here. Hope you like the game ! 🙂

Tuple in python

1)Defining a tuple – A tuple is same as list just instead of square bracket we use parenthesis. Tuples are for immutable lists that means the data stored in the tuple can not be changed those are fix lists. Once you define a tuple, you can access individual elements by using each item’s index, just as you would for a list.

ExampleYou have to make a rectangle for your game whose width and height should not change throughout the game.

shapes = (200, 50)
print(shapes[0])
print(shapes[1])

Output ->

200
50

*we can ensure that its size doesn’t change by putting the dimensions into a tuple. While assigning the values to a tuple it’s called packing and when extracting values from a tuple it’s called unpacking.

If you will try to alter the value in a tuple say shapes[0]  = 250, Then python will return a type error. Basically, because we’re trying to alter a tuple,
which can’t be done to that type of object, Python tells us we can’t assign a
new value to an item in a tuple .

2) Writing over a Tuple – Although you can’t modify a tuple, you can assign a new value to a variable that holds a tuple. So if we wanted to change our dimensions, we could redefine the entire tuple:

shapes = (200, 50)
print("real shape:")
for shape in shapes:
print(shape)
shapes = (400, 100)
print("\nModified shape:")
for shape in shapess:
print(shape)

Output ->

real shape:
200
50

Modified shape:
400
100

3) Add, multiply a tuple – A tuple is not a number. But it can be added to or multiplied. By adding two tuples, they are concatenated. One is put after the other. By multiplying a tuple with a number, we add the tuple to itself a certain number of times.
Example:-

amount = (10, 20, 30)

# Add two tuples.
total = amount + amount
print(total)

# Multiply tuple.
mul = amount* 3
print(mul)

Output ->

(10, 20, 30, 10, 20, 30)
(10, 20, 30, 10, 20, 30, 10, 20, 30)

 

When compared with lists, tuples are simple data structures. Use them when you want to store a set of values that should not be changed through-out the life of a program.

 

Parts of a List

1. Slicing a List – This is required when you have to work with big data and want to access only a small part of it, To make a slice, you specify the index of the first and last elements you want to work with. As with the range() function, Python stops one item before the second index you specify.
Example – I have a big list of users of my website but i want to give a gift voucher to the first 3 registrations then –

users = ['charles', 'martina', 'vikky', 'kattie', 'sam']
print(users[0:3])

Output ->

['charles', 'martina', 'vikky']

If you omit the first index in a slice, Python automatically starts your slice at the beginning of the list:

users = ['charles', 'martina', 'vikky', 'kattie', 'sam']
print(users[:4])

Output ->

['charles', 'martina', 'vikky', 'kattie']

*A similar syntax works if you want a slice that includes the end of a list.

2. Looping Through a Slice – If you want to perform some particular operation on a particular subset of list then you can use for loop in slice.
Example printing the name of lucky winners of voucher.

users = ['charles', 'martina', 'vikky', 'kattie', 'sam']
print("Here are the first three winners of vouchers:")
for user in users[:3]:
    print(user.title())

Output ->

Here are the first three winners of vouchers:
Charles
Martina
Vikky

*When you’re working with data, you can use slices to process your data in chunks of a specific size.

3.Copying a List – There will be times when you want the exact same list as the first one created.
Let’s take a situation and understand where you can use it – Imagine you have created a list of your favorite ice-creams and you want to make a separate list for your friend too, but viola you have same taste buds and your friend likes everything in your list so far.
This can be achieved by copying a list,you can make a slice that includes the entire original list by omitting the first index and the second index ( [:] ). This tells Python to make a slice that starts at the first item and ends with the last item, producing a copy of the entire list.

my_icecreams = ['mango', 'chocochips', 'strawberry', 'vanila']
friend_icecreams = my_icecreams[:]
print("My favorite ice-creams are:")
print(my_icecreams)
print("\nMy friend's favorite ice-creams are:")
print(friend_icecreams)

Output ->

My favorite ice-creams are:
['mango', 'chocochips', 'strawberry', 'vanila']

My friend's favorite ice-creams are:
['mango', 'chocochips', 'strawberry', 'vanila']

If you had simply set friend_icecreams equal to my_icecreams, you will not get separate lists and you will not be able to append them separately or perform simple operations on them separately because the choice of your friend may vary for the rest of the flavors.
Now let’s add different flavors to each list to prove them as different lists:

my_icecreams.append('butterscotch')
friend_icecreams.append('chocolate')
print("My favorite ice-creams are:")
print(my_icecreams)
print("\nMy friend's favorite ice-creams are:")
print(friend_icecreams)

Output ->

My favorite ice-creams are:
['mango', 'chocochips', 'strawberry', 'vanila', 'butterscotch']

My friend's favorite ice-creams are:
['mango', 'chocochips', 'strawberry', 'vanila', 'chocolate']

*So if you want to make a copy of your list and perform actions on it without changing the real list you should use slice while making a copy of a list.

Lists work well for storing sets of items that can change throughout the life of a program. The ability to modify lists is particularly important when you’re working with a list of users on a website.However, sometimes you’ll want to create a list of items that cannot change. Tuples allow you to do just that. Python refers to values that cannot change as immutable, and an immutable list is called a Tuple.

My next blog will be on Tuples till then Happy Slicing List! 🙂

Playing with Numerical Lists

Many reasons are there to store a set of numbers. For example in a game you will need to store the high score of the player, In data visualization it’s most of the times you have to scribble up with huge amount of digits. That’s when python rescues you by proving a number of tools to work with lists of numbers.

range() Function – It makes easy to generate a series of numbers.

Example :

for value in range(1,4):
print (value)

OUTPUT ->

1
2
3

*The range() function causes Python to start counting at the first value you give it, and it stops when it reaches the second value you provide. Because it stops at that second value, the output never contains the end value, which would have been 4 in this case.
To print the number from 1 to 4, you will use range(1,5).

Implementing range() in list- Now to make a list of numbers you can directly wrap list() around a call to the range() function, the output will be a list of numbers. You can create almost any set of numbers using range() function.

Example – You want to make a list of squares of first 10 numbers.(In
Python, two asterisks (**) represents exponents .)

squares = [value**2 for value in range(1,11)]
print(squares)

Output:

[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

Simple statistics on list – There are few python functions that are specific list of numbers.

Example – find minimum, maximum and sum of all numbers in list.

>>>digits = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
>>>min(digits)
0
>>>max(digits)
9
>>>sum(digits)
45

The examples I used here are a short list of numbers but these functions are very beneficial when millions of numbers are to be stored in lists like you want to plot a graph of population of each country in the world, you will need much larger lists of numbers which can be handled by these functions easily, We can also work with a small part of list, which python calls slice.

I will write about Slice in my next blog! Till then happy listing numbers!