Go Back   Science Forums
View Single Post
Old 04-17-2009   #1 (permalink)
phillip1882's Avatar
phillip1882
Thinking


Location:
florida
 
phillip1882 is a glorious beacon of lightphillip1882 is a glorious beacon of lightphillip1882 is a glorious beacon of lightphillip1882 is a glorious beacon of light
 



Not Ranked  0 score     
programming for beginners

i know there are plenty of these type of tutorials on the net already, but one more won't make the internet explode. *knocks on wood* for this tutorial i'll be using python. if you don't have python, i highly recommend it. you can download it here, Python Programming Language -- Official Website
so, you want to be a programmer, eh? i must warn you, once you start, it can be hard to stop. i've gotten to the point now where i'm programming my own games, which is where the real fun begins, but it's a long journey to get to that point. if that's your goal, you must be prepared for the long haul. rule #1. in order to become a good programmer, you actually have to write programs. so let's take it one step at a time. we'll start with a question. what exactly is a variable? a variable is a spot in memory where data is stored. "thank you einstein, now would you be so kind as to translate that into english?"
i like to think of variables as storage bins. they usually hold a number, but can hold other things, for later use.
let's take a quick example. open up some kind of word processor. i personally recommend jedit, but if you really want to you can use notepad. write the following
Code:
x = 5
print x
save it as twolines.py, now to run it. if your using linux, this is fairly simple, just open a terminal, use cd <folder> to go to the correct folder, and type "python twolines.py" (without the quotes). if your using windows this can be harder, visit python.org for tutorials on how to do it in windows.
as you can see you don't get much, just a 5 on the screen. but let's look at what the program is doing. first we are assigning the number 5 to the variable x. then we are calling the print function, and telling it to give us that x value.
okay, so what? well let's make it a bit more complex. try the following
Code:
x = 5
y = 7
print x*y
print x+y
print x/y
print x-y
copy this, save it, and try running. as you can see you get the following.
35
12
0 (?)
-2
what's up with that zero? well unfortunately all variables have a specific type. since we assigned 5 to x and 7 to y, both these variables are assigned as integers. this means that they can be any positive or negative value, but not decimal. there are two ways to get the correct result for the third line. one is to assign one of the variables a decimal value such as assigning y 7.0. the other is to use the "float" function. no, not float as in "hey, that sailor is drowning, let's give him a floating device." but float as in floating decimal point, or a decimal point that can shift position.
lets try it.
Code:
x = 5
y = 7
print x*y
print x+y
print float(x)/y
print x-y
"okay that's kind of cool, we got the correct decimal value, but i want to be able in input my own x and y values without having to save every time."
ah now your thinking like a programmer. let's take in some user input.
try saving the following
Code:
x = int(raw_input("what x value would you like? "))
y= int(raw_input("what y value would you like? "))
print "x*y =", x*y
print "x+y =", x+y
print "x/y =", float(x)/y
print "x-y =", x-y
run, then run again with different inputs, and a third time with other inputs.
congratulations! you're well on your way to becoming a master programmer. that's it for lesson 1. here's a practice problem for you. write a program that switches the x and y value. (hint: use a third variable.)

Last edited by phillip1882; 04-17-2009 at 07:43 PM..
Reply With Quote
 
» Advertisement
» Current Poll
Who's the sexiest man alive? Johnny Depp or Robert Pattinson?
Johnny Depp - 27.27%
3 Votes
Robert Pattinson - 0%
0 Votes
Someone else (please specify) - 45.45%
5 Votes
I'm too macho to think a guy is sexy - 27.27%
3 Votes
Total Votes: 11
You may not vote on this poll.


All times are GMT -8. The time now is 06:09 PM.

Hypography?

Hypography [n.]: A combination of "hyperlink" and "bibliography" - ie, a list of links to electronic documents. Comparable to discography and bibliography, but not cartography.

We have been online since May 2000, and aim to be the best place to find and share science-related content of all kinds.

Share the love!

Please add more science to your life. Use our RSS feeds on your blog, your portal, or your favorite feedreader!


Powered by vBulletin® Version 3.8.3
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Copyright © 2000-2009 Hypography
Part of the Hypography - Science for Everyone Network