Introduction to Python

What is Python?

  • Really, what IS Python?
  • Why are we using Python?
  • Have you used Python before? (Hint: Raspberry Pi Minecraft?)

Python (and computer programming) basics

  • Syntax
  • Data types
  • Operators
  • Variables 

Syntax

  • Basically grammar for computers
  • Someone give an example of a grammatically correct ENGLISH sentence!
  • Now move around the punctuation and randomize the capitalization and scramble the words
  • There's a grammar for every language, including computer language -- it's called syntax!

Basic data types

  • String
  • Integer
  • Float
  • Boolean 
  • Examples?

Hello World!

  • cd into Documents and make a directory named "python_work" and cd into that
  • Make a file called "hello_world.py"
  • Edit the file and type:
    • print("Hello world!")
  • Launch your new program in the terminal!

Operators

  • +, -, *, /, %, <, >
  • Order of operations anyone?
  • Open up your Python interpreter and try it out
    • ex type "python3" into your terminal
    • >>> 5+5*10
  • Note: Ctrl+D quits out of the interpreter back into the terminal emulator!

Python excercises

  • >>> print("Hello world!")
  • Try to solve this math problem by typing one line into the interpreter: 
    • Suppose you do chores once a week, for which you get $5 pocket money, and you have a paper route which you do 5 times a week and get $30—how much money would you earn in a year?

Variables

  • In the interpreter type in:
    • >>> fred = 100
    • >>> print(fred)
  • We just created a variable, called "fred"
  • What is the data type of the variable fred?
  • A variable is just something that stores value.
  • A variable varies, and can be reassigned. Try:
    •  >>> fred = 200
    • >>> john = fred
    • >>> print(john)

Strings and variables 

  • In Python, if you want a variable to hold a string, you need to put quotes around it
    • >>> fred = "this is a string"
    • >>> print(fred) 
  • Now, try this:
    • >>> fred = "this is two
    • What did you get?
  • Try this:
    • fred = ’’’this is two ... lines of text in a single string’’’

Tricks with strings

  • Try this:
    • >>> print(10 * ’a’)
    • >>> print(20 * ’abcd’)
  • A "%" can be a placeholder for values:
    • >>> mytext = ’I am %s years old’
    • >>> print(mytext % 12)
  • Try this as well:
    • >>> mytext = ’Hello %s, how are you today?’
    • >>> name1 = ’Joe’
    • >>> name2 = ’Jane’
    • >>> print(mytext % name1)

Lists 

  • If you wanted to store a shopping list in a variable you could create a string:
    • >>> shopping_list = ’eggs, milk, cheese, celery, peanut butter, baking soda’
    • >>> print(shopping_list)
  • Another way we could do that is create an actual list object in Python. Lists are useful for several reasons.
    • >>> shopping_list = [ ’eggs’, ’milk’, ’cheese’, ’celery’, ’peanut butter’]
    • >>>print(shopping_list)
  • This is more typing, but it’s also more useful. We could print the 3rd item in the list by using its position (called its index position), inside square brackets []:
    • >>> print(shopping_list[2])

Adding and replacing in lists

  • Let's try replacing:
    • >>> shopping_list[3] = ’lettuce’
    • >>>print(shopping_list)
  • Now adding to the END of the list:
    • >>> shopping_list.append(’chocolate bar’)
    • >>> print(shopping_list)
  • A tuple is a list, but it's immutable. 
    • >>> t = (1, 2, 3)
    • >>> print(t[1])
    • Try to add to a tuple. 
  • What does immutable mean? 
Made with Slides.com