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.
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:
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.
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().
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?'
When run, this simple program will associate the string in single quotes with the container 'red', then print the data associated with 'red'.
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).
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.
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: ')
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.
Stay tuned for part two, which will cover more on strings, as well as an introduction to definitions.
It’s Black Friday week in the Null Byte shop! If you’ve been wanting to improve your skill set in hacker- and cybersecurity-geared topics such as Python, Raspberry Pi, and Linux, now’s the time. We’ve got huge sales on online courses, and we’ve outlined 13 favorites you won’t want to miss. Check them out!