1 / 15

Introduction to Python

Introduction to Python. Lesson 2a Print and Types. Learning Outcomes. In this lesson the students will: Print a message that contains text, numbers and calculations Create variables with proper names Perform calculations using addition, subtraction and multiplication.

crwys
Télécharger la présentation

Introduction to Python

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 to Python Lesson 2a Print and Types

  2. Learning Outcomes • In this lesson the students will: • Print a message that contains text, numbers and calculations • Create variables with proper names • Perform calculations using addition, subtraction and multiplication

  3. More on printing: print() • print() can print text in quotes, numbers and calculations separated by commas • For example: print(“1+2 = ”, 1+2) 1+2 = 3

  4. Variables and values • A variable is a named piece of memory that stores a value • To create a variable in Python: • write the name of the variable and assign it a value num_1 = 10 = means assignment (store the value on the right in the variable on the left)

  5. Example • So what if I wanted to create another variable to store the value 7 and print it to the screen? • Let’s do it in IDLE together

  6. Sample code num_1=7 print(num_1)# Easy print example print(“The value is ”, num_1) # better example

  7. Naming variables • Must begin with a letter (a - z, A - Z) or underscore (_) • Remaining characters can be letters, numbers or _ • Case Sensitive • Can be any (reasonable) length • Can’t be a keyword • The following is recommended: • Always start a variable name with a lowercase letter. • Use underscores to separate words. • Use meaningful names

  8. Valid names • My_var • my_var • $_123 • number! • thisIsAVARiableDoYOuLikeIT • print

  9. Kinds of values • Python allows us to store different types of values (data types or classes). For example: • Integers: whole numbers e.g. 5, 899, 12 • Strings: A stringis a sequence of one or more characters. • Strings start and end with quote " or apostrophe ' characters. name = "Hello"str1= "This is a string"str2 ="This is a really reallyreally long string!"

  10. Example • Write a program that creates two Strings. The first String should store your first name and the second string should store your surname. Print your full name to the screen on a single line. • Write a program that has two integer variables, one with the value 100 and the other with the value 15. Print out the values in the variables.

  11. Sample Code first_name = “John” surname=“Doe” print(first_name, surname) ____________________________________________ num_1 = 100 num_2 = 15 print(num_1, num_2)# better with a message

  12. Operations and Expressions

  13. Example • Write a program that creates two integer variables, gives them values and then prints the sum of the variables to the screen.

  14. Sample Code num_1 = 10 num_2 = 20 print(num_1,“+”, num_2, “=”, num_1+num_2)

  15. Lesson Review • In this lesson …

More Related