Skip to main content

Posts

Showing posts from March, 2018

The learning journey

As you progress through the rest of the book, don’t be afraid if the concepts don’t seem to fit together well the first time. When you were learning to speak, it was not a problem for your first few years you just made cute gurgling noises. And it was OK if it took six months for you to move from simple vocabulary to simple sentences and took 5-6 more years to move from sentences to paragraphs, and a few more years to be able to write an interesting complete short story on your own. We want you to learn Python much more rapidly, so we teach it all at the same time over the next few chapters. But it is like learning a new language that takes time to absorb and understand before it feels natural. That leads to some confusion as we visit and revisit topics to try to get you to see the big picture while we are defining the tiny fragments that make up the big picture. While the book is written linearly and if you are taking a course, it will progress in a linear fashion, don’t ...

What could possibly go wrong?

As we saw in our earliest conversations with Python, we must communicate very precisely when we write Python code. The smallest deviation or mistake will cause Python to give up looking at your program. Beginning programmers often take the fact that Python leaves no room for errors as evidence that Python is mean, hateful and cruel. While Python seems to like everyone else, Python knows them personally and holds a grudge against them. Because of this grudge, Python takes our perfectly written programs and rejects them as “unfit” just to torment us. >>> primt 'Hello world!' File "<stdin>", line 1 primt 'Hello world!' ˆ SyntaxError: invalid syntax >>> primt 'Hello world' File "<stdin>", line 1 primt 'Hello world' ˆ SyntaxError: invalid syntax >>> I hate you Python! File "<stdin>", line 1 I hate you Python! ˆ SyntaxError: invalid syntax >>> if you come out...

The building blocks of programs

we will learn more about the vocabulary, sentence structure, paragraph structure, and story structure of Python. We will learn about the powerful capabilities of Python and how to compose those capabilities together to create useful programs. There are some low-level conceptual patterns that we use to construct programs. These constructs are not just for Python programs, they are part of every programming language from machine language up to the high-level languages. input: Get data from the the “outside world”. This might be reading data from a file, or even some kind of sensor like a microphone or GPS. In our initial programs, our input will come from the user typing data on the keyboard. output: Display the results of the program on a screen or store them in a file or perhaps write them to a device like a speaker to play music or speak text. sequential execution: Perform statements one after another in the order they are encountered in the script. conditional executio...

What is a program

The definition of a program at its most basic is a sequence of Python statements that have been crafted to do something. Even our simple hello.py script is a program. It is a one-line program and is not particularly useful, but in the strictest definition, it is a Python program. It might be easiest to understand what a program is by thinking about a problem that a program might be built to solve, and then looking at a program that would solve that problem. Lets say you are doing Social Computing research on Facebook posts and you are interested in the most frequently used word in a series of posts. You could print out the stream of facebook posts and pore over the text looking for the most common word, but that would take a long time and be very mistake prone. You would be smart to write a Python program to handle the task quickly and accurately so you can spend the weekend doing something fun. For example look at the following text about a clown and a car. Look at the text an...

Writing a program

Typing commands into the Python interpreter is a great way to experiment with Pythons features, but it is not recommended for solving more complex problems. When we want to write a program, we use a text editor to write the Python instructions into a file, which is called a script. By convention, Python scripts have names that end with .py. To execute the script, you have to tell the Python interpreter the name of the file. In a UNIX or Windows command window, you would type python hello.py as follows: csev$ cat hello.py print 'Hello world!' csev$ python hello.py Hello world! csev$ The “csev$” is the operating system prompt, and the “cat hello.py” is showing us that the file “hello.py” has a one line Python program to print a string. We call the Python interpreter and tell it to read its source code from the file “hello.py” instead of prompting us for lines of Python code interactively. You will notice that there was no need to have quit() at the end of the Python progr...

Terminology: interpreter and compiler

An interpreter reads the source code of the program as written by the programmer, parses the source code, and interprets the instructions on-the-fly. Python is an interpreter and when we are running Python interactively, we can type a line of Python (a sentence) and Python processes it immediately and is ready for us to type another line of Python. Even though we are typing these commands into Python one line at a time, Python is treating them as an ordered sequence of statements with later statements able to retrieve data created in earlier statements. We are writing our first simple paragraph with four sentences in a logical and meaningful order. It is the nature of an interpreter to be able to have an interactive conversation as shown above. A compiler needs to be handed the entire program in a file, and then it runs a process to translate the high level source code into machine language and then the compiler puts the resulting machine language into a file for later execution. ...

Conversing with Python

Before you can converse with Python, you must first install the Python software on your computer and learn how to start Python on your computer. That is too much detail for this chapter so I suggest that you consult www.pythonlearn.com where I have detailed instructions and screen-casts of setting up and starting Python on Macintosh and Windows systems. At some point, you will be in a terminal or command window and you will type python and the Python interpreter will start executing in interactive mode: and appear somewhat as follows: Python 2.6.1 (r261:67515, Jun 24 2010, 21:47:49) [GCC 4.2.1 (Apple Inc. build 5646)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> The >>> prompt is the Python interpreter’s way of asking you, “What do you want me to do next?”. Python is ready to have a conversation with you. All you have to know is how to speak the Python language and you can...

tag