Jens Braun

Learn Python quick


Скачать книгу

when we are first getting started with the language. However, if you exit from the Python Shell and enter it again, all the commands you type will be gone. In addition, you cannot use the Python Shell to create an actual program. To code an actual program, you need to write your code in a text file and save it with a .py extension. This file is known as a Python script.

      To create a Python script, click on File > New File in the top menu of our Python Shell. This will bring up the text editor that we are going to use to write our very first program, the “Hello World” program. Writing the “Hello World” program is kind of like the rite of passage for all new programmers. We’ll be using this program to familiarize ourselves with the IDLE software.

      Type the following code into the text editor (not the Shell).

      #Prints the Words “Hello World”

      print (“Hello World”)

      You should notice that the line #Prints the Words “Hello World” is in red while the word “print” is in purple and “Hello World” is in green. This is the software’s way of making our code easier to read. The words “print” and “Hello World” serve different purposes in our program, hence they are displayed using different colors. We’ll go into more details in later chapters.

      The line #Prints the Words “Hello World” (in red) is actually not part of the program. It is a comment written to make our code more readable for other programmers. This line is ignored by the Python interpreter. To add comments to our program, we type a # sign in front of each line of comment, like this:

      #This is a comment

      #This is also a comment

      #This is yet another comment

      Alternatively, we can also use three single quotes (or three double quotes) for multiline comments, like this:

      ’’’

      This is a comment

      This is also a comment

      This is yet another comment

      ’’’

      Now click File > Save As… to save your code. Make sure you save it with the .py extension.

      Done? Voilà! You have just successfully written your first Python program.

      Finally click on Run > Run Module to execute the program (or press F5). You should see the words Hello World printed on your Python Shell.

      To see these steps in action, you can check out this excellent tutorial by mybringback:

      https://www.youtube.com/watch?v=pEFr1eYIePw.

      However, note that he used Python 2 in the video, so some commands will give you an error. If you want to try his codes, you need to add ( ) for the print statements. Instead of writing print ‘Hello World’, you have to write print (‘Hello World’). In addition, you have to change raw_input() to input(). We’ll cover print() and input() in Chapter 5.

      Now that we’re done with the introductory stuff, let’s get down to the real stuff. In this chapter, you’ll learn all about variables and operators. Specifically, you’ll learn what variables are and how to name and declare them. We’ll also learn about the common operations that we can perform on them. Ready? Let’s go.

      Variables are names given to data that we need to store and manipulate in our programs. For instance, suppose your program needs to store the age of a user. To do that, we can name this data userAge and define the variable userAge using the following statement.

      userAge = 0

      After you define the variable userAge, your program will allocate a certain area of your computer's storage space to store this data. You can then access and modify this data by referring to it by its name, userAge. Every time you declare a new variable, you need to give it an initial value. In this example, we gave it the value 0. We can always change this value in our program later.

      We can also define multiple variables at one go. To do that simply write

      userAge, userName = 30, ‘Peter’

      This is equivalent to

      userAge = 30

      userName = ‘Peter’

      A variable name in Python can only contain letters (a - z, A - B), numbers or underscores (_). However, the first character cannot be a number. Hence, you can name your variables userName, user_name or userName2 but not 2userName.

      In addition, there are some reserved words that you cannot use as a variable name because they already have preassigned meanings in Python. These reserved words include words like print, input, if, while etc. We’ll learn about each of them in subsequent chapters.

      Finally, variable names are case sensitive. username is not the same as userName.

      There are two conventions when naming a variable in Python. We can either use the camel case notation or use underscores. Camel case is the practice of writing compound words with mixed casing (e.g. thisIsAVariableName). This is the convention that we’ll be using in the rest of the book. Alternatively, another common practice is to use underscores (_) to separate the words. If you prefer, you can name your variables like this: this_is_a_variable_name.

      Note that the = sign in the statement userAge = 0 has a different meaning from the = sign we learned in Math. In programming, the = sign is known as an assignment sign. It means we are assigning the value on the right side of the = sign to the variable on the left. A good way to understand the statement userAge = 0 is to think of it as userAge <- 0.

      The statements x = y and y = x have very different meanings in programming.

      Confused? An example will likely clear this up.

      Type the following code into your IDLE editor and save it.

      x = 5

      y = 10

      x = y

      print ("x = ", x)

      print ("y = ", y)

      Now run the program. You should get this output:

      x = 10

      y = 10

      Although x has an initial value of 5 (declared on the first line), the third line x = y assigns the value of y to x (x <- y), hence changing the value of x to 10 while the value of y remains unchanged.

      Next, modify the program by changing ONLY ONE statement: Change the third line from x = y to y = x. Mathematically, x = y and y = x mean the same thing. However, this is not so in programming.

      Run the second program. You will now get

      x = 5

      y = 5

      You can see that in this example, the x value remains as 5, but the value of y is changed to 5. This is because the statement y = x assigns the value of x to y (y <- x). y becomes 5 while x remains unchanged as 5.

      Besides assigning a variable an initial value, we can also perform the usual mathematical operations on variables. Basic operators in Python include +, -, *, /, //, % and ** which represent addition, subtraction,