Friday, August 20, 2010
Sorry about the wait, I'm also making a c++ blog, so check that out. I'll post the link up later. For the next post we will build a text based game.
Sunday, August 1, 2010
Operations
Another capability that python has is that it can do math. You can use simple operations to solve difficult equations. Just input the equation you want to solve into the interpreter and press enter.
for example:
1+2
Here are all the operations:
operation example equivalent
+ 1+1=2 addition
- 2-1=1 subtraction
* 2*2=4 multiplication
/ 6/2=3 division
% 7%3=1 modulus (gives the remainder of a division)
This will be very helpful in any program that needs any sort of mathematical calculations.
You can also use these in more complex programs using the raw_input() command. Open a new file and start your program.
as an example, we will make a tip calculator:
print "Welcome to tip calculator! Type in the amount of your bill and I'll calculate your tip!"
bill = raw_input("Bill:")
tip = .15*bill
print tip
This program asks for the bill, and takes 15% of it, which is the typical tip and displays it.
These operators can serve any purpose, like a car price calculator, or, as demonstrated above, a tip calculator.
Thursday, May 27, 2010
Random Numbers
Random numbers.
Random numbers are a very important piece of any programming language.
The difficult part is importing the random module. You can write your own randomness module or use the supplied one. To do this simply type:
This imports the random module. Now to set a variable to a random number, simply type:
The random.randint() gives a random integer between the two numbers you put in the parentheses. This is the simplest use of random numbers.
This can be used in conjunction with the elif command to give random output. Here is an example:
if abcd == 1:
print "hello"
elif abcd == 2:
print "hi"
Etc. Etc.
This is very helpful in many scenarios such as having something different said every time with the print command.
Next Post: to be announced
Random numbers are a very important piece of any programming language.
The difficult part is importing the random module. You can write your own randomness module or use the supplied one. To do this simply type:
Import random
This imports the random module. Now to set a variable to a random number, simply type:
abcd= random.randint(1, 10)
The random.randint() gives a random integer between the two numbers you put in the parentheses. This is the simplest use of random numbers.
This can be used in conjunction with the elif command to give random output. Here is an example:
import random
abcd = random (1,10)
if abcd == 1:
print "hello"
elif abcd == 2:
print "hi"
Etc. Etc.
This is very helpful in many scenarios such as having something different said every time with the print command.
Next Post: to be announced
Elif command
The elif command allows you to put several else-like commands in a row. It works like a combination of else and if.
for example:
if < insert condition here >:
< insert action here >
elif < insert condition here >:
< insert action here >
elif < insert condition here >:
< insert action here >
etc. etc.
This makes it so that the elif only executes if the first if is false, and will execute in order, so be careful of that.
Note: ALWAYS remember to indent the line after the else, elif and if commands and put the colon after the condition.
This becomes extremely helpful if there are several different outcomes for a single action.
Next Post: Random Numbers
for example:
if < insert condition here >
elif
elif
This makes it so that the elif only executes if the first if is false, and will execute in order, so be careful of that.
Note: ALWAYS remember to indent the line after the else, elif and if commands and put the colon after the condition.
This becomes extremely helpful if there are several different outcomes for a single action.
Next Post: Random Numbers
Else and if commands
Today we will learn about the else, if, then and elif commands. The if command works just like real life. It works like this:
if < insert condition here>:
< insert action here >
Note: You must indent the line after the if statement and the else statement. Also, you must put a colon after the condition.
So if for example variable "a" is 1 and you want to do something if it is 4, then you write this:
if a == 4:
< insert action here >
Note: you use one equals sign to set things equal to each other, but you use two to compare them. That is why I used one in the variables post and two in this post.
If you want it to do something if "a" = 4 but somethng else if it isn't, then do this:
if a == 4:
< insert action here >
else:
< insert action here >
Note: Always remember to put the colon after else otherwise it won't work.
Next post: elif command
if < insert condition here>:
Note: You must indent the line after the if statement and the else statement. Also, you must put a colon after the condition.
So if for example variable "a" is 1 and you want to do something if it is 4, then you write this:
if a == 4:
< insert action here >
Note: you use one equals sign to set things equal to each other, but you use two to compare them. That is why I used one in the variables post and two in this post.
If you want it to do something if "a" = 4 but somethng else if it isn't, then do this:
if a == 4:
<
else:
<
Note: Always remember to put the colon after else otherwise it won't work.
Next post: elif command
Friday, March 20, 2009
The raw_input() Command
This command tells the computer to wait for user input before continuing. So if you write,
print 'hello world'
raw_input('press enter to continue')
print 'goodbye'
It would print, "hello world" and wait until you press enter to continue, then print "goodbye"
You can also put input into variables. For example, if you want to make a variable "name" and have it ask for a name, you write:
name = raw_input('what's your name?')
Press enter to insert what you typed into the variable.
To make it print the name you typed in, write:
print name
This prints what you have entered as your name.
Next post : else, if and then commands
print 'hello world'
raw_input('press enter to continue')
print 'goodbye'
It would print, "hello world" and wait until you press enter to continue, then print "goodbye"
You can also put input into variables. For example, if you want to make a variable "name" and have it ask for a name, you write:
name = raw_input('what's your name?')
Press enter to insert what you typed into the variable.
To make it print the name you typed in, write:
print name
This prints what you have entered as your name.
Next post : else, if and then commands
Wednesday, March 4, 2009
Variables
Variables are a very important part of the python programming language.
They work very much like variables in algebra. You can set anything not used by python as a variable.
For example, using a as a variable works because it is not already a command. You can also use more than one letter, like abcdefg as a variable.
Making variables is easy. Just type:
abcd = 4
This is just an example. You can use almost any variable name, as long as it isn't used by python. so "print" is a bad variable name, but "print_object" could work.
You can also set the variable to words, not just numbers. For example:
abcd = 'example'
Just make sure to put the quotation marks around the text.
Next Post : raw_input()
They work very much like variables in algebra. You can set anything not used by python as a variable.
For example, using a as a variable works because it is not already a command. You can also use more than one letter, like abcdefg as a variable.
Making variables is easy. Just type:
This is just an example. You can use almost any variable name, as long as it isn't used by python. so "print" is a bad variable name, but "print_object" could work.
You can also set the variable to words, not just numbers. For example:
abcd = 'example'
Just make sure to put the quotation marks around the text.
Next Post : raw_input()
Subscribe to:
Posts (Atom)