Wednesday 18 July 2012

Learn The Absolute Basics Of Python In 15 Minute

I am on my quest to learn python. I am trying to learn it with problem solving which I talked about in my last post. First two things that are coming in handy to solve small problem are python while loop and for statement. I will explain (my interruption) of these functions as well as lists, prints, input, string, float, integer, variable and white-spaces. The purpose of this post is to help an absolute beginner to python as a peer help from another beginner to get you started and learn the rest by yourself. I am using python 3.

Understanding Print in python


Print is anything that your program outputs. Learn the basics which will be enough to get you started and you can continue from this with Google afterward. For python 3, this is how you output a line. 

print("This line will be print")

Understanding string in python


String is a string of words. That's all you need to remember. From above you can say the line that was printed is a string but numbers such as 2, 2.5 are not strings and you can't do math combining two different things.

Understanding float in python


Float are numbers with decimal points next to it. Ex. 2.005, 3.25

Understanding int in python


Int stands for integer - numbers without decimals. Ex. 2 , 3

Understanding Input in python


Input is asking the user to input something. If you run the code from below it will ask you the question and allow you to input something in the program. Remember even if the user input is a float or integer it will convert it into string.

input("How old are you?")

To convert the input string to float or integer in order to apply math on it, you can do the following:

float(input("How old are you?")

You can use brackets as shown with different function on top of it to work and convert.

Understanding variables in python


Variables are variables you set for anything. An example will be x = 1 . But when you read it on program you say x is assigned for 1. An example of print function that is used to store the input given by user is:

x = float(input("How old are you?")

Now you can use the variable on anything while programming which is storing the age that was input by the user. Here's an use shown below to print the age afterwards.

print(x)

Understanding list in pythong


List contains element. List are made the following way


listname = [1, 1, 2, 3, 5]

You can store and call elements from inside a list. You can also make a empty list to store any input or from function later in the program.

Understanding why space after  ':'


White-spaces are used in python to separate or add in the function. If we create a new function and statement then everything from white-space will be included under that condition after ':' . Four spaces separate a section. You will see  more about how it works with while and for statement.

Understanding while statement in python


Look at the code below:


n = 0
while n < 10:
n = n+1
print(n)


We can write it in pure English like this:

[ N has value 0. As long as n is less then do this: add 1 to n and make it a new n and then print the new n. ] Since 1 is still not more than 10 it will continue doing this for 10 times and then stop when the condition meet. Another way to think about this is while this condition doesn't meet do something repeatedly until it does.


Understanding for Statement in python


Let's look at this pieces of code:

mylist = [1, 2, 3, 4]
for everynumber in mylist:
    print(everynumber+1)


The first line is a list name mylist containing the numbers inside. The second line reads for everynumber (you can name this anything), which is a variable to represent every single number in the list followed by in mylist. After that is the ':' to get whitespace and inside the for function. Then we printed out everynumber+1 which is adding one to every single number from mylist. The program should output  2, 3, 4, 5.

The next step


You can do so many things with only these in python. I recommend trying all the things you can do so far with this code before you move on. First get more understanding of these functions and syntax i you need and then learn the math symbols in python (some are different). With that try to make program that will solve some math equation. One of the best tool is Project Euler and you can solve first few problems without any further knowledge. These problems are difficult, so it will take longer to solve but it will certainly boost up your learning strategy. You might consider subscribing because many future articles on python are coming and I will also post solution to Project Euler with clue at the beginning if you need further help. Feel free to ask any question on the comment below or go to http://stackoverflow.com to get help from real programmer if Google can't aid you well enough. Happy programming :)

No comments:

Post a Comment