1 / 20

Introduction

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?.

benoit
Télécharger la présentation

Introduction

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction George Mason University

  2. Today’s topics • Review of Introduction chapter • Go over examples and questions • Introduction to Python

  3. Introduction • What is a program? Let’s draw a diagram • What does a computer know how to do?

  4. Examples • What examples did you have trouble with?

  5. 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

  6. 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

  7. Running Your Code • 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

  8. Example run

  9. 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:

  10. 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 • Basically, only share/show/ask about what is on the terminal

  11. 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)

  12. 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()

  13. Exercise 12 • Let’s draw what memory looks like, and why it’s important to understand the concept of storage for programming

  14. 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

  15. Variables • 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

  16. 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

  17. 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()

  18. Comments • 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

  19. 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

  20. Questions? • 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

More Related