📕
Dan Fitz's Notes
  • README
  • Ai
    • Supervised Machine Learning
      • Introduction To Machine Learning
      • Regression With Multiple Input Variables
      • Classification
  • Csharp
    • C Sharp Advanced
      • Generics
      • Delegates
      • Lambda Expressions
      • Events
    • C Sharp Fundamentals
      • Intro To C
      • Primitive Types And Expressions
      • Non Primitive Types
      • Control Flow
      • Arrays And Lists
      • Working With Dates
      • Working With Text
      • Working With Files
      • Debugging Applications
    • C Sharp Intermediate
      • Classes
      • Association Between Classes
      • Inheritance
      • Polymorphism
      • Interfaces
  • Java
    • Inheritance Data Structures Java
      • Inheritance Polymorphism Using Overriding And Access Modifiers
      • Abstract Classes And Debugging
      • File I O And Exceptions
      • Collections Maps And Regular Expressions
    • Intro To Java
      • Introduction To Java Classes And Eclipse
      • Unit Testing Arrays And Array Lists
      • Static Variables Methods And Polymorphism Using Overloading
  • Javascript
    • Algorithms Data Structures
      • Big O Notation
      • Analyzing Performance Of Arrays And Objects
      • Problem Solving Approach
      • Problem Solving Patterns
      • Recursion
      • Searching Algorithms
      • Bubble Selection And Insertion Sort
      • Merge Sort
      • Quick Sort
      • Radix Sort
      • Data Structures Introduction
      • Singly Linked Lists
      • Doubly Linked Lists
      • Stacks And Queues
      • Binary Search Trees
      • Tree Traversal
      • Binary Heaps
    • Complete Nodejs
      • Understanding Node.js
      • REST AP Is And Mongoose
      • API Authentication And Security
      • Node.js Module System
      • File System And Command Line Args
      • Debugging Node.js
      • Asynchronous Node.js
      • Web Servers
      • Accessing API From Browser
      • Application Deployment
      • Mongo DB And Promises
    • Complete React Native
      • Working With Content
      • Building Lists
      • Navigating Users Between Screens
      • State Management
      • Handling Screen Layout
      • Setting Up An App
      • More On Navigation
      • Advanced Statement Management With Context
      • Building A Custom Express API
      • In App Authentication
    • Epic React
      • React Fundamentals
      • React Hooks
      • Advanced React Hooks
      • Advanced React Patterns
      • React Performance
    • Fireship Firestore
      • Firestore Queries And Data Modeling Course
      • Model Relational Data In Firestore No SQL
    • Functional Light Javascript
      • Intro
      • Function Purity
      • Argument Adapters
      • Point Free
      • Closure
      • Composition
      • Immutability
      • Recursion
      • List Operations
      • Transduction
      • Data Structure Operations
      • Async
    • Js Weird Parts
      • Execution Contexts And Lexical Environments
      • Types And Operators
      • Objects And Functions
      • Object Oriented Java Script And Prototypal Inheritance
      • Defining Objects
    • Mastering Chrome Dev Tools
      • Introduction
      • Editing
      • Debugging
      • Networking
      • Auditing
      • Node.js Profiling
      • Performance Monitoring
      • Image Performance
      • Memory
    • React Complete Guide
      • What Is React
      • React Basics
      • Rendering Lists And Conditionals
      • Styling React Components
      • Debugging React Apps
      • Component Deep Dive
      • Building A React App
      • Reaching Out To The Web
      • Routing
    • React Testing
      • Intro To Jest Enzyme And TDD
      • Basic Testing
      • Redux Testing
      • Redux Thunk Testing
    • Serverless Bootcamp
      • Introduction
      • Auction Service Setup
      • Auction Service CRUD Operations
      • Auction Service Processing Auctions
    • Testing Javascript
      • Fundamentals Of Testing
      • Static Analysis Testing
      • Mocking Fundamentals
      • Configuring Jest
      • Test React Components With Jest And React Testing Library
    • Typescript Developers Guide
      • Getting Started With Type Script
      • What Is A Type System
      • Type Annotations In Action
      • Annotations With Functions And Objects
      • Mastering Typed Arrays
      • Tuples In Type Script
      • The All Important Interface
      • Building Functionality With Classes
    • Web Performance With Webpack
      • Intro
      • Code Splitting
      • Module Methods Magic Comments
  • Other
    • Algo Expert
      • Defining Data Structures And Complexity Analysis
      • Memory
      • Big O Notation
      • Logarithm
      • Arrays
      • Linked Lists
      • Hash Tables
      • Stacks And Queues
      • Strings
      • Graphs
      • Trees
    • Aws Solutions Architect
      • AWS Fundamentals IAM EC 2
    • Fundamentals Math
      • Numbers And Negative Numbers
      • Factors And Multiples
      • Fractions
    • Mysql Bootcamp
      • Overview And Installation
      • Creating Databases And Tables
      • Inserting Data
      • CRUD Commands
      • The World Of String Functions
      • Refining Our Selections
      • The Magic Of Aggregate Functions
    • Random Notes
      • Understanding React Hooks
  • Python
    • Data Analysis Using Python
      • Loading Querying And Filtering Data Using The Csv Module
      • Loading Querying Joining And Filtering Data Using Pandas
      • Summarizing And Visualizing Data
    • Intro To Python
      • Course Introduction Intro To Programming And The Python Language Variables Conditionals Jupyter Notebook And IDLE
      • Intro To Lists Loops And Functions
      • More With Lists Strings Tuples Sets And Py Charm
      • Dictionaries And Files
Powered by GitBook
On this page
  • Lists
  • Loops
  • for
  • while
  • Functions
  • Built-in functions
  • User-defined functions
  • The main function
  • Documentation (for users and programmers)
  1. Python
  2. Intro To Python

Intro To Lists Loops And Functions

Lists

Lists are one of the most common data structures in Python.

Features:

  • Mutable

  • Members do not have to be same type

  • Ordered

sample_list = ['hello', 4, False]

Useful operations:

  • len(list) returns number of members

  • list[0] returns member at index

    • Note: Accessing members at an index that doesn't exist will throw an IndexError

  • list.index('dog') returns the index of the value

  • list.append(item) adds item to the end

  • list.pop() removes and returns last item

  • list.pop(1) removes and returns item at given index

  • item in list checks if item is a member of the list and returns boolean

Loops

Loops repeat a process/operation multiple times.

for loops run x amount of times (whatever you decide), and while loops run indefinitely as long as a condition is met.

for

Iterating over a list:

numbers = [1, 2, 3, 4, 5]
for number in numbers:
  print(number)

Iterating over a string:

word = "Hello"
for letter in word:
  print(letter)

Iterating over a range of integers (useful when you want to run a code block a specific number of times):

for x in range(10):
  print(x) # prints 0 to 9
  
for x in range(1, 7):
  print(x) # prints 1 to 6
  
for x in range(0, 31, 5):
  print(x) # prints 5, 10, 15, 20, 25, 30
  
for x in range(5, -1, -1):
  print(x) # prints 5 to 0

while

Just be careful with a while loop because if the condition is never met, it never runs. And if the condition is always met, you'll run into an infinite loop that crashes your program.

Pro tip: A common use case is to continually request a specific input, re-asking until the user actually provides it.

userInput = ""
while userInput != "hello":
  userInput = input("Please say 'hello'")

print("You said hello! Thank you.")

To exit a while loop completely, simply use the break keyword.

To move onto the next iteration in a while loop, simply use the continue keyword.

Functions

Functions are just blocks of code that can accept an input, performs some computation, and then can return an output. They are useful for code reuse and help make your application more modular.

Built-in functions

Functions that are part of the core language. Python just hands them to you automatically.

User-defined functions

To create your own function, here's the basic syntax:

def function_name(param1, param2, paramN):
  optionalOutput = ""
  # computations occur here
  return optionalOutput

Pro tip: It's recommended to include a docstring as the first comment in your user-defined function. This description is accessible to any other dev via help(fn) or fn.__doc__.

def fn():
  """This is where the docstring lives"""
  # ...

The main function

Every Python module (i.e. file) has a special __name__ variable that's accessible.

  • If the file is being run as the main program, __name__ is set to "__main__"

  • For all other modules, __name__ is set to the module's filename

Pro tip: Best practice when running a main program is to use the following code pattern:

def main():
  # Entry point for program
  
if __name__ == "__main__":
  main()

Documentation (for users and programmers)

PreviousCourse Introduction Intro To Programming And The Python Language Variables Conditionals Jupyter Notebook And IDLENextMore With Lists Strings Tuples Sets And Py Charm

Last updated 3 years ago