2016-01-19 15:39:35 -05:00
2016-01-19 18:01:15 -05:00

Python Fundamentals

Learning Objectives

Students Will Be Able To...

  • Create a variable in python
  • Assign the different data structures to variables
  • Write python statements using control flow
  • Write python statements using loops and iteration

Context

  • The fundamentals of programming translate throughout every language
  • Like learning any new language we're going to start with the basics and build up
  • If you wanted to learn English you wouldn't start by reading a novel, but with the alphabet

Variables

  • Variables are a way to store and save data for use
  • This is called assignment. You are assigning a value to a variable
  • Declaring Variables
    • Cannot start with a number
    • Cannot declare with special characters
    • Written in snake case
  • Open up Python in the terminal
name = "Jason"
fav_num = 8
turtles = ["Raph", "Leo", "Mickey", "Donny"]

Data Types

  • Now you may have noticed that variables can hold different types of values
  • These are called Data Types and python3 has many built-in types
    • Strings
      • Sequence of 0 or more characters(a-z, A-Z, 0-9, !,@,#, ect).
      • python type str() or with the literal '' or ""
      • methods to know
        • .format() [https://pyformat.info/](More info)
        • .isdigit(), islower(), isupper(), check to see if the string is a digit and so on. There are many more like these
        • .lower(), .upper() changed the string to lower and up case
        • .split() changes the string to a list based around the character[s] given
    • Numbers
      • Floats
        • decimals
        • python type float() or with the literal 23.3
        • methods to know
          • is_integer() Return True if the float a whole number, and False otherwise
      • Integers
        • whole number
        • python type int() or with the literal '4'
    • Lists
      • Ordered sequence of items
      • python type list() or the literal ['a', 'b', 'c']
      • methods to know
        • .append() adds a item to a list
        • .pop() removes and returns the last item from the list
    • Dictionary
      • Collections of key, value pairs
      • python type dict() or the literal {'key':'value'}
      • methods to know .get() return the value of a give key, or a default value if its not found .values() returns a list of values in the Dictionary .keys() returns a list of keys in the Dictionary
    • Booleans
      • Represents something that is True or False
      • python type 'bool()' or the literal 'True'
    • Range
      • Range is a special type that represents range of numbers
    • None
      • nothing, nothing at all
      • python type ... there inst one only the literal None

Notable built in functions

  • len() return the length of the given sequence
  • help() shows help documentation of the given object
  • dir() show the available methods of the give object

Math Operators

All math operators can be done on both floats and ints Python comes with the following symbols for mathematical operators.

  • + add
  • - subtract
  • * multiplication
  • \ division
  • \\ floor divided, always returns a whole number
  • % modulo: finds the remainder after division
  • The language also supports PEMDAS
    • 5+(50+5)

Comparison Operators

  • == equality
  • != not equal
  • < less then
  • <= less then or equal to
  • greater then

  • >= greater then or equal to

Control Flow

  • Now we have reached if/else statements
  • If an expression you passed in is True do something
  • Else do something else
if expression == true:
    run code
    
if name == "Jason":
    print("That is an awesome name")
else: 
    print("You should get a different name")
    
if number > 100:
    print("That's a big number")
elif number > 50 && number < 100:
    print("That's a medium number")
else:
    print("Your number is puny")
  • Things to note
    • Put a colon after the expression you want to evaluate
    • if to elif to else
    • indents show what code is part of the body of the statement and where it ends

Lists and Indexing

  • What if you wanted to store more data.
  • Can be assigned to variables
  • Can hold different data types at once
  • The values are indexed for us starting at zero
my_list = ["Jason", "Anna Kendrick", 2015, True]

my_list[0] == "Jason" # True

my_list[2] == 2016 # False
  • Just a heads up indexing through a list is similar to indexing with strings.
  • the value at index zero will be the first element in the list, or the first letter in a string

Functions and Statements

  • We declare our functions with the word def for define
  • Functions follow the same naming principles as declaring variables
    • Snake case
    • Do not start with numbers or special characters
  • Remember how we used white space to organize our code with if/else statements. Well that idea holds true everywhere in Python
def my_name():
    return "My name is Jason"
  • Functions allow us to build code that is reusable
  • This follows the concept of DRY - Don't Repeat Yourself
  • Functions can also take arguments. These allow our functions to be more dynamic
def add(name):
    return "My name is " + name
  • When there is no return statement, the function implicitly returns None
Description
No description provided
Readme 59 KiB
Languages
Python 100%