Hacking Secret Ciphers with Python

(Ann) #1
Chapter 3 – The Interactive Shell 35

It’s a bad idea to have differently-cased variables in your program. If you stored your first name
in the variable name and your last name in the variable NAME, it would be very confusing when
you read your code weeks after you first wrote it. Did name mean first and NAME mean last, or
the other way around?


If you accidentally switch the name and NAME variables, then your program will still run (that is,
it won’t have any “syntax” errors) but it will run incorrectly. This type of flaw in your code is
called a bug. A lot of programming is not just writing code but also fixing bugs.


Camel Case


It also helps to capitalize variable names if they include more than one word. If you store a string
of what you had for breakfast in a variable, the variable name whatIHadForBreakfast is
much easier to read than whatihadforbreakfast. This is called camel case, since the


casing goes up and down like a camel’s humps. This is a convention (that is, an optional but
standard way of doing things) in Python programming. (Although even better would be
something simple, like todaysBreakfast. Capitalizing the first letter of each word after the
first word in variable names makes the program more readable.


Practice Exercises, Chapter 3, Set B


Practice exercises can be found at http://invpy.com/hackingpractice 3 B.


Summary - But When Are We Going to Start Hacking?............................................................................................


Soon. But before we can hack ciphers, we need to learn some more basic programming concepts.
We won’t need to learn a lot before we start writing encryption programs, but there’s one more
chapter on programming we need to cover.


In this chapter you learned the basics about writing Python instructions in the interactive shell.
Python needs you to tell it exactly what to do in a strict way, because computers don’t have
common sense and only understand very simple instructions. You have learned that Python can
evaluate expressions (that is, reduce the expression to a single value), and that expressions are
values (such as 2 or 5 ) combined with operators (such as + or -). You have also learned that you
can store values inside of variables so that your program can remember them to use them later on.


The interactive shell is a very useful tool for learning what Python instructions do because it lets
you type them in one at a time and see the results. In the next chapter, we will be creating
programs of many instructions that are executed in sequence rather than one at a time. We will go
over some more basic concepts, and you will write your first program!

Free download pdf