Experimenting With Numbers and Text In Python

Introduction

You probably want to jump in and start coding right away. That’s a great attitude to have, but it’s much better to experiment with the language and your programming environment first. If you’ve never programmed or never worked with an interpreted language like Python before, it’s important to get a feel for the way Python works and start to develop a workflow. One great aspect of Python being interpreted is the ability to write a couple of quick lines of code and test them out in real time. There really isn’t much setup beyond what you’ve already done.

Playing With Numbers

Without knowing anything about the language, you can use Python like a basic calculator. Open up either your .py file or the interpreter. Type in a basic math problem and run it.

>>> 10+25
35

If you used a file, you’re probably wondering what happened. That’s one main difference between the methods of running Python. The interpreter will display the value of commands that you entered, files won’t. In the interpreter, just type one number and hit Enter,

>>> 15
15

It’s just the value of what you entered. So, what about files? Well, there’s an easy way to handle that. You just have to tell Python to print what you entered.

print(10+25)

Now, when you run the file, it will correctly output 35. Don’t worry too much about the parenthesis yet. They will come into play later, but just know that whatever you put between them is what Python will output. Also, in case you were wondering, print works in the interpreter too, and you will need it. The interpreter shows that output as a convenience, but it doesn’t actually mean anything. Using print is the “right” way to display output.

You can continue to play around with numbers in Python and try to string together as complex of equations as you’d like. Obviously, more complex math is going to require more some more help, and there are math libraries available in Python for that, but that’s something for a later time.

print(5*10/25-3**3)

The more you practice, the better your understanding of how Python handles numbers will be. For a bit of a challenge, divide numbers that do not divide evenly. You might notice something odd.

Playing With Letters

You can’t do as much with text yet as you can with numbers, but you can do some. One thing remains the same between text and numbers, if you’re using a Python file, you’re going to need to use print to show your output. One quick difference to note is that text needs to be wrapped in quotes. In Python and just about any programming language, text and individual letters need to be wrapped in quotes. So, try getting Python to print out some text.

>>> "Hello World!"
'Hello World!'

If you’re using a file:

print("Hello World!")
Hello World!

Did you notice the difference? Remember from the previous section that the interpreter just spits back out the same thing that you entered. The single quotes from the interpreter example are a great demonstration of this. Python didn’t really process anything. In the print example the quotes are gone. That’s because print knows that you’re interested in the words, not the quotes.

In Python you can add pieces of text together. Well, that’s sort of true. The actual term is “string concatenation,” but you don’t care about that right now. Try adding a couple of words together in print.

print("Hello"+"World!")

Did you get what you expected? All text is literal. That’s part of the reason for using quotes. Python has no idea that those are two words and humans expect a space between them. That part’s up to you. Try it again with a space.

print("Hello"+" World!")

That’s better. Experiment some more with text, and see what you can do. Python allows you to do a ton of interesting things with text, but this is just a small taste.

Exercises

  1. Get Python to print out the sum of 132 and 45.
  2. Have Python divide 125 by 5 then multiply the result by 2 and print it.
  3. Raise 3 to the second power and multiply the result by 3. Divide it by 2 and print the result.
  4. Multiply the sum of 3 and 5 by the difference of 17 and 9. Divide the result by the product of 3 and 2. Print.
  5. Tell Python to print out your name.
  6. Add the words “Hello” and “World” with a space in between, and print.
  7. Have Python print out a sentence by adding each word together.
  8. Multiply “Hello” by 5 and print the result.
  9. Add “Hello” and 5
  10. BONUS: Try to get #9 to work.

Table of Contents

  1. Python Introduction and Installation Guide
  2. Python Files and the Interpreter
  3. Experimenting With Numbers and Text In Python
  4. Python Variables
  5. Working With Number Variables In Python
  6. Python String Basics
  7. Advanced Python Strings
  8. Python Comments
  9. Python Lists
  10. Python List Methods
  11. Python Multidimensional Lists
  12. Python Tuples
  13. Python Boolean Operators
  14. Python If Statements
  15. Python While Loops
  16. Python For Loops
  17. Python Dictionaries
  18. Python Advanced Dictionaries
  19. Python Functions


Comments and Discussions
Linux Forum