✒️
SEI 802
  • Introduction
  • About These Notes
  • Syllabus
  • Development Workflow
    • Installfest
      • Mac OSX
      • Linux
      • Git Configuration
      • Sublime Packages
    • Command Line
      • The Terminal
      • Filesystem Navigation
      • File Manipulation
      • Additional Topics
    • Intro to Git
      • Version Control
      • Local Git
      • Remote Git
      • Git Recipes
    • Group Collaboration
      • Git Workflows
      • Project Roles and Tools
    • VS Code Tips & Tricks
  • HTML/CSS
    • HTML
    • CSS Selectors
    • CSS Box Model and Positioning
      • Box Model
      • Display and Positioning
      • Flexbox
      • Grid
      • Flexbox & Grid Games
      • Floats and Clears
      • Additional Topics
    • Advanced CSS
      • Responsive Design
      • Pseudo-Classes/Elements
      • Vendor Prefixes
      • Custom Properties
      • Additional Topics
    • Bootstrap
    • CSS Frameworks
    • Accessibility
  • JavaScript
    • Primitives
    • Arrays
    • Objects
    • Control Flow
      • Boolean Expressions
      • Conditionals
      • Loops
      • Promises
    • Functions
      • Callbacks
      • Timing Functions
      • Iterators
    • DOM and Events
    • DOM Manipulation
    • HTML5 Canvas
    • How To Reduce Redundancy
    • (2019) JavaScript OOP
    • (2016) OOP with Classes
    • (1995) OOP with Prototypes
      • Constructors
      • Prototypes
    • Intro to TDD
    • Scoping
    • Inheritance
      • Prototypal Inheritance
      • Call, Apply, and other Functions
      • ES6 Inheritance
      • Resources
    • Custom Node Modules
    • Additional Topics
      • AJAX, Fetch, and Async/Await
      • AJAX w/JSON and Localstorage
        • AJAX w/JSON
        • Local Storage
      • Async module
      • Data Scraping
  • jQuery
    • Intro
      • DOM Manipulation
      • Reddit Practice
      • Styling
      • Events
    • Plugins
    • AJAX
  • APIs
    • Fetch
    • AJAX w/jQuery
    • AJAX w/Fetch
  • Databases
    • Intro to SQL
    • Advanced SQL
    • MongoDB
      • Intro to NoSQL
      • CRUD in MongoDB
      • Data Modeling
      • Intermediate Mongo
  • Node/Express
    • Node
      • Intro to Node
      • Node Modules
      • Node Package Manager (NPM)
    • Express
      • Intro to Express
        • Routes
        • Views
        • Templates
        • Layouts and Controllers
        • CRUD & REST
          • Get and Post
          • Put and Delete
      • APIs with Express (request)
      • APIs with Express (axios)
    • Sequelize
      • Terminology
      • Setup
      • Using Models
      • Seeding Data
      • Validations and Migrations
      • Resources
      • 1:M Relationships
      • N:M Relationships
    • Express Authentication
      • Research Components
      • Code Components
      • Auth in Theory
        • Sessions
        • Passwords
        • Middleware
        • Hooks
      • Auth in Practice
        • Create the User
        • User Signup
        • Sessions
        • User Login
        • Authorization and Flash messages
    • Testing with Mocha and Chai
    • Mongoose
      • Mongoose Associations
    • JSON Web Tokens
      • Codealong
    • Additional Topics
      • oAuth
      • Geocoding with Mapbox
      • Geocoding and Google Maps
      • Cloudinary
      • Websockets with Socket.io
      • SASS
  • Ruby
    • Intro to Ruby
    • Ruby Exercises
    • Ruby Classes
    • Ruby Testing with Rspec
    • Ruby Inheritance
    • Ruby Data Scraping
  • Ruby on Rails
    • Intro to Rails
    • APIs with Rails
    • Asset Pipeline
    • Rails Auth and 1-M
      • Auth Components
    • Rails N:M
    • ActiveRecord Polymorphism
    • Additional Topics
      • oAuth
      • SASS
      • Rails Mailers
      • Cloudinary
      • Jekyll
  • React (Updated 2019)
    • ES6+/ESNext
      • Const and Let
      • Arrow Functions
      • Object Literals and String Interpolation
      • ES6 Recap
      • ES6 Activity
    • Intro to React
      • Create React App
      • Components and JSX
      • Virtual DOM
      • Props
      • Dino Blog Activity
      • Nested Components
      • Lab: LotR
    • React State
      • Code-Along: Mood Points
      • Code-Along: Edit Dino Blog
      • Lab: Simple Calc
      • Lifting State
    • React Router
      • Browser History/SPAs
      • React Router (lesson and full codealong)
      • Router Lab
    • Fetch and APIs
      • APIs with Fetch and Axios
      • Fetch the Weather
    • React Hooks
    • React LifeCycle
      • Lab: Component LifeCycle
    • React Deployment
    • Additional Topics
      • React Frameworks
        • Material UI Theming
      • Typescript
        • More Types and Syntax
        • Tsconfig and Declaration Files
        • Generics with Linked List
      • Redux
      • TypeScript
      • Context API
      • React Native
  • Meteor
  • Deployment and Config
    • Deploy - Github Pages
    • Deploy - Node/Sequelize
    • Deploy - Node/MongoDB
    • Deploy React
    • Deploy - Rails
      • Foreman (Environment Variables)
    • Deploy - AWS Elastic Beanstalk
    • Deploy - S3 Static Sites
    • Deploy - Django
    • Deploy - Flask
  • Data Structures and Algorithms
    • Recursion
    • Problem Solving - Array Flatten
    • Binary Search
    • Algorithm Complexity
    • Stacks and Queues
    • Bracket Matching
    • Ruby Linked Lists
      • Sample Code
      • Beginner Exercises
      • Advanced Exercises
    • JS Linked Lists
      • Sample Code
      • Beginner Exercises
      • Beginner Solutions
    • Hash Tables
    • Intro to Sorting
    • Insertion Sort
    • Bucket Sort
    • Bubble Sort
    • Merge Sort
    • Quick Sort
    • Heap Sort
    • Sorting Wrapup
    • Hashmaps
    • Trees and Other Topics
  • Python
    • Python Installation
    • Intro to Python
    • Python Lists
    • Python Loops
    • Python Dictionaries
    • Python Sets and Tuples
    • Python Cheatsheet
    • Python Functions
    • Python Classes
    • Python Class Inheritance
    • Intro to Flask
    • Intro to SQLAlchemy
      • Flask and SQLAlchemy
    • Using PyMongo
    • Intro to Django
    • CatCollector CodeAlong
      • URLs, Views, Templates
      • Models, Migrations
      • Model Form CRUD
      • One-to-Many Relations
      • Many-to-Many Relations
      • Django Auth
    • Django Cheatsheet
    • Django Auth
    • Django Polls App Tutorial
    • Django School Tool Tutorial
    • Django 1:M Relationships
    • Custom Admin Views
    • Data Structures and Algorithms
      • Recursion
      • Binary Search
      • Stacks and Queues
      • Linked Lists
      • Binary Trees
      • Bubble Sort
      • TensorFlow & Neural Networks
    • Adjacent Topics
      • Raspberry Pi
      • Scripting
  • Assorted Topics
    • History of Computer Science
    • Regular Expressions
    • Intro to WDI (Course Info)
    • Being Successful in WDI
    • Internet Fundamentals
      • Internet Lab
    • User Stories and Wireframing
      • Wireframing Exercise: Build an Idea
    • Post WDI
      • Learning Resources
      • Deliverables -> Portfolio
      • FAQ
  • Projects
    • Project 1
    • Project 2
    • Project 3
      • Project 3 Pitch Guidelines
    • Project 4
    • Past Projects
      • Project 1
      • Project 2
      • Project 3
      • Project 4
      • Portfolios
    • Post Project 2
    • MEAN Hackathon
      • Part 1: APIs
      • Part 2: Angular
    • Portfolio
  • Web Development Trends
  • Resources
    • APIs and Data
    • Tech Websites
    • PostgreSQL Cheat Sheet
    • Sequelize Cheat Sheet
    • Database Administration
  • Archived Section
    • (Archived) ReactJS
      • Intro to React
        • Todo List Codealong
        • Additional Topics
      • Deploy React
      • React with Gulp and Browserify
        • Setting up Gulp
        • Additional Gulp Tasks
      • React Router
        • OMDB Router
        • OMDB Search
        • Additional Resources
      • React Animations
        • CSS Animations
    • AngularJS
      • Intro to AngularJS
        • Components and SPA
        • Create an Angular App
      • Angular Directives and Filters
      • Angular Animation
      • Angular Bootstrap Directives
        • Bootstrap Modals
      • Angular $http
      • Angular Services
        • Service Recipes
        • ngResource
        • Star Wars Codealong
      • Angular Routing
      • Angular + Express
      • Angular Authentication
        • Additional Topics
      • Angular Components
      • Angular Custom Filters
      • Angular Custom Directives
Powered by GitBook
On this page
  • Python
  • Python Console
  • Philosophy
  • Familiar Datatypes
  • Negative String Indexes!!
  • Whitespace
  • Functions
  • For Loops

Was this helpful?

Python

PreviousTrees and Other TopicsNextPython Installation

Last updated 3 years ago

Was this helpful?

Python

Python is a widely used high-level programming language used for general-purpose programming, created by Guido van Rossum and first released in 1991. Python has a design philosophy which emphasizes code readability. You'll often find that your first guess at what a function is called is surprisingly correct.

For instance, if you want to print something, you write:

print("I'm not dead yet!")

Also, it's true. Python's name is derived from Monty Python.

Python Console

Python has an interactive console you can use in your terminal. Type python3 to get started.

Note: there are two versions: Python2 and Python3. We'll be using Python3. If you ever type simply python in the terminal the terminal will assume you mean Python2. Python3 is better.

Philosophy

The core philosophy of Python is summarized by the document , which includes aphorisms such as:

  • Beautiful is better than ugly

  • Explicit is better than implicit

  • Simple is better than complex

  • Complex is better than complicated

  • Readability counts

Familiar Datatypes

Python has all the data types you're used to using in JavaScript. Python has variables, functions, booleans, integers, floats, strings, lists and dictionaries (objects). You never declare var in Python.

This is all legal Python:

my_name = "Steve"
my_age = 2017 - 1988
is_alive = True
my_hobbies = ["board games", "programming", "bikes"]

person = {
  "name": my_name,
  "age": my_age,
  "hobbies": my_hobbies,
}

print(person)
print(person["name"], "has", len(person["hobbies"]), "hobbies.")

Negative String Indexes!!

Python has some little features that make code really sweet to write. Negative string (and array) indexes is simply one example of great syntax options Python offers.

alphabet = "abcdefghijklmnopqrstuvwxyz"
vowels = "aeiou"

# we can use negative numbers to access characters in a string from backwards!
last_letter = alphabet[-1]
third_to_last_letter = alphabet[-3]
print(last_letter, "is the last letter in the alphabet")
print(third_to_last_letter, "is the third to last letter in the alphabet")

# we can use the `in` keyword to see if something is part of a string!
if ("e" in vowels):
  print("yup. that's a vowel.")

Whitespace

Python never uses curly braces to define blocks of code. Instead, it forces programmers to write their code with proper whitespace. Many people find this annoying and you're bound to hear people scoff at Python for this decision.

Here's what a for loop and an if else statement look like in Python. The whitespace is important! You have to consistently indent with tabs, or spaces. Whichever you choose you have to be consistent with how many tabs or spaces you use at each level!

Notice that Python uses a weird word elif for "else if" statements.

for i in range(1, 18):
  if i % 3 == 0 and i % 5 == 0:
    print("divisible by three and five")
  elif i % 3 == 0:
    print("divisible by three")
  elif i % 5 == 0:
    print("divisible by five")
  else:
    print(i)

Functions

Python allows you to define functions with parameters. Parameters can even be declared with default values in case you want to call a function without passing in parameters:

def greet(name="Mysterious Stranger"):
  print("Hello there", name)

greet("Bruce Wayne")
greet()

For Loops

There's no for (var i = 0; i < a.length; i++) for loops in Python. All for loops in Python use automatic iterators, like this:

# traditional for loop
for i in range(0,10):
  print(i)

# backwards for loop, where it goes down by negative one at each step.
for i in range(10, 0, -1):
  print(i)

# a for loop going from 0 to 100 incremented by 17 at each step.
for i in range(0, 100, 17):
  print(i)

Python comes with lots of built in libraries. We're able to import all sorts of useful tools.

Ever wanted to shuffle an array?

import random

cards = ["Ace", "King", "Queen", "Jack", "Ten", "Nine", "Eight"]
print("Original Deck:", cards)
random.shuffle(cards)
print("Shuffled Deck:", cards)

Ever wanted to choose one random thing from a list?

import random

print("Where should I move?")
cities = ["Seattle", "Portland", "San Francisco", "Vancouver"]
city = random.choice(cities)
print("That's it. I'm moving to", city)

That's just a small taste. Let's see what else Python has to offer!

The Zen of Python