Python: Input and Variables

In our last article we covered the history of Python, the print command, working with strings and variables, and how to comment your code. In this episode we will learn how to retrieve input from users, work with variables, and if you are lucky, Loops.

So arise young knight and join me as we search for the Holy Grail of programming!

Variables Vary Very Vivaciously

The easiest way to describe a variable is to think of a box that holds information. You can store data in the box, you can take data out of the box and you can put different data in the box. Heck, if you don’t get a programming job after reading this series, you can even live in a box.

Here is how you work with variables in Python:


#!/usr/local/bin/python


sometext = “I like to eat grits.”


some_number = 127


anothernumber = 113


totalnumbers = some_number + anothernumber


print sometext

print some_number

print totalnumbers

print some_number + anothernumber

print “some_number plus anothernumber is”, some_number + anothernumber

print sometext * 5

In the above example we create variables by assigning them a name and a value (i.e. sometext = “I like to eat grits”). We then print them out. Here is the output:

  I like to eat grits.

  127

  150

  150

  some_number plus anothernumber is 150

  I like to eat grits. I like to eat grits. I like to eat grits. I like to eat grits. I like to eat grits.

The code above should be mostly self-explanatory. The exception would be the statement: print sometext * 5. Because sometext holds a string value and not a mathematical one, the program writes the text contained in the sometext variable five times. When the * symbol is used in this manner it is known as the Repetition operator.

{mospagebreak title=Beating…I Mean Asking the User for Information}

So now that we know everything there is to know about printing and we know about variables, let’s learn how to get data from the user and work with it.


#!/usr/local/bin/python


print “Go no further!”

name = raw_input(“Who are you?”)

print “Did you say:”, name

The above code prints a statement: Go no further! Then it asks the user: Who are you? and prompts them for some text. When the user types in the text and presses enter, it then prints: Did you say: and appends the value of the variable name to the end.

Let’s say the user entered in the name: The Knights Who Go Neep! This is what the code would print:

  Go no further!

  Who are you?

  Did you say: The Knights Who Go Neep!

We use raw_input when we want to get textual input from a user. If we wanted to get a numeric value, we would use input, like so:


#!/usr/local/bin/python


age = input(“Enter your age: “)

print “You are”, age

print “If I were twice your age I would be: “, age * 2

Same principle as before. If the user had entered an age of 20, this would be the printout result:

  Enter your age:

  You are 20

  If I were twice your age I would be 40

And of course we could always combine the two together:


#!/usr/local/bin/python


first_name = raw_input(“What is your first name?”)

last_name = raw_input(“What is your last name?”)

age = input(“How old are you?”)


print “Your full name is “, first_name + last_name

Print “And this is your age: “, age

If the user had answered Lando Calrissian 50, then the printout would be:

  Your full name is LandoCalrissian

  And this is your age: 50

When you use the + symbol with text it is called concatenating strings, or adding them to one another. You will note that the values in the concatenated sentence have no space. You must add one to the sentence like so:


#!/usr/local/bin/python


first_name = raw_input(“What is your first name?”)

last_name = raw_input(“What is your last name?”)

age = input(“How old are you?”)


print “Your full name is “, first_name + “ “ + last_name

Print “And this is your age: “, age

This will print:

  Your full name is Lando Calrissian

  And this is your age: 50

{mospagebreak title=Loops}

Computers are good at doing repetitive tasks; they don’t need to take a lunch break or go to the potty, and unless you program them to, they won’t spend seven hours of their eight hour work day ambling aimlessly around the Internet. The way we get computers to do these repetitive tasks is through iteration, or loops.

The While Loop

The While Loop basically works like this: Do this, while that is true. Like when my girlfriend nags me. My brain says: Create ulcers, while she complains about me not taking out the garbage. When she stops, so does the ulcer creation. Here it is in code:


#!/usr/local/bin/python


my_count = 0

while my_count < 100:

    my_count = my_count + 1

print my_count

This code creates a variable called my_count and stores the value 0 in it. Next it creates a loop that says while the value of my_count is less than 100, print the value of count. It then goes through the loop over and over again, adding 1 to the value of my_count until it reaches 100. The result of this program would be:


  1

  2

  3

  4

  5

  etc

  etc

  etc

  100

A brief note: When working with Python, it is mandatory to indent by four spaces each level. So if you are doing a loop, there must be four spaces. If you add an if to that loop, it must be eight spaces etc. Here is a pseudocode example:


#!/usr/local/bin/python


some code

while variable == variable

    morecode

    while blah blah

    more code

    print something

print whatever happens after the while is escaped

We can also use the loop to force a user to enter some data, as in a password program:


#!/usr/local/bin/python


user_password =”something”


while password !=”something”:

    password = raw_input(“Enter your password to continue nerd: “)

    print “Welcome Captain my Captain! I didn’t notice you. Did you gain weight?”

Notice that I indented the line that contained: password = raw_input. This is because it is the next level under the While statement. Then I did not indent the print statement. That is because the print statement occurs OUTSIDE of the loop and is only triggered when the loop is exited. If the user tried to use this program and did not enter the right password several times, here is what it would look like:

  Enter your password to continue nerd: apples

  Enter your password to continue nerd: oranges

  Enter your password to continue nerd: biscuits

  Enter your password to continue nerd: something

  Welcome Captain my Captain! I didn’t notice you. Did you gain weight?

{mospagebreak title=For Loop}

The For Loop will loop for every value you have in a list. Here is a sample:


#!/usr/local/bin/python


for letter in “James”:

print letter

This code will loop until every letter of James is printed out, like so:

  J

  A

  M

  E

  S

Pretty simple right? Let’s involve the user more:


#!/usr/local/bin/python


user_name = raw_input(“Enter your name: “)


for letter in user_name:

print letter

If the users name is something like Javier, it will print out:

  J

  A

  V

  I

  E

  R

And that is it for loops in Python. There are no Do While loops as in other languages.

Conditional Statements

Sometimes in programming you need to ask certain questions and have the program respond accordingly. You can do so with conditional statements. Basically the way they work is this: "If this, then that." They get more complicated of course. You also can say "If this happens, do this, else do that." Or even "If this and That, or This or That, then do this else do that or that."

I hope that is confusing, because this is the end of the article, and I want to ensure you return to enlighten yourself when I write the next episode on Python Conditionals.

Till then…

[gp-comments width="770" linklove="off" ]

antalya escort bayan antalya escort bayan Antalya escort diyarbakir escort