Introduction

1 / 20

# Introduction - PowerPoint PPT Presentation

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.

## PowerPoint Slideshow about ' Introduction' - benoit

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
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?
• 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
• Place your code inside the template file available on the course syllabus
• Indentation is important!
• Save the file (keep the name template.py)
• Open a terminal and change directory to the folder containing your template.py
• Type the following to run your code:

python template.py

• This will print the returned result to the screen
Errors
• 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:
Errors
• 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
Exercise 3

3. return area

• In python:

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

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
• Let’s draw what memory looks like, and why it’s important to understand the concept of storage for programming
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
Variables
• You pick their names from letters, numbers, or the underscore (no spaces!)
• 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
Expressions
• 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
• 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()