School Bells

The 8th Grade commences soon, so posting will grind to a near halt. Please expect probably about 3 posts per week, as school will get in the way of my trivial pursuits on the Internet.

Snakes for Novices ~ Pt. 4

Simple Data Types

Data is the center of everything electronic. The basic form is 1s and 0s stored in memory registers, but the Python data types are a little less confusing. The most basic data type is the int, or integer. The integer data type can hold fairly large numbers. Then, there is the str, or string, data type. Strings holds text, and look like this:

'Hello, universe!'

These two types need no special song and dance to be assigned. You only have to do this:

string = 'I am teh string!'
integer = 1749

Advanced Data Types

These data types are a tad more complex, but not very. The first is the list. Lists hold multiple values of any kind. They are assigned like so:

shoppinglist = ['milk', 'eggs', 'wheat']

The values inside of an list can be different types, like this:

alphabet = ['a', 43, 'banana']

Data from a list is accessed like this:

print alphabet[0]

0 here is the first entry, so the output would be:

a

The next type is a tuple. A tuple is like a list, but it is not enclosed in brackets. They are declared like this:

employeeinfo_1 = 'Doe', 'John', 54, 'California'

The data in a tuple is accessed the same way as a list.

Tuples can also be inside of other tuples. They need to be enclosed in parentheses, though.

tupleexample = 12, (32, 94, 45)

If you type tupleexample in the interpreter after declaring it, it will show this:

(12, (32, 94, 45))

Lastly, we have dictionaries. Dictionaries are like lists, but each entry has a second entry attributed to it. For example:

fruitdictionary = {'apple': 'A red fruit.', 'pear': 'A green fruit.'}

This will create a dictionary with two entries about fruit. To access them, type this in the interpreter:

fruitdictionary['pear']

This will print ‘A green fruit.’

Try playing around with these data types and see what you can do.

Next chapter: Flow Control

The Shell’s Most Useless Program

yes

The team that worked on the programs in the /bin folder must have been really bored to have made this. The yes program will continuously repeat anything included as a parameter. For example:

yes Trololol!

will keep repeating Trololol! until the process is terminated.
This program has also been in Linux for quite a while. May I ask, why?

It’s probably too late for this…

I sincerely apologize for my harsh words in “A Nostalgic Retrospective”. My rash opinions have hurt many, and I apologize for that. I have deleted the post, and I retract all of those statements. I didn’t mean what I said. I don’t want to cause trouble. Sometimes my mind gets ahead of my mouth.

Snakes for Novices ~ Pt. 3

Python Programs

The interpreter is great, but true programs are files. Therefore, we will start using .py files.
To save text as a Python program, you need to use a simple text editor. You should be able to choose the file extension, which for Python programs is .py. Notepad is good for Windows, and gedit or nano is good for Linux. When a piece of text is saved as a Python program, it becomes executable, and can be run by clicking on it or typing “python [program]” in the shell.

Getting Input from The User

A program that tells you what 2+2 is isn’t very useful. Therefore, we should be able to allow the user to give input. To do this, the raw_input function is used. A function is a collection of instructions that are referenced by the name of the function. This particular function accepts input from the user through the keyboard. Take a look at this sample program:

inputtext = str(raw_input())
print inputtext
raw_input('Press Enter to exit.')
exit()

I’ll explain this program step by step.

Line 1: A variable (section of memory set aside for use) is created, inputtext, that stores the input given by the user. The str(raw_input()) part is the raw_input function plus a part that tells the function what type of data it is going to get (A string (more on this later), in this case.). Whatever the raw_input function receives will become the contents of the inputtext variable.
Line 2: The contents of inputtext are shown on the screen.
Line 3: The program tells the user to press Enter and waits.
Line 4: The program exits.

This program is very simple, but the raw_input function can be used for much more intricate programs, such as adventure games. Try making one yourself and see how it turns out.

Next chapter: Data Types