If you remember so far we have mentioned three types, int, float, and str. For those who don't remember int is integer and is a whole number integer (3 and 91 for example). Float is floating point and has a decimal value (7.2 or even 9.0) and str is string or a string of characters ("Hello, World" is a string). To convert between types Python allows the following:
Convert to integer:
>>>int("29")
29
>>>x=5
>>>int(x)
5
>>>int(3.14159)
3
Convert to float:
>>>float("3.14159")
3.14159
>>>float 5
5.0
Convert to string:
>>>str(3.14159)
'3.14159'
>>>str(42)
'42'
You can also test to see what type you have. You do this by using the type() function call. Here are some examples.
>>>type('42')
<class 'str'>
>>>type(4.2)
<class 'float'>
As the class I am following online is using an older version of Python I'm trying to keep up with the new rules but I may (will) miss some. I started running them on the new one so I can learn it better and hopefully this will fix the discrepancies I've been having. Here is a site that has a lot of the new changes.
Showing posts with label float. Show all posts
Showing posts with label float. Show all posts
Friday, September 10, 2010
Operators, Operands, and Hello, World!
Good morning! I couldn't sleep so I'm going through my Python tutorial before work. This morning we'll talk about manipulating variables. We used operators and operands yesterday, but I did not explain them. Operators are symbols, like + and - that manipulate data. The data being manipulated is the operand. So in the statement:
>>>x*3
We are multiplying the variable x (The value of a variable is substituted prior to evaluation by the computer) by 3. 3 and x are operands and * is the operator. You can remember this easily by remember the operator of a piece of machinery is the person doing the work. In this case the * is doing the work on the two operands. The following are legal operators:
Another operator we have used already is the assignment operator or =. Using = assigns the value on the right to the variable on the left. It is similar but different from the equals sign which we will go over later. First let's remember the different types we have gone over (int, float) and let's add another one 'str.' Str, or string, types are strings of characters such as "Hello, World." The "Hello, World" program is a popular first program and so we can use it today to create our first saved program while learning about strings.
First open IDLE (Python GUI). Select File>New Window. A new window with a text editor pops up and here we can write our program.
##This is a Hello, World! program.
##It prints "Hello, World!" on the screen.
print "Hello, World!"
*Note - Remember the # is the comment designator and is not necessary, but a tip for later when writing longer more complex programs is to always include good notes in your program. You can include these comments for your own benefit, put comments of your own or simply start with print.
Select File>Save>> HelloWorld.py (or any name followed by the .py extension) >Save. Now, let's run our program! Do this by selecting Run>Run Module, or pressing F5. If you have made changes it will let you know you need to save it to run it. When you click Run Module a Python Shell will pop up if you didn't have one up already and you should see this:
You now have a program you can run over and over again as long as you want! It doesn't do a whole lot, but it's all yours. Hope everyone has a great Friday!
>>>x*3
We are multiplying the variable x (The value of a variable is substituted prior to evaluation by the computer) by 3. 3 and x are operands and * is the operator. You can remember this easily by remember the operator of a piece of machinery is the person doing the work. In this case the * is doing the work on the two operands. The following are legal operators:
** = exponent/power
* = Multiply
/ = Divide
+ = Plus
- = Minus
Another operator we have used already is the assignment operator or =. Using = assigns the value on the right to the variable on the left. It is similar but different from the equals sign which we will go over later. First let's remember the different types we have gone over (int, float) and let's add another one 'str.' Str, or string, types are strings of characters such as "Hello, World." The "Hello, World" program is a popular first program and so we can use it today to create our first saved program while learning about strings.
First open IDLE (Python GUI). Select File>New Window. A new window with a text editor pops up and here we can write our program.
##This is a Hello, World! program.
##It prints "Hello, World!" on the screen.
print "Hello, World!"
*Note - Remember the # is the comment designator and is not necessary, but a tip for later when writing longer more complex programs is to always include good notes in your program. You can include these comments for your own benefit, put comments of your own or simply start with print.
Select File>Save>> HelloWorld.py (or any name followed by the .py extension) >Save. Now, let's run our program! Do this by selecting Run>Run Module, or pressing F5. If you have made changes it will let you know you need to save it to run it. When you click Run Module a Python Shell will pop up if you didn't have one up already and you should see this:
![]() |
| Hello, World! |
You now have a program you can run over and over again as long as you want! It doesn't do a whole lot, but it's all yours. Hope everyone has a great Friday!
Thursday, September 9, 2010
Declaring Variables w/ Python
Hello again! It's Thursday, which is one of my favorite days, if only because it means Friday is almost here! I'd like to end my Thursday with a lesson on variables. Before we get started however I thought it would be a good learning experience, for myself and perhaps others, to do my logic work in psedocode, and actual code (and perhaps a flowchart). For the "real code" I'm going to use Python.
There are a few really good links for help on Python programming. One is from the Python website itself here. As well as from the Introduction to Computer Science and Programming course on MIT's OpenCourseWare (OCW) website. The MIT OCW website has many classes and programs you can go through free of charge and look very interesting. Harvard also has a similar setup here and it looks like they use C whereas MIT is using Python in instruction.
If you want to follow along with me you should go to the Python website and download version 2.7 If you prefer to use 3.0 that's fine but 2.7 is backward and forward compatible whereas 3.0 has gotten rid of some things that I may be using since I'm following the MIT OCW course, recorded in 2008.
Once you've installed Python, bring up the Python Shell also called IDLE (Python GUI) which should look like this:
From here you can input all the commands which I will preface with >>> to look the same as the Python Shell.
Now, onto the lesson: Variables are memory locations, the contents of which may vary throughout the program. For example:
>>>numberOne = 1 #Hit Return
*Note - # is Python for comment. Meaning, typing it would not hurt, but does nothing for you. I am only letting you know here to hit return to enter 1 as the value for numberOne.
This sets a memory location, nicknamed "numberOne" to the value of 1. To write this in pseudocode you can write it the same "numberOne = 1" since this is a simple statement, or to be more clear:
set numberOne = 1
Remember I said variables can change. We can now type in the Shell:
>>>numberOne = numberOne + 1
This may look weird, but it should be simple enough to see what is happening. We are taking numberOne and setting it equal to numberOne + 1, or just adding one to it. If you type:
>>>print numberOne
You should now get the result: 2.
The last thing I want to mention briefly is the data type of this number:
2
The number 2 is an integer or int, and has no decimal value. In Python, and most other programming languages, this is important. The type is decided when you initialized the variable. To make this make sense try typing 7/2 into the Python Shell and hit return. Notice the answer is not 3.5 but 3! An integer has no decimal value, and your attempt at dividing, which would create a decimal is ignored and it throws out the remainder.
If you are writing a program that requires the decimal values, you can use what is called a floating point or float. To make it a float, just type 7.0 (the .0 part making it float) and divide that by 2. What do you see? 3.5!
Be careful when writing your programs that you keep track of this. There are ways in many programming languages to change the type, but doing so unnecessarily can cause leaks and bugs in your program.
Lastly, a neat trick to see the remainder of the integer division is by using the % sign. Using % divides one side by the other and gives the remainder.
>>>7%2
1
Tomorrow I'll try and post something a little more... interesting to program. Until then enjoy the tutorials and let me know what you think!
There are a few really good links for help on Python programming. One is from the Python website itself here. As well as from the Introduction to Computer Science and Programming course on MIT's OpenCourseWare (OCW) website. The MIT OCW website has many classes and programs you can go through free of charge and look very interesting. Harvard also has a similar setup here and it looks like they use C whereas MIT is using Python in instruction.
If you want to follow along with me you should go to the Python website and download version 2.7 If you prefer to use 3.0 that's fine but 2.7 is backward and forward compatible whereas 3.0 has gotten rid of some things that I may be using since I'm following the MIT OCW course, recorded in 2008.
Once you've installed Python, bring up the Python Shell also called IDLE (Python GUI) which should look like this:
![]() |
| IDLE (Python GUI) / Python Shell |
Now, onto the lesson: Variables are memory locations, the contents of which may vary throughout the program. For example:
>>>numberOne = 1 #Hit Return
*Note - # is Python for comment. Meaning, typing it would not hurt, but does nothing for you. I am only letting you know here to hit return to enter 1 as the value for numberOne.
This sets a memory location, nicknamed "numberOne" to the value of 1. To write this in pseudocode you can write it the same "numberOne = 1" since this is a simple statement, or to be more clear:
set numberOne = 1
Remember I said variables can change. We can now type in the Shell:
>>>numberOne = numberOne + 1
This may look weird, but it should be simple enough to see what is happening. We are taking numberOne and setting it equal to numberOne + 1, or just adding one to it. If you type:
>>>print numberOne
You should now get the result: 2.
The last thing I want to mention briefly is the data type of this number:
2
The number 2 is an integer or int, and has no decimal value. In Python, and most other programming languages, this is important. The type is decided when you initialized the variable. To make this make sense try typing 7/2 into the Python Shell and hit return. Notice the answer is not 3.5 but 3! An integer has no decimal value, and your attempt at dividing, which would create a decimal is ignored and it throws out the remainder.
If you are writing a program that requires the decimal values, you can use what is called a floating point or float. To make it a float, just type 7.0 (the .0 part making it float) and divide that by 2. What do you see? 3.5!
Be careful when writing your programs that you keep track of this. There are ways in many programming languages to change the type, but doing so unnecessarily can cause leaks and bugs in your program.
Lastly, a neat trick to see the remainder of the integer division is by using the % sign. Using % divides one side by the other and gives the remainder.
>>>7%2
1
Tomorrow I'll try and post something a little more... interesting to program. Until then enjoy the tutorials and let me know what you think!
Subscribe to:
Posts (Atom)


