Basic Python Program that counts from 1 to 10

B

Many programmers get overwhelmed when they initially approach a problem. An effective approach to help solve the problem, regardless of size, is to logically divide the problem into parts. In this tutorial, we discuss the basic python program that counts from 1 to 10.

For example, let’s code program that counts from 1 to 10. Each time the count increments we want to display a new number, and to help with the concept of code blocks we are going to show what happens after we reach 10. One approach to help in the development of a workflow is to use pseudocode.

Let’s Make a Plan (pseudocode!) for the basic python program that counts from 1 to 10

I like to keep things organized on my computer, so first create a folder, put it in your “documents” folder or someplace similar. I created a folder called, my_python_code.

Step 1: set count to 1
Step 2: display count
Step 3: increment count
Step 4: repeat step 2 and step 3 until count = 10 + 1
Step 5: if count = 11, display “Counting Complete”

Learning to code is hands-on, so open up your code editor, and enter the following code. Do not copy and paste no matter how tempting it is. Doing so will hinder learning.

It is always recommended to follow the Style Guide for Programming Pyhton Code. Follow the link below for details

STYLE GUIDE FOR PROGRAMMING PYTHON CODE

WHAT SHOULD YOU LOOK FOR IN A PYTHON CODE EDITOR?

Python 2.7:

# Python 2.7

count = 1
# Code block 1
while count < 11:
print count
count = count + 1
# Code block 2
if count == 11:
print 'Counting complete.'

Python 3:

# Python 3

count = 1
# Code block 1
while count < 11:
print(count)
count = count + 1
# Code block 2
if count == 11:
print('Counting complete.')

Note that the first line of each example has a # (hash character), followed by a space and then an explanation. This is an inline comment. Such comments have a number of purposes, but for the most part, they are used to either explain code or summarize a specific approach a developer took. Do the comments in the above examples make sense to you? If not, change them.

Finally, as you will soon come to find out, comments are ignored by Python when the code is executed.

Did you notice that the examples use both single =, and double ==? This can be confusing.

  1. n the expression count = 1, the = assigns the value of 1 to the variable named count.
  2. In the conditional, if count == 11:, the == compares the value of count with 11, returning a boolean True or False. Can you tell what the statement evaluates to in the above example after each iteration?

Save the file as count_to_ten.py in the folder you created then exit the editor. Open a terminal or command prompt and navigate to the folder you created.

Now run the code of Basic Python Program: python count_to_ten.py

And the output should look something like this:

Basic Python Program that counts from 1 to 10

Congrats! You just wrote your first program.

Some Additional Useful Content from other sources

How to create a Free Blog on Blogger

About the author

Pardeep Patel
Pardeep Patel

Add comment

Basics of Python