Welcome to Python

Tips for navigating the slides:
  • Press O or Escape for overview mode.
  • Visit this link for a nice printable version
  • Press the copy icon on the upper right of code blocks to copy the code

Welcome!

Girl Develop It is here to provide affordable and accessible programs to learn software through mentorship and hands-on instruction.

Classroom "rules":

  • I am here for you!
  • Every question is important
  • Help each other
  • Have fun

Class Outline

This may change if we end up needing more or less time for certain topics.

  • Class 1: Intro to Python
  • Class 2: Functions
  • Class 3: Conditionals
  • Class 4: Loops
  • Class 5: Lists
  • Class 6: Tuples & dictionaries

Class logistics

Introductions

Tell us about yourself:

  • Name
  • Pronouns
  • Location
  • Programming experience (if any, not required!)
  • What interests you about Python?

Today's topics

  • What/why Python?
  • Simple data types & values
  • Names
  • Expressions & operators
  • Built-in functions
  • Imports
  • Built-in methods

What is Python?

The Python language

  • An "interpreted" general purpose programming language.
    • Huh?? Runs through an interpreter application, as opposed to "compiled" languages, which are converted to machine code and run directly by a computer
  • Can run on many systems: Unix, Windows, Mac.
  • An easy-to-read syntax where whitespace is significant.
  • Open source

The Python language

Example (that you don't need to understand yet!):


                    def add_numbers_between(start, end):
                        sum = 0
                        for n in range(start, end):
                            sum += n
                        return n

                    add_numbers_between(5, 10)
                    

Why Python?

Benefits

For learning:

  • Python's syntax is considered easier to read and use than most languages.
  • The language includes features of many different types of languages, so you can learn concepts that translate to other languages (functional programming, object-oriented programming, etc).

For development:

  • Python can be used for a wide range of applications (web, data science, graphics, games, language, etc) thanks to the huge number of libraries and tutorials.

Console applications

You can make entirely text-based console (command-line) applications/scripts. These are common for automating tedious/repetitive tasks.

Screenshot of a console application

Desktop applications

Applications for non-technical users typically include a GUI (graphical user interface).

Screenshot of a desktop GUI application

Web applications

Web apps are increasingly more popular than downloaded apps. HTML/CSS/JS would be used for the frontend, but Python can be used for the backend of a web application.

Screenshot of a desktop GUI application

Data analysis notebook

Data analysts and data scientists often use notebooks, like Jupyter Notebooks to document and share calculations and outputs.

Screenshot of a Jupyter notebook with Python code

πŸ’¬ What's one thing you'd LOVE to make with Python?

Python setup

Coding Python locally

Usually, we would write Python code on our own computers using:

(click the links above for setup info)

Coding Python on Repl.it

Installing/running Python on lots of different systems/versions is tricky.

In this class, we'll use Replit, an online editor.

https://repl.it/languages/python3

Repl.it setup

  • Create a Replit account
  • Once you're logged in, create a new Python repl
screenshot of repl

print()

We use print() to tell Python what to output. This is very useful in debugging - ex, to see the value of a variable.


                    print("hello world!"
                    

(simple) Data types & values

(simple) data types & values

Programs manipulate values.

Each value has a certain data type.

Data type Example values
Integers 2 44 -3
Floats 3.14 4.5 -2.0
Booleans True False
Strings 'Β‘hola!' 'its python time!'

Names

Name bound to value

Names

In Python, objects have names. Different types of objects (variables, functions, etc) can have names.


A variable is a name bound to a value using an assignment statement:

x = 7
Name Value

Names

The value can be any expression:

x = 1 + 2 * 3 - 4 // 5
Name Expression

It can also be a function:

x = add(1+2)
Name Expression

Name rules

Only certain character combinations are allowed:

  • Must start with a letter or the underscore character
  • Cannot start with a number
  • Can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )

Allowed names

  • banana_gram
  • Bananagram
  • _banana_gram

Non-allowed names

  • 1banana_gram
  • banana-gram
  • banana gram

See more examples at W3Schools: https://www.w3schools.com/python/gloss_python_variable_names.asp

Name rules

Names are case sensitive.

If name is my_fav_hobby, the following are not equivalent:

  • my_fav_hobbY
  • My_fav_hobby
  • MY_FAV_HOBBY
  • myfavhobby

Programming can help you develop an eye for these little details, but software development tools will also help you find these issues.

Naming conventions for variables & functions

  • Use a lowercase single letter (variables only), word, or words
  • Separate words with underscores
  • Make names meaningful, ex first_name rather than fn


Examples

  • m (variables only)
  • hobby
  • my_fav_hobby

See more examples at Real Python: https://realpython.com/python-pep8/#naming-styles

Using names

A name can be referenced multiple times:


                x = 10
                y = 3

                result1 = x * y
                result2 = x + y
                

A name that's bound to a data value is also known as a variable.

πŸ’¬ How many variables are in that code snippet?
4 variables: x, y, result1, result2

Name rebinding

A name can only be bound to a single value.


                my_name = 'Liz'

                my_name = my_name + 'iz'
                

πŸ’¬ Will that code error? If not, what will my_name store?
It will not error (similar code in other languages might, however). The name my_name is now bound to the value 'Liziz'.

Expressions & operators

Expressions (with operators)

An expression describes a computation and evaluates to a value.

Some expressions use operators:


                    18 + 69
                    

                    6/23
                    

                    2 * 100
                    

                    2 ** 100
                    

Let's try it in Repl!

Mathematical operators

Operator Name Example
+ Addition 1 + 2 = 3
- Subtraction 2 - 1 = 1
* Multiplication 2 * 3 = 6
/ Division 6 / 3 = 2
% Modulus (divide & return the remainder) 5 % 2 = 1
** Exponentation (aka powers) 2 ** 3 = 8
// Floor division (divide & round down to the nearest integer) 5 // 2 = 2

Built-in functions

Expressions (with function calls)

Many expressions use function calls:


                    pow(2, 100)
                    

                    max(50, 300)
                    

                    min(-1, -300)
                    

Learn more about built-in functions in the Python docs.

Let's try these in Repl!

Expressions (both ways)

Expressions with operators can also be expressed with function call notation:


                    2 ** 100
                    pow(2, 100)
                    

Imports

Importing functions & modules

Some functions from the Python standard library are available by default. Others are included with a basic Python installation, but need to be imported into your current file before they can be used.

Import a single function from the Operator module :


                    from operator import add

                    18 + 69
                    add(18, 69)
                    

Importing functions & modules

Import the entire Operator module (all of its functions):


                    import operator

                    18 + 69
                    add(18, 69)
                    

Why would we import a single function vs an entire module? Some modules are big! You'll get better performance by importing only what you need.

Nested expressions

Nested expressions

We can combine multiple expression inside one another. These are evaluated from the inside out.

screenshot of expression tree

Exercise: Expressions

Open Open the Class1Ex1OperatorExpressions repl.

Let's work through these questions together!

Forking Replit files

  1. Click the link to the Replit, ex https://replit.com/@lizkrznarich/Class1Ex1OperatorExpressions
  2. Click Fork Replit
  3. Click Edit in workspace

Built-in methods

Built-in methods

Each Python data type has built-in capababilites that are specific to that data type. These are call built-in methods.

Built-in methods use a different syntax from the operators and functions we looked at in the last section:



                        'blar'.upper()

                        my_string = 'blar'
                        my_string.upper()
                        

Learn more about built-in String methods in the Python docs

String methods

The Python String data type comes with some handy built-in methods that can be used to manipulate bits of text.

Function Description
.upper() Returns a copy of the string with all characters converted to uppercase
.lower() Returns a copy of the string with all characters converted to lowercase
.title() Returns a titlecased version of the string where words start with an uppercase character and the remaining characters are lowercase
.swapcase() Returns a copy of the string with uppercase characters converted to lowercase and vice versa
.strip() Returns a copy of the string with the leading and trailing whitespace characters removed

For the full list, see Python3 docs

Exercise: Manipulating strings

Open the Class1Ex2TextProcessing repl. Let's play with some strings!

How is it going?

Please fill out the feedback form to let me know how I can improve the class!

https://forms.gle/iENMqYvdahR9AP5UA