Python Programming 2

In this chapter we're going to learn about text input and output. This is a staple of computer programming in general as virtually in all forms of computers, phones, and tablets there is input and output (IO). When you log into twitter, you view the output tweets displayed from twitter's server by other users. When you send a text message to someone you are inputting data with the expectation of that data being delivered to another user.

Again, in python, this is very easy. In most programming languages you need to perform variable declaration in order to spawn variables that hold data. And in some, you may need to specify this data before hand.

Data, as you may know, comes in many different forms. Every picture you see, every video you watch has some sort of method for displaying data in a visual way for you.

At the root level, all data comes in the form of 0's and 1's. 0 means off and 1 means on. With enough 0's and 1's you can create anything you see that uses a computer. But how is that represented? Forget pictures. If all data comes down to is 0's and 1's how do you make the number 75, for example?

The system of numbers you are familiar with is the decimal system. You count from 0-9 and move to another place. Take say the number 125. Reading from right to left, 5 is in the 1's place. 2 is in the 10's place. 1 is in the 100's place. Because we are familiar with this system it is easy for us to manipulate numbers. Computers use the binary system. Which holds a 1 or 0 in each place. Take say the number 10101. This number is actually 21 in decimal. Reading from right to left again, the 10101 is in the 1's place. The 10101 is in the 2's place. The 10101 is in the 4's place. The 10101 is in the 8's place. The 10101 is in the 16's place.

Written as a mathematical expression you can compute this to decimal easily. 1(16) + 0(8) + 1(4) + 0(2) + 1(1) = 21

What is the number 00010101 in decimal? The answer is still 21. Why? Well the 0's in the front of 10101 may be in the 32's, 64's, and 128's place but because they are 'off' numbers we don't count these values. It's important to note that I used a number with 8 placeholders intentionally. A 0 or 1 is known as a bit of data. 8 bits strung together is known as a byte.

How does it know to use other characters? We know we can use decimal numbers from binary but how does a computer use numbers and letters? ASCII and Unicode are the two formats to be familiar with. Check out this short video I cut from David Malan's course. He is a Harvard computer science professor.

Have you ever heard of kilobytes, megabytes, and gigabytes? These are markers for data measurement. A kilobyte is 1024 bytes (usually just represented as 1000). Several kilobytes is about the size of one of your microsoft word documents. A megabyte is 1000 kilobytes or 1M bytes. Several megabytes is about the size of a 3 minute song, a photo, or a modern webpage. A gigabyte is 1000 megabytes or 1B bytes. Several gigabytes is about the size of a blockbuster movie or a video game. A terabyte is 1000 gigabytes or 1T bytes. We are just now entering the age of terabytes where we measure hard drives and servers in how many terabytes of data they can hold. For further storage sizes, consult the table below or read this short document on some of the larger data storage values. Now that you have an understanding of Information Theory (click the damn link), lets talk about different data types.

Knowing this isn't nearly as important as getting the concept since python doesn't require you to know all about different data types.

int values store integers or whole numbers. float values store complex numbers that contain decimals. char values store characters (basically anything you see on a keyboard). string values store strings of characters like words or sentences. These are just some of the many data types you will encounter. Things like images and videos are represented by bytes but we will talk about this another time.

How do we take these ideas and implement them into our code? Lets start by using python in our PyCharm CE again. Make a new python file and type in

x = 3

By doing this we initialize the variable x and assign it the value of 3. Underneath that line write the following:

print(x)

When you run this program you will find the number 3 printed out to the console. The reason of course is because you assigned the value of 3 to the variable x. In between the first and second line of code write

x = 5

What is going to be printed to the screen? 3 or 5? Run the program to find out.

You can see that 5 has replaced 3 since you redeclared the variable. Delete everything and try this out.

x = 10

y = 20

print(x+y)

30 is printed to the screen because math in python is just that easy. What if you actually wanted it to say 10+20?

x = 10

y = 20

print(x, '+', y)

You need to create something called string concatenation. It basically means you are not using x and y as numbers but rather as strings or words to be put in a sentence.

Let's actually print a word out. We can say:

x = "Hello"

y = "Mr. Scanlan"

print(x, ' ', y)

That's a brief understanding of output. What about input? Say we want to get information from the user and output it to the screen. In order to do this we need to create a variable capable of grabbing the information and storing it. There is no sense in asking for information without saving it. To do this write:

name = raw_input("Enter your name: ")

When you run this program, the console will prompt you for input. You can then write your name in and hit enter. Upon hitting enter the value will be stored in the name variable you created. Try:

name = raw_input("Enter your name: ")

print(name)

This will print the name entered in to the screen. What if you wanted to ask for a number? You could simple write

number = raw_input("Enter a number: ")

print(number)

but this will only get a string value of a number. Meaning you can't perform math functions with it. In order to get a number in the variable write.

name = int(raw_input("Enter your name: "))

print(name)

This will force the number to become an integer value. Now remember this is only a whole number. Decimal numbers will be rounded. To grab any kind of number, simply write.

name = float(raw_input("Enter your name: "))

print(name)

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Video tutorials Video 1 Video 2 Video 3


Now for some practice. Write an application that asks for a user's name, high school, and gpa. Then print this information to the screen.

Create a basic adding calculator. Ask a user for a number. Then another number. Add these two numbers together and print them to the screen.

results matching ""

    No results matching ""