Showing posts with label Programming. Show all posts
Showing posts with label Programming. Show all posts

Thursday, 2 August 2012

Learn Python with Codecademy

If you have been following my posts, you know I was busy learning python and showing off the new skills. I have already posted a lot about learning python and tools. You might also remember Codecademy that I have shared previously for beginner programmer. Codecademy now has a section to learn python. Python is an easy first language and quit powerful. Now you can learn all the basics of python in Codecademy.


Codecademy is an interactive online programming environment to learn for beginner programmer. Codecademy has lesson set and explanation as well as online compiler to set you up for learning program without extra stuff. So far, they have different units of course on the following section:


  • Python Syntax
  • Strings and Console Output 
  • Conditionals and Control Flow 
  • Functions 

More units are coming soon. Don't fear the word unit. There are at most two lessons in them and all are fairy easy. Codecademy is completely free. You can also sign up if you wish to track your progress along the way. So, what are you waiting for? Learn the basics of python with codeacademy now and start coding like pro :)

I have used Codecademy when I first started learning programming and I personally have like it. Leave your thoughts and experience in the comment section below. If your done with basics if python you might also be interested in reading my other python post such as Best tools to learn and code python with, Learn program by solving problems, Make python prime detector to improve your skills.

Tuesday, 31 July 2012

Project Euler Problem 1 solved with python 3

check mar, chek button, project euler, checking problem, check python problem, check python project euler problem, solve python problem, solve python project euler problem
I have been showing off my excitement of learning python with last few blog post in my blog talking about the best tools to learn python with and so on. One of my recent post was about the Project Euler and I didn't forget to mention how good of an tool to learn and use python practically. As you can see I am a beginner as well, so without farther bragging I will start solving these problems one by one. Continue reading for more stuff.


First of all if you are complete beginner and still confused about what loops like for and whiles or how if statement works go head to my post teaching everything in python for beginner that you need before solving Project Euler problems.

Since there is no point if you just copy paste my solution, I will discuss about some strategy and thought process that will help you solve it yourself first.

Problem 1If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. Find the sum of all the multiples of 3 or 5 below 1000.
Here's how I solved it. First figured out how to find the multiples of a number, then sum it. As revise I also had to eliminated the repeated multiples of both 3 and 5.

Here's my code:


m = list(range(0,1000,3))  # putting all the multiples of 3 in list m
n = list(range(0,1000,5))  # multiples of 5 in list n
l = list(range(0,1000,15)) # multiples of both 3 and 5 (3*5) = 15 in list l
print((sum(m+n))-(sum(l))) # printing sum of m+n and (-) the sum of l

I hope you found it useful or learn something new. I am going to solve more problems in Euler with either python or C++ in the future. You can come back here in the future for more articles like this.

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 :)

Tuesday, 17 July 2012

Learn Programming By Solving Problems

The quest of learning a new programming language is hard and takes a long period of time. We normally start out learning a programming language by learning all it's syntax and practice with it and later go into algorithms and actual problem solving. I am a beginer as well and trying to learn python as the first language and I don't think learning all the synstax before going into solving an actual problem is worth the effort. Project Euler is a place where you can find the problems you need to solve as you learn a new programming language.


More about Project Euler


Project Euler is free and contains hundreds of free problems. These problems are not solvable with your regular pen and paper or calculator. You will be needing help from programming language. You can use any language you want. Some of the problems are extremely difficult but solvable. And this project is very popular so you wouldn't have any problem finding help for it. It will also improve your mathematical and analytical skills as well as programming language with project Euler. Some problem needs deep thinking and planning, so don't panic at the first time.


Learn programming by solving problems


Programming is useful to solving an problem.  Why not start with problem and solve it as you learn. My advice is to choose a simple programming language and learn some basic print functions and few more which shouldn't take more than an hour or so. Then go to the Project Euler and start solving the problem. Just Google search for anything new that you can't solve the problem without and learn it and use it to make your program work. This way you actually learn and understand the procedure and functions more deeply and see real example . This is also equally rewarding. Learning syntax and function gets boring when it is hard to find an good use to it just like our math class in school. With project Euler you also self motivate and find new points for learning the programming language. I choose python because you can do things little faster with it. If you are new to programming python is the best choice for you because of it's simplicity and effecienct manner with it's powerful work.

Read: Best tools to learn python with

Hope you got something out of this post and took a step forward in learning the programming language better. Have fun with Project Euler and subscribe for more articles on this topic as well as various other tips on online tricks. Happy programming.

Create a simple factor generator and prime detector with python


python programming, python how to, python tutorial, python how to generate factors and detect prime numbers
Python is a very efficient program and let's you do a lot of things with a very small coding unlike stuff like C++. I am a beginner in python and learning all the little things. With the beginner function I learned I was able to create a simple algorithm (big word!) to generate factors of a number and detect if it is prime from user input number.


Before going into any explanation (my interreption) here is the code:

n = int(input("what number you want to check? \n:"))
j = (list(range(1, n, 1)))
whylist = []
for numbers in j:
if n%numbers == 0:
whylist.append(numbers)
print("The factors are: ", whylist)

if len(whylist) == 1:
print("So, this is a prime number!")
input()

Read: Best tools to learn python with


Understanding the python factor generator


So save it with yourname.py and double click it to see if it is working. From line one, it asks a user input of a number that is converted into integer which is stored in the variable n. The method used to find factors are by dividing each number by all numbers from 1 to itself and check if the reminder is 0. If remainder is 0 that means it's a factor. To get a list of number from 1 to the given input (n) we created the second line naming the list j. Then we created another list to store the factors itself.

Next we used a for function. For the forth line the basically says: 'for' every 'numbers' in list 'j' followed by ':' to specify what to do next for the 'for'. Next line reads if n(user input) equals to 0 then (:) add the 'numbers' to the 'whylist'. The next line just prints the number out with a string.

Now detecting prime is easy with count function which will count how many elements are in the list (whylist). If the only thing found in whylist is 1 it's a prime number. We used another if statement saying if the lenth(element number) of whylist is 1, then print 'this is a prime number'. We added a input() at the end to push the program for any other input till the program ends.

I hope this post helped you with what you were looking for or helped you learn something new. If you have question leave them below and I will try my best to assist you. You might consider subscribing to this blog because future python tutorial with simple explanation like this one are on my post idea list. Have a great day.

Tuesday, 3 July 2012

How to be the smart student with Python

If you are a school student and currently learning or thinking about learning python, you should know how to hack your life with it. As a learner, I am trying to engulf the basics and I hope to offer you guys something better in the future when I actually learn something. For now I will show you some simple technique you can use to make your math life easier. You can make a simple program to solve complex equation that you know you have to do it in class repeatedly with python and use it whenever you need to solve or check a problem.

For the cool kids, this is not a cheat at all. If you can make a program about the equation, you will most likely know more about that equation than a regular students. This is because you have to break the equation down in step and understand it's method to solve it in steps. You will learn both programming and math at the same time.

Be warned, because I am not a real programmer and I am just a beginner in the python world. So consider my 'stupidity' in such. But anyway we need two tools for our project today. First is basics learning tool and another is software and compiler. Just read two to three chapter from the tutorial site and you will be able to make simple math program easily. If your math notation and formulas goes out of simple math, you can always Google it for how to use it. For math programming I recommend having a pen and paper next to you as well.




We will do an example of python program. To make a program we need a problem to solve. The problem is our math equation. Let's do a hard one first! We will solve the quadratic equation, which is:

And you should know there are two solution to this equation because of plus and minus. I am yet to figure our how to use that on python directly but I will use what I know so far. So let's make the equation simpler to get two result separately.

 
What we are gonna solve  from is ax² +bx+c

Ok, now let's go to the PyScripter>New Python module and delete everything. We need input for a b and c. But when we input something it becomes an 'string' (sentence/word) which we will use 'float'(number with decimal) to use it on computation. Anything after '=' meas to set a variable for that section and for us to remember the user input. Let's write first three line of our program.



a = float(input("Enter a: "))
b = float(input("Enter b: "))
c = float(input("Enter c: "))



Here we set some variable to remember the input and whatever inside " " will show in the pop up box. Run this with the little green play button fro the top and it should ask you for input.

Now to solve the problem and output them with print command. But since we broke down the equation into 2 parts, we will have to add two more variable and solve them separately. What we do next is:


x_1 = (((-b)-(((b**2)-(4*a*c))**.5))/2*a)
x_2 = (((-b)+(((b**2)-(4*a*c))**.5))/2*a)


And now to print them on the output:



print("X1 = ", x_1)
print("x2 = ", x_2)


Now if you run the program, it should definitely give you the right answer. But we don't want to load the editor to get the math solved. So, we will make it as command promt program. The only other thing you need to add now is pause command, so it will pause for the answer.

import os
os.system("pause")


Don't run! But save the program somewhere (.py) . Here's how your finished product look like:



If you have saved it, it's done! Now go ahead and double click it to see your program running as .exe and solve your math aster. I you want to solve repeatedly you can use some loop function with it as well or use it right from the editor. if you run it with double click, you should see something like this:



For another example, I created the program for Pythagorean theorem about right triangle. Which is a² + b² = c² . So here is the programm, I don't think it needs explanation, just look the program and it's fairly simple to engulf.



a = int(input("enter side a, write 0 for unknown"))
b = int(input("enter side b, write 0 for unknown"))
c = int(input("enter c, write 0 for unknown"))

d = (((c**2)-(b**2))**.5)
if a == 0:
    print("a = ", float(d))

if b == 0:
    print("b= ", float(((c**2)-(a**2))**.5))

if c == 0:
    print("c= ", float(((a**2)+(b**2))**5))

import os
os.system("pause")



Don't use the last part of the code if you want to use it right from the editor. You can learn more about math functions by searching on google :P And use this hack for your boring everyday repeated math and be more productive and feel smart!


Monday, 2 July 2012

Best tools to learn and code python with

best learning tool for python programing
Python is one of the most easiest yet powerful and efficient programming language out there. I am not a programmer but I started learning this and pretty excited about it. Learning python is easy, but if you have the right tools and teacher. I can recommend you a good teaching website for basics and to get the right tool. Continue reading for more information.

Best place to learn python


If you are a student like me, I think you can learn some basics of python and start coding to make your own script to solve your own problems. But that's for another post, today it's about best teaching place and the right tool. The best teaching site for python learning and specially up to date python learning is from "How to think like a computer Scientist". If your new, learn python 3 - the latest one. This wil guide you through the explanation and basics of everything. After you finish this, you will be able to start doing some basic scripting to do stuff.

Best tool to write python on


Now for the best tool to learn. By tool I mean the software or python copmpiler. The website recomends a python compiler that is called "PyScripter" which is still my fevorite. And of course these are free and open source project. Up to date software with every built in function for python programming.

Another tool for python learner


For learners it is also important to have a immediate compiler. This gives you the result immediately and other builds on lines o programming. Unless you want to write print every single time, you should have a immediate compiler with you to learn the basics. The software I am using is called "IDLE (python GUI)" . You can do more things with it other than just immediate action but I prefer doing the other stuff on PyScripter.

I am just a learner. If you have found a better tool or learning center, feel free to share it in the comments below. Any question or thoughts are welcome there as well :)