Skip this Video
Download Presentation

Loading in 2 Seconds...

play fullscreen
1 / 20

Introduction - PowerPoint PPT Presentation

  • Uploaded on

Introduction. George Mason University. Today’s topics. Review of Introduction chapter Go over examples and questions Introduction to Python. Introduction. What is a program? Let’s draw a diagram What does a computer know how to do?. Examples. What examples did you have trouble with?.

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
Download Presentation

PowerPoint Slideshow about ' Introduction' - benoit

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

George Mason University

today s topics
Today’s topics
  • Review of Introduction chapter
  • Go over examples and questions
  • Introduction to Python
  • What is a program? Let’s draw a diagram
  • What does a computer know how to do?
  • What examples did you have trouble with?
converting your code to python
Converting your code to python
  • Solution to exercise 1:

1. num1 = get the first number from the user

2. num2 = get the second number from the user

3. difference = num1 – num2

4. return difference

  • In python:

num1 = input(“Please enter the first number: ”)

num2 = input(“Please enter the second number: ”)

difference = num1 – num2

return difference

input( )
  • input(“prompt”) displays the prompt to the user
    • waits for them to type in something with the keyboard
    • returns whatever was typed in when the user hits enter
    • this value is often stored
running your code
Running Your Code
  • Place your code inside the template file available on the course syllabus
    • Indentation is important!
  • Save the file (keep the name
  • Open a terminal and change directory to the folder containing your
  • Type the following to run your code:


  • This will print the returned result to the screen
  • If we make a mistake in our code, it might crash or print out the wrong value
  • Let’s modify our code to do something wrong:
  • There are many many types of errors
    • If you encounter one, you are allowed to Google the error (cut and paste just the error message) or ask a friend what that error message means
      • You could Google “SyntaxError: invalid syntax”
    • You are NOT allowed to Google for a solution, or ever show your actual code to a friend
    • Understanding what an error message means is different than knowing how to fix it
    • Basically, only share/show/ask about what is on the terminal
exercise 3
Exercise 3

1. radius = get the radius from the user

2. area = radius x radius x 3.14

3. return area

  • In python:

radius = input(“Enter the radius”)

area = radius * radius * 3.14

return area

In python, multiplication is represented with *

In python, integer division represented with / or //

In python, normal division represented with x/(y*1.0)

exercise 9
Exercise 9

1. number = get the number from the user

2. product1 = number x 3

3. product2 = product1 x 3

4. product3 = product2 x 3

5. return product1 + “ “ + product2 + “ “ + product3

  • In python:

number = input(“Please enter a number”)

product1 = number * 3

product2 = product1 * 3

product3 = product2 * 3

return str(product1) + “ “ + str(product2) + “ “ + str(product3)

You can add together numbers and strings, but you must convert the number to a string before adding it to the string, with str()

exercise 12
Exercise 12
  • Let’s draw what memory looks like, and why it’s important to understand the concept of storage for programming
writing code in python
Writing code in python
  • A value is stored in memory by using the assignment operator, =
    • The left side is the label for a memory location, called a variable
    • The right side is always a value (or something that simplifies to a value), that is stored in memory
    • num1 is a variable that stores the value entered by the user
  • You pick their names from letters, numbers, or the underscore (no spaces!)
    • cannot start with a number
    • Python is case-sensitive, so num1 and Num1 are different variables
  • Descriptive variable names are preferred to single letters
    • so difference is better than diff, d, or a
  • Python simplifies expressions, like num1 – num2, to be used in an assignment or return statement
  • Like mathematical expressions, precedence matters; use parentheses as need
    • 3 + 2 * 5 is different than (3 + 2) * 5
expressions and types
Expressions and types
  • Python cannot mix string and numeric types in expressions
    • convert a number to a string using str()
    • convert a string to an integer with int()or rational number with float()
  • Commentsstart with # and are not run
    • They can occur on the same line as code; anything to the right of # does not get run
  • You can put English notes inside your code like this
what else is in this example
What else is in this example?
  • Python cares about indentation, but we will learn why later
  • Your text editor will highlight strings and comments in different colors (pink and gray here)
  • keywordshave special meaning in python, and cannot be used as variable names; your text editor will highlight them in blue
  • We will learn about lines 1 and 11 later this semester
  • If we have time, let’s convert the exercises from today into python code
  • One of these will be the coding part of the quiz in the next lab