News: Learn to Code in Python, Part One: Variables, Input and Output

Learn to Code in Python, Part One: Variables, Input and Output

In this article, I'll be exploring the basics of Python, i.e. variables, input and output. You'll need Python (2.7+), a computer, and some free time. 

Variables

Simply put, variables are like envelopes. They are empty and meaningless until we place objects and meaning into them and give the envelope a name. When you call up a variable in Python, you "give" or "associate" data with a labeled container. For example: 

red=1

This simple program associates the integer '1' with the container 'red'. '1' can be replaced with any integer, so long as it is a number. However, say you wanted to write a program that asked a user his/her name. This can be done by associating 'red' with a string. Strings are basically a series of characters-letters, numbers, special symbols like (_) and (.) etc., enclosed in single quotes ('), double quotes ("), or triple quotes ('''). For example: 

red='Hi, my name is robot, what is yours?'

This program will associate that string of text enclosed in single quotes with the container 'red'. A more complicated rendition of strings involves the percentage symbol (%), however I will cover that in the second part of this introductory series.

Input and Output

We've covered how to store data in variables/containers, but how do we access it? When a program runs, the basic I/O (input/output) functions are print, raw_input(), and input()

print

One of the most useful tools available in Python is the print function. This simply allows the program to display or print data for the user to read. For example: 

red='Hi, how are you?'

print red

When run, this simple program will associate the string in single quotes with the container 'red', then print the data associated with 'red'. 

Learn to Code in Python, Part One: Variables, Input and Output

raw_input()

This is also a very useful tool. It basically allows the program to accept characters (like strings) into the program. The two brackets at the end of the function can be used to display a string, or variable data. For example:

print "Hi, my name is Robot. What's your name?"

Note: Notice how I used double quotes to enclose this string. This is because there is an apostrophe in the sentence (what's). 

answer=raw_input()

print 'Hi '+answer+', how are you?'

This simple program will first print Robot's introduction and question, wait for a response, then greet the user with his name and another question.

Learn to Code in Python, Part One: Variables, Input and Output

input()

This function will only work for integers. It's useful when dealing with numerical user input, like in a calculator. If a user inputs characters that are not numerical, Python will display an error message stating that the entered data is not defined, which simply means Python understood the input as a request for a variable, but couldn't find the variable in the program. 

Here's an example of a simple program that adds two integers together using the input() function:

print 'Please enter two integers:'

integer1=input('Integer one: ')

integer2=input('Integer two: ')

finaladd=integer1+integer2

print 'The numbers added together are: ', finaladd

Note: Notice how I use a comma (,) in place of an addition symbol (+) when "adding" integers into strings. 

Learn to Code in Python, Part One: Variables, Input and Output

Stay tuned for part two, which will cover more on strings, as well as an introduction to definitions.

Just updated your iPhone? You'll find new emoji, enhanced security, podcast transcripts, Apple Cash virtual numbers, and other useful features. There are even new additions hidden within Safari. Find out what's new and changed on your iPhone with the iOS 17.4 update.

Photo by kapook

3 Comments

Nice! Python is such a user friendly intro language. Great to see a starting-level text on it here! Good job mate.

Thanks! its one, if not my favorite languages :) just for its simplicity, its great for an introduction to programming.

hey! This is a good article for beginners like me .... am in. This tutorial is also going to help me in BuggingWeb that's my blog.

Share Your Thoughts

  • Hot
  • Latest