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
>>> 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
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,
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.
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
>>> "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
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
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.
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.
- Get Python to print out the sum of 132 and 45.
- Have Python divide 125 by 5 then multiply the result by 2 and print it.
- Raise 3 to the second power and multiply the result by 3. Divide it by 2 and print the result.
- 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.
- Tell Python to print out your name.
- Add the words “Hello” and “World” with a space in between, and print.
- Have Python print out a sentence by adding each word together.
- Multiply “Hello” by 5 and print the result.
- Add “Hello” and 5
BONUS: Try to get #9 to work.
Table of Contents
- Python Introduction and Installation Guide
- Python Files and the Interpreter
- Experimenting With Numbers and Text In Python
- Python Variables
- Working With Number Variables In Python
- Python String Basics
- Advanced Python Strings
- Python Comments
- Python Lists
- Python List Methods
- Python Multidimensional Lists
- Python Tuples
- Python Boolean Operators
- Python If Statements
- Python While Loops
- Python For Loops
- Python Dictionaries
- Python Advanced Dictionaries
- Python Functions