Lesson 13 Introduction to Programming Student Resources


This simple flowchart shows how a conditional statement is used to help someone decide whether to go golfing



Download 0.83 Mb.
Page3/9
Date28.05.2018
Size0.83 Mb.
#51381
1   2   3   4   5   6   7   8   9

This simple flowchart shows how a conditional statement is used
to help someone decide whether to go golfing.

Flowcharting can be beneficial to all types of people, not just programmers, because it helps clarify what actually happens or needs to happen in an event or process. You can also use flowcharts to train people (for example, to illustrate what to do in the event of a fire at your school). With a flowchart, it is easier for you to identify problem areas and opportunities for improvement. In fact, while flowcharts are not as frequently used in programming as they used to be, they are used to capture business process models and decision making.



Student Resource 13.5

Introduction to Programming: Using Python

Student Name:_____________________________________________________ Date:_____________

We all know how useful computers are. But for a computer to do anything, it needs a computer program. Without programmers, we would not have the programs that make our computers and mobile devices so useful. Many people think that programming is complicated and that program code is hard to understand. While this can be true for complex software, writing simple programs can be easy and fun. Learning how to program is not only rewarding, but it can help you become better in many areas, such as problem solving in mathematics. This hands-on exercise will teach you some basics of programming in the Python language. You will work within an interpreted environment so that you can enter individual commands to see what they do, and later you can put together a program.

Writing Python

Directions: In this exercise, you’ll practice writing Python code in the Python interpreter. Python as a programming language is popular among both hobbyists and professionals. Python programs can be found running on websites as well as on Windows and Linux computers.

We’ll start simply and then keep adding in more elements of code. Keep in mind that any programming requires accurate typing and an exact arrangement of words and symbols. Don’t get frustrated if your code doesn’t do what you expect. Keep trying. If you get stuck or need help, ask your teacher.

When you start Python, a window appears with some introductory text about the version you are running. Also, a prompt appears, waiting for your command: >>>



You will type your code after the >>> prompt. As soon as you hit the Enter key, that code will be interpreted (converted into machine language and executed). The result (if any) will display on the next line.



Specifying a Value

The simplest instruction to enter in Python is specifying a value at the prompt, which then becomes the output. Type the following sequence:



  • 5
    The number 5 becomes the output.

  • 5+2
    Python adds the two numbers, and the output is the number 7.

  • 5*2
    Python multiplies the two numbers, and the output is the number 10.

  • 5+2*3
    Notice that the computer obeys mathematical operator precedence rules by computing 2*3 first, before adding 5. The output is the number 11.



Strings

A string is a series of characters placed inside quotations marks. You can use either double or single quotes. Try typing "Hello there" . The output will be:

'Hello there'

Variables

Variables are names attached to memory locations, and they store information for us. For example, we can create a variable called age and another called name. We can then tell the computer to assign a value to the variable. To do this, we use an assignment statement. Type the following sequence:



  • age=15
    The number 15 is now stored as the value for the variable age. Notice that nothing is output in response to this instruction (unless you have a typo).

  • name="your name"
    Use your first and last name in place of your name. Don’t forget the quotation marks and the space between your names. Again, there is no output.

  • age
    This command does not output age but instead outputs the value stored in the variable age. The number 15 is the output.

  • name
    You should see your first and last name.

Assignment statements consist of a variable name, an equal sign, and either a value or a computation. Try typing the following assignment statement:

  • greeting="Hello "+name
    Don’t forget the space after hello but within the quote marks. The + is used to “add” strings together (known as string concatenation). In this case, we are creating a string of “hello ” (with a space) and your name combined together, placed into the variable greeting.

Now try typing:

  • greeting
    You will see what is stored in the variable greeting. The two instructions should look something like the following.

Assignment statements are often used not just to store values but to perform computations. One type of computation is to change a value already stored in a variable. For this, we use a variation of the assignment statement where the variable appears on both the left and right side of the equal sign.

Let’s add 1 to age by using age=age+1. In a program, it means “take the value of age, add 1, and store the result in age.” Try the following sequence:


  • age=age+1

  • age
    The output should be the number 16.



Download 0.83 Mb.

Share with your friends:
1   2   3   4   5   6   7   8   9




The database is protected by copyright ©ininet.org 2024
send message

    Main page