Presentation is loading. Please wait.

Presentation is loading. Please wait.

Python For Kids – Week 2 Subtitle. Variables  A variable is a container in which a data value can be stored within the computer’s memory  The data can.

Similar presentations


Presentation on theme: "Python For Kids – Week 2 Subtitle. Variables  A variable is a container in which a data value can be stored within the computer’s memory  The data can."— Presentation transcript:

1 Python For Kids – Week 2 Subtitle

2 Variables  A variable is a container in which a data value can be stored within the computer’s memory  The data can be text or numbers  The stored value can the be referenced using the variable’s name  Data to be stored in a variable is assigned with = Example: a=8

3 Variables # This assigns the value of 100 to the variable named ‘number’ number = 100 - Different variables can have the same value - To find out what the value of a variable is, use the print function #print the variable ‘number’ to find its value print(number)

4 Changing Variables  You can change the value of a variable number = 350 print (number) - You can also assign a variable to another variable new_number = number print (new_number)

5 Naming Variables  Variable names should be meaningful  They can include letters, numbers, and the underscore (_)  Variable names CANNOT start with a number  Variable names CANNOT have spaces  Case matters! Teacher is not the same as teacher

6 Strings  In programming, text is called a string  Create a string by putting quotes around the text

7 Adding Variables with Numbers >>> first = 5 >>> second = 3 >>> first + second

8 Adding Variables with Strings >>> first = “happy ” >>> second = “new year” >>> first + second

9 Adding Variables with Numbers in “ “ >>> first = “5” >>> second = “3” >>> first + second

10 Adding Variables Using ‘ ‘ >>> first = ‘5’ >>> second = ‘3’ >>> first + second

11 Changing Variables >>> score = 7 >>> score = score + 2 >>> print(score)

12 Multi-line text  To use more than one line of text in your string, use three single quotes (‘’’) and then hit the enter key between lines >>> str_example = '''How do you do today? I am fine, thank-you.'''

13 Printing Quote Marks >>> string_a = '''She said, "I can't believe you didn't know that." ‘’’ OR >>> string_b = 'She said, "I can\'t believe you didn\'t know that." ‘ OR >>> string_c = "She said, \"I can't believe you didn't know that\" "

14 Values Within Strings >>> grade = 98 >>> message = ‘I scored %s on my test.’ >>> print (message % grade) %s is a place holder for the grade

15 Multiplying Strings >>> print(10 * ‘a’) Good for adding spaces in your text

16 Lists  Regular variables can only store a single item of data  Python has a data structure called a list  A list is a Python object that can hold multiple items of data  The data is stored in list ‘elements’  You can reference each element individually by using its index number

17 List Elements >>> my_family = [“mom”, “dad”, “brother”, “sister”, “grandma”] The list name is ‘my_family’ and contains 5 elements. Element index numbers start at ZERO my_family “mom”“dad”“brother”“sister”“grandma” 01234

18  Elements in a list can be added or deleted  To reference an item in a list, use the list name followed by square brackets containing the element’s index number  So, to reference “brother”, we use the variable name my_family[2]

19 >>> my_family = [“mom”, “dad”, “brother”, “sister”, “grandma”] >>> print(my_family[2]) Add items to a list >>> my_family.append(‘aunt’) >>> print(my_family)

20 Remove Items from a List >>> del my_family[0] >>> print(my_family)

21 List Arithmetic You can join lists by using the + >>> lista = [‘a’, ‘b’, ‘c’, ‘d’] >>> list1 = [1, 2, 3, 4] >>> print(lista + list1)

22 Multiplying Lists  You can also multiply lists >>> list1 = [3, 5] >>> print(list1 * 6) Cannot divide or subtract lists

23 Practice  Solve the following word problem in the Python Shell  If there are 3 buildings with 25 ninjas hiding on each roof and 2 tunnels with 40 samurai hiding inside each tunnel, how many ninjas and samurai are about to do battle?

24 Homework  Read Chapter 3  Do Programming Puzzles 1 & 3 at the end of chapter 3 (pages 41-42)


Download ppt "Python For Kids – Week 2 Subtitle. Variables  A variable is a container in which a data value can be stored within the computer’s memory  The data can."

Similar presentations


Ads by Google