parent
7b385e8529
commit
bc5b92a475
@ -0,0 +1,114 @@
|
|||||||
|
# Intro to SQL
|
||||||
|
|
||||||
|
## Video Link
|
||||||
|
|
||||||
|
[Intro to SQL](https://www.youtube.com/watch?v=nZfVXH4CBGg&list=PLdnONIhPScSQqXfMndCJRYWQl-0uApORf&index=22)
|
||||||
|
|
||||||
|
## Lesson Objectives tools
|
||||||
|
1. Connect to Postgres through CLI
|
||||||
|
1. Create a Database
|
||||||
|
1. Create a table
|
||||||
|
1. Insert into the table
|
||||||
|
1. Select from table
|
||||||
|
1. Update the table
|
||||||
|
1. Delete from table
|
||||||
|
|
||||||
|
## Connect to Postgres through CLI
|
||||||
|
|
||||||
|
Since we used homebrew to install, use this command to start up the postgres enviornment (this is like running mongod when we were using MongoDB).
|
||||||
|
|
||||||
|
```
|
||||||
|
brew services start postgresql
|
||||||
|
```
|
||||||
|
|
||||||
|
You can use this command to stop the service if you would like or you can just leave it running in the background at all times (it will disconnect when you shut down your machine).
|
||||||
|
|
||||||
|
```
|
||||||
|
brew services stop postgresql
|
||||||
|
```
|
||||||
|
|
||||||
|
Lets start up the postgres shell:
|
||||||
|
|
||||||
|
```sql
|
||||||
|
psql -l (list all subdatabses)
|
||||||
|
psql db_name (start psql shell, using the sub database db_name)
|
||||||
|
psql (start psql shell connecting to your username)
|
||||||
|
```
|
||||||
|
|
||||||
|
Once inside the `psql` app, you can list the sub databases like this:
|
||||||
|
|
||||||
|
```sql
|
||||||
|
\l
|
||||||
|
```
|
||||||
|
|
||||||
|
## Create a Database
|
||||||
|
|
||||||
|
Like MongoDB, Postgres has "sub-databases":
|
||||||
|
|
||||||
|
```SQL
|
||||||
|
CREATE DATABASE foo; -- create the sub database foo
|
||||||
|
DROP DATABASE foo; -- drop it
|
||||||
|
CREATE DATABASE test_db;
|
||||||
|
\connect test_db; -- connect to the test_db sub database
|
||||||
|
```
|
||||||
|
|
||||||
|
## Data types
|
||||||
|
|
||||||
|
Postgres has the following data types:
|
||||||
|
|
||||||
|
1. int - whole number
|
||||||
|
1. decimal - float/decimal
|
||||||
|
1. bool - boolean
|
||||||
|
1. varchar(n) - small text
|
||||||
|
1. text - large text
|
||||||
|
1. timestamp - date
|
||||||
|
|
||||||
|
## Create a table
|
||||||
|
|
||||||
|
- Instead of collections, we have tables, which are just like a spreadsheet, or grid. Rows are entries, and columns are properties of each row.
|
||||||
|
- Unlike MongoDB, you have to tell Postgres, what data type each column is. It's very 'strict'
|
||||||
|
|
||||||
|
```sql
|
||||||
|
CREATE TABLE foo ( name varchar(20) ); -- create a table called 'foo' with one column called 'name' which is a small text column
|
||||||
|
\dt -- describe your tables
|
||||||
|
DROP TABLE foo; -- drop a table
|
||||||
|
CREATE TABLE users ( id serial, name varchar(20), age int, email varchar(32) ); -- 'test' table has an id column, which is just a number, and a name column
|
||||||
|
\d users; -- describe the columns of the test sub database
|
||||||
|
```
|
||||||
|
|
||||||
|
## Insert into the table
|
||||||
|
|
||||||
|
```sql
|
||||||
|
INSERT INTO users ( name, age, email ) VALUES ( 'Matt', 36, 'matt.huntington@generalassemb.ly'); -- create a row
|
||||||
|
```
|
||||||
|
|
||||||
|
## Select from table
|
||||||
|
|
||||||
|
```sql
|
||||||
|
SELECT name FROM users; -- select all rows from the users table. display only the name column
|
||||||
|
SELECT * FROM users; -- select all rows from the users table. display only the all columns
|
||||||
|
SELECT * FROM users WHERE name = 'Matt'; -- select all rows from the user table where the name column is set to 'Matt'
|
||||||
|
SELECT * FROM users WHERE name LIKE '%Matt%'; -- select all rows from the user table where the name column contains 'Matt'
|
||||||
|
SELECT * FROM users WHERE name = 'Matt' AND email = 'matt.huntington@generalassemb.ly'; -- select all rows from the user table where the name column is set to 'Matt' AND the email column is set to matt.huntington@generalassemb.ly
|
||||||
|
SELECT * FROM users WHERE name = 'Matt' OR email = 'matt.huntington@generalassemb.ly'; -- select all rows from the user table where either the name column is set to 'Matt' OR the email column is set to matt.huntington@generalassemb.ly
|
||||||
|
SELECT * FROM users WHERE age = 36; -- select all rows from the user table where the age column is set to 36
|
||||||
|
SELECT * FROM users WHERE age != 16; -- select all rows from the user table where the age column is not set to 16
|
||||||
|
SELECT * FROM users WHERE age > 26; -- select all rows from the user table where the age column is greater than 26
|
||||||
|
SELECT * FROM users WHERE age < 46; -- select all rows from the user table where the age column is less than 26
|
||||||
|
SELECT * FROM users WHERE age <= 36; -- select all rows from the user table where the age column is less than or equal to 36
|
||||||
|
SELECT * FROM users WHERE age >= 36; -- select all rows from the user table where the age column is greater than or equal to 36
|
||||||
|
SELECT * FROM users WHERE age IS NULL; -- select all rows from the user table where the age column has no value
|
||||||
|
SELECT * FROM users WHERE age IS NOT NULL; -- select all rows from the user table where the age column has any value
|
||||||
|
```
|
||||||
|
|
||||||
|
## Update the table
|
||||||
|
|
||||||
|
```sql
|
||||||
|
UPDATE users SET name = 'Matthew' WHERE id = 1; -- update the users table. Set the name column to 'Matthew' for every row that has the id column set to 1
|
||||||
|
```
|
||||||
|
|
||||||
|
## Delete from table
|
||||||
|
|
||||||
|
```sql
|
||||||
|
DELETE FROM users WHERE id = 1; -- delete all rows from the users table that have the id column set to 1
|
||||||
|
```
|
||||||
@ -0,0 +1,84 @@
|
|||||||
|
# Intermediate SQL
|
||||||
|
|
||||||
|
## Video Link
|
||||||
|
|
||||||
|
[Intermediate SQL](https://www.youtube.com/watch?v=cYZVvWJu5Qs&list=PLdnONIhPScSQqXfMndCJRYWQl-0uApORf&index=22)
|
||||||
|
|
||||||
|
## Lesson Objectives tools1. Alter a table
|
||||||
|
|
||||||
|
1. Alter a table
|
||||||
|
1. Limit
|
||||||
|
1. Sorting
|
||||||
|
1. Aggregation
|
||||||
|
1. Joins
|
||||||
|
|
||||||
|
## Alter a table
|
||||||
|
|
||||||
|
```sql
|
||||||
|
ALTER TABLE users ADD COLUMN test VARCHAR(20); -- add an test string column
|
||||||
|
ALTER TABLE users DROP COLUMN test; -- drop the test column
|
||||||
|
ALTER TABLE users RENAME name TO first_name -- rename a column
|
||||||
|
ALTER TABLE users ADD COLUMN id serial PRIMARY KEY; -- add an id column that increments with each new row
|
||||||
|
ALTER TABLE users RENAME TO people; -- rename a table
|
||||||
|
ALTER TABLE people ALTER COLUMN first_name TYPE text; -- chang the data type of a column
|
||||||
|
```
|
||||||
|
|
||||||
|
## Limit
|
||||||
|
|
||||||
|
```sql
|
||||||
|
SELECT * FROM people LIMIT 1; -- select all rows from people table, but show only the first column
|
||||||
|
SELECT * FROM people LIMIT 1 OFFSET 1; -- select all rows from people table, but show only one column. Skip the first row
|
||||||
|
```
|
||||||
|
|
||||||
|
## Sorting
|
||||||
|
|
||||||
|
```sql
|
||||||
|
SELECT * FROM people ORDER BY first_name ASC; -- select all rows from people table, order by name alphabetically
|
||||||
|
SELECT * FROM people ORDER BY first_name DESC; -- select all rows from people table, order by name reverse alphabetically
|
||||||
|
SELECT * FROM people ORDER BY age ASC; -- select all rows from people table, order by age ascending
|
||||||
|
SELECT * FROM people ORDER BY age DESC; -- select all rows from people table, order by age descending
|
||||||
|
```
|
||||||
|
|
||||||
|
## Aggregation
|
||||||
|
|
||||||
|
```sql
|
||||||
|
SELECT SUM(age), first_name FROM people GROUP BY first_name; -- divide all rows into groups by name. Show the SUM of the ages of each group. Also show what name each group has
|
||||||
|
SELECT AVG(age), first_name FROM people GROUP BY first_name; -- divide all rows into groups by name. Show the AVG of the ages of each group. Also show what name each group has
|
||||||
|
SELECT MIN(age), first_name FROM people GROUP BY first_name; -- divide all rows into groups by name. Show the MAX of the ages of each group. Also show what name each group has
|
||||||
|
SELECT MAX(age), first_name FROM people GROUP BY first_name; -- divide all rows into groups by name. Show the MIN of the ages of each group. Also show what name each group has
|
||||||
|
SELECT COUNT(age), first_name FROM people GROUP BY first_name; -- divide all rows into groups by name. Show how many rows have a value in the age column. Also show what name each group has
|
||||||
|
SELECT COUNT(*), first_name FROM people GROUP BY first_name; -- divide all rows into groups by name. Show the number of rows in each group. Also show what name each group has
|
||||||
|
SELECT array_agg(first_name), age FROM people GROUP BY age; -- divide all rows into groups by age. List the names in each group and show what age each group has
|
||||||
|
```
|
||||||
|
|
||||||
|
## JOINS
|
||||||
|
|
||||||
|
```sql
|
||||||
|
SELECT * FROM people CROSS JOIN companies;
|
||||||
|
SELECT * FROM people JOIN companies ON people.employer_id = companies.id -- find all people who have an employer_id column set and show which company they work for
|
||||||
|
SELECT * FROM people LEFT JOIN companies ON people.employer_id = companies.id -- find all people have an employer_id column set and show which company they work for. In addition to this set, add on all people who do not have an employer_id column set
|
||||||
|
SELECT * FROM people RIGHT JOIN companies ON people.employer_id = companies.id -- find all people have an employer_id column set and show which company they work for. In addition to this set, add on all companies who do not have any people with employer_id columns set to the company's id column
|
||||||
|
SELECT * FROM people FULL OUTER JOIN companies ON people.employer_id = companies.id; -- find all people have an employer_id column set and show which company they work for. In addition to this set, add on all companies who do not have any people with employer_id columns set to the company's id column and all people who do not have an employer_id column set
|
||||||
|
```
|
||||||
|
|
||||||
|
## Combining Statments
|
||||||
|
|
||||||
|
You can combine `WHERE`, `LIMIT`, `ORDER BY`, `OFFSET` with your `JOIN` statements:
|
||||||
|
|
||||||
|
```sql
|
||||||
|
SELECT * FROM people LEFT JOIN companies ON people.employer_id = companies.id WHERE first_name LIKE 'M%' ORDER BY age ASC LIMIT 2 OFFSET 1;
|
||||||
|
```
|
||||||
|
|
||||||
|
You can even add `GROUP BY` and aggregation functions too:
|
||||||
|
|
||||||
|
```sql
|
||||||
|
SELECT AVG(age), first_name FROM people LEFT JOIN companies ON people.employer_id = companies.id WHERE first_name LIKE 'M%' GROUP BY first_name ORDER BY avg ASC LIMIT 2 OFFSET 1;
|
||||||
|
```
|
||||||
|
|
||||||
|
The order where you add these in the statement matters. The following breaks (`GROUP BY` must come before `ORDER BY`, `LIMIT`, `OFFSET`):
|
||||||
|
|
||||||
|
```sql
|
||||||
|
SELECT AVG(age), first_name FROM people LEFT JOIN companies ON people.employer_id = companies.id WHERE first_name LIKE 'M%' ORDER BY avg ASC LIMIT 2 OFFSET 1 GROUP BY first_name;
|
||||||
|
```
|
||||||
|
|
||||||
|
If it breaks, just try reordering until it works. You can also research the order in which they need to come in the statement
|
||||||
@ -0,0 +1,106 @@
|
|||||||
|
# PostGres - Advanced
|
||||||
|
|
||||||
|
## Lesson Objectives - important
|
||||||
|
1. Joins
|
||||||
|
1. Linking Tables
|
||||||
|
1. Alias
|
||||||
|
1. Indexes
|
||||||
|
1. Constraints
|
||||||
|
|
||||||
|
## Lesson Objectives - good to know about
|
||||||
|
1. EER Diagrams
|
||||||
|
1. Unions
|
||||||
|
1. Truncate
|
||||||
|
1. Triggers
|
||||||
|
1. Views
|
||||||
|
1. Functions/Stored Procedures
|
||||||
|
1. Transactions
|
||||||
|
1. Locks
|
||||||
|
1. Privileges
|
||||||
|
1. Denormalization
|
||||||
|
1. Excel -> CSV -> MySQL
|
||||||
|
|
||||||
|
## Important
|
||||||
|
|
||||||
|
### Joins
|
||||||
|
1. Cross Join
|
||||||
|
1. Inner Join
|
||||||
|
1. Left Join
|
||||||
|
1. Right Join
|
||||||
|
1. Full Join
|
||||||
|
```sql
|
||||||
|
SELECT table1.column1, table2.column2
|
||||||
|
FROM table1
|
||||||
|
INNER JOIN table2
|
||||||
|
ON table1.common_filed = table2.common_field;
|
||||||
|
```
|
||||||
|
|
||||||
|
### Linking Tables
|
||||||
|
1. `http://code.tutsplus.com/articles/sql-for-beginners-part-3-database-relationships--net-8561`
|
||||||
|
1. One to One Relationships
|
||||||
|
- each user has one address
|
||||||
|
- only one person at that address
|
||||||
|
1. One to Many/Many to One Relationships
|
||||||
|
- customer has many orders
|
||||||
|
1. Many to Many Relationships
|
||||||
|
- actors and movies
|
||||||
|
1. Self Referncing Relationships
|
||||||
|
- customer referral
|
||||||
|
|
||||||
|
### Alias
|
||||||
|
```sql
|
||||||
|
SELECT t1.column1 as col1, t2.column2 as col2
|
||||||
|
FROM table1 as t1
|
||||||
|
INNER JOIN table2 as t2
|
||||||
|
ON t1.common_filed = t2.common_field;
|
||||||
|
```
|
||||||
|
|
||||||
|
### Indexes
|
||||||
|
1. `CREATE INDEX index_name ON table_name (column_name);`
|
||||||
|
1. `CREATE INDEX index_name ON table_name (column1_name, column2_name);`
|
||||||
|
1. Primary Key
|
||||||
|
|
||||||
|
### Constraints
|
||||||
|
1. NOT NULL
|
||||||
|
1. Unique
|
||||||
|
1. Foreign Keys
|
||||||
|
```sql
|
||||||
|
CREATE TABLE companies(
|
||||||
|
id SERIAL PRIMARY KEY,
|
||||||
|
name VARCHAR(16) NOT NULL UNIQUE,
|
||||||
|
city VARCHAR(16)
|
||||||
|
);
|
||||||
|
INSERT INTO companies ( city ) VALUES ('Palo Alto');
|
||||||
|
CREATE TABLE people(
|
||||||
|
id INT PRIMARY KEY,
|
||||||
|
name VARCHAR(16) NOT NULL,
|
||||||
|
email VARCHAR(32) NOT NULL UNIQUE,
|
||||||
|
company_id INT REFERENCES companies(id)
|
||||||
|
);
|
||||||
|
```
|
||||||
|
|
||||||
|
## Good to Know About
|
||||||
|
|
||||||
|
### EER Diagrams
|
||||||
|
|
||||||
|
### Unions
|
||||||
|
|
||||||
|
### Truncate
|
||||||
|
|
||||||
|
### Triggers
|
||||||
|
|
||||||
|
### Views
|
||||||
|
|
||||||
|
### Functions/Stored Procedures
|
||||||
|
|
||||||
|
### Transactions
|
||||||
|
|
||||||
|
### Locks
|
||||||
|
|
||||||
|
### Privileges
|
||||||
|
|
||||||
|
### Denormalization
|
||||||
|
|
||||||
|
### Excel -> CSV -> MySQL
|
||||||
|
|
||||||
|
### SQL Injection
|
||||||
@ -0,0 +1,79 @@
|
|||||||
|
# MySQL Bootcamp
|
||||||
|
|
||||||
|
## Lesson Objectives
|
||||||
|
|
||||||
|
1. What is a DB?
|
||||||
|
1. What are the different ways to store data?
|
||||||
|
1. What are the advantages of a database?
|
||||||
|
1. What does SQL Stand for? What is it?
|
||||||
|
1. Diagram MySQL structure
|
||||||
|
|
||||||
|
```sql
|
||||||
|
-- CREATE DB/TABLES
|
||||||
|
SHOW DATABASES;
|
||||||
|
CREATE DATABASE our_database_name;
|
||||||
|
USE our_database_name;
|
||||||
|
CREATE TABLE people (first_name VARCHAR(20), age INT);
|
||||||
|
DESCRIBE people;
|
||||||
|
|
||||||
|
-- CRUD
|
||||||
|
INSERT INTO people (first_name, age) VALUES ('Matt' , 34);
|
||||||
|
SELECT age FROM people;
|
||||||
|
SELECT * FROM people;
|
||||||
|
UPDATE people SET weight = 300 WHERE first_name = 'Bill';
|
||||||
|
DELETE FROM people WHERE first_name = "Bill";
|
||||||
|
|
||||||
|
-- OPERATORS
|
||||||
|
SELECT * FROM people WHERE age != 63;
|
||||||
|
SELECT * FROM people WHERE age < 63;
|
||||||
|
SELECT * FROM people WHERE age > 63;
|
||||||
|
SELECT * FROM people WHERE age >= 63;
|
||||||
|
SELECT * FROM people WHERE age <= 63;
|
||||||
|
SELECT * FROM people WHERE first_name first_name LIKE "%Charlie%";
|
||||||
|
SELECT * FROM people WHERE first_name NOT LIKE "%Charlie%";
|
||||||
|
SELECT * FROM people WHERE age IS NULL;
|
||||||
|
SELECT * FROM people WHERE age IS NOT NULL;
|
||||||
|
|
||||||
|
|
||||||
|
-- AND/OR
|
||||||
|
SELECT * FROM people WHERE first_name = 'Matt' AND age = 43;
|
||||||
|
SELECT * FROM people WHERE first_name = 'Matt' OR age = 49;
|
||||||
|
|
||||||
|
-- ORDER
|
||||||
|
SELECT * FROM people ORDER BY age DESC;
|
||||||
|
SELECT * FROM people ORDER BY first_name DESC;
|
||||||
|
SELECT * FROM people ORDER BY age ASC LIMIT 2;
|
||||||
|
SELECT * FROM people ORDER BY age ASC LIMIT 2 OFFSET 1;
|
||||||
|
SELECT * FROM people ORDER BY age DESC, first_name ASC;
|
||||||
|
|
||||||
|
-- ALTER TABLE
|
||||||
|
ALTER TABLE people ADD COLUMN weight FLOAT;
|
||||||
|
ALTER TABLE people DROP COLUMN height;
|
||||||
|
ALTER TABLE people MODIFY COLUMN height FLOAT;
|
||||||
|
|
||||||
|
ALTER TABLE people ADD COLUMN height FLOAT AFTER first_name;
|
||||||
|
ALTER TABLE people MODIFY COLUMN height FLOAT AFTER age;
|
||||||
|
|
||||||
|
ALTER TABLE people ADD COLUMN id INT FIRST;
|
||||||
|
ALTER TABLE people MODIFY COLUMN height FLOAT FIRST;
|
||||||
|
|
||||||
|
ALTER TABLE people ADD COLUMN dob DATETIME;
|
||||||
|
ALTER TABLE people CHANGE dob date_of_birth DATETIME;
|
||||||
|
|
||||||
|
ALTER TABLE people ADD COLUMN id INT NOT NULL AUTO_INCREMENT PRIMARY KEY FIRST;
|
||||||
|
|
||||||
|
-- AGGREGATION
|
||||||
|
SELECT COUNT(*), age FROM people GROUP BY age;
|
||||||
|
SELECT SUM(salary), age FROM people GROUP BY age;
|
||||||
|
SELECT AVG(salary), age FROM people GROUP BY age;
|
||||||
|
SELECT MIN(salary), age FROM people GROUP BY age;
|
||||||
|
SELECT MAX(salary), age FROM people GROUP BY age;
|
||||||
|
SELECT GROUP_CONCAT(first_name), age FROM people GROUP BY age;
|
||||||
|
SELECT GROUP_CONCAT(first_name), age, height FROM people GROUP BY age, height;
|
||||||
|
|
||||||
|
-- JOINS
|
||||||
|
SELECT * FROM people JOIN companies ON people.employer_id = companies.id;
|
||||||
|
SELECT * from people JOIN companies;
|
||||||
|
SELECT * FROM people RIGHT JOIN companies ON people.employer_id = companies.id;
|
||||||
|
SELECT * FROM people LEFT JOIN companies ON people.employer_id = companies.id;
|
||||||
|
```
|
||||||
@ -0,0 +1,234 @@
|
|||||||
|
|
||||||
|
|
||||||
|
# Intro to Python pt. 1
|
||||||
|
|
||||||
|
## Video Link
|
||||||
|
|
||||||
|
[Intro to Python pt 1](https://generalassembly.wistia.com/medias/zk66z0xm5x)
|
||||||
|
|
||||||
|
## Lesson Objectives
|
||||||
|
|
||||||
|
1. Print a message
|
||||||
|
1. Add a comment
|
||||||
|
1. Create a variable and assign it a value
|
||||||
|
1. Explain the different data types
|
||||||
|
1. Perform calculations with variables
|
||||||
|
1. Use string operations
|
||||||
|
1. Create a list
|
||||||
|
1. Access an element of a list
|
||||||
|
1. Use conditional statements to perfom a set of commands depending on the situation
|
||||||
|
|
||||||
|
|
||||||
|
## Set up
|
||||||
|
|
||||||
|
In today's `student_examples` directory, touch a file `program.py`
|
||||||
|
|
||||||
|
|
||||||
|
## Running files
|
||||||
|
|
||||||
|
These notes are for python 3.x, so use `python3 <YOUR FILENAME>.py` to run your files
|
||||||
|
|
||||||
|
## Print a message
|
||||||
|
|
||||||
|
You can print a message to the user
|
||||||
|
|
||||||
|
```python
|
||||||
|
print("hello!")
|
||||||
|
```
|
||||||
|
|
||||||
|
## Add a comment
|
||||||
|
|
||||||
|
- Comments let you summarize what you're doing
|
||||||
|
- They don't get executed
|
||||||
|
|
||||||
|
```python
|
||||||
|
# this will not be executed
|
||||||
|
```
|
||||||
|
|
||||||
|
- Python doesn't have a specific syntax for multi-line comments so we just use a # for each line.
|
||||||
|
|
||||||
|
```python
|
||||||
|
# to do multiple line comments
|
||||||
|
# you have to put a hash at the start of every
|
||||||
|
# line
|
||||||
|
```
|
||||||
|
|
||||||
|
## Create a variable and assign it a value
|
||||||
|
|
||||||
|
```python
|
||||||
|
a = "hello"
|
||||||
|
print(a) ##print the value of the variable 'a'
|
||||||
|
```
|
||||||
|
|
||||||
|
## Data Types in Python
|
||||||
|
|
||||||
|
There are lots of different types of data that you can use in python
|
||||||
|
|
||||||
|
- String (text)
|
||||||
|
- Integers (whole numbers)
|
||||||
|
- Float (decimal numbers)
|
||||||
|
- Booleans (True/False)
|
||||||
|
|
||||||
|
You can convert one data type to another
|
||||||
|
|
||||||
|
```python
|
||||||
|
a = str(1) #a = "1"
|
||||||
|
b = int("5") #b = 5
|
||||||
|
c = float(4) #c = 4.0
|
||||||
|
d = int(5.7) #d = 5
|
||||||
|
```
|
||||||
|
|
||||||
|
## Performing Calculations
|
||||||
|
|
||||||
|
```python
|
||||||
|
a = 1
|
||||||
|
b = a + 1 #b = 2
|
||||||
|
c = b * 3 #c = 6
|
||||||
|
d = c - 1 #d = 5
|
||||||
|
e = d / 2 #e = 2.5
|
||||||
|
f = d ** 2 #exponent: f = 25
|
||||||
|
```
|
||||||
|
|
||||||
|
## String Operations
|
||||||
|
|
||||||
|
```python
|
||||||
|
a = "first string"
|
||||||
|
b = "second string"
|
||||||
|
c = a + " " + b
|
||||||
|
```
|
||||||
|
|
||||||
|
## Lists
|
||||||
|
|
||||||
|
You can create lists of things
|
||||||
|
|
||||||
|
```python
|
||||||
|
a = [1, 5, "some string", True, 5.6]
|
||||||
|
```
|
||||||
|
|
||||||
|
You can even have lists of lists
|
||||||
|
|
||||||
|
```python
|
||||||
|
a = [
|
||||||
|
[1, 2, 3], #first row
|
||||||
|
[4, 5, 6], #second row
|
||||||
|
[7, 8, 9], #third row
|
||||||
|
[10] #fourth row
|
||||||
|
]
|
||||||
|
```
|
||||||
|
|
||||||
|
You can conceptualize a list of lists however you want
|
||||||
|
|
||||||
|
### ACTIVITY
|
||||||
|
|
||||||
|
If you want to make the previous example have columns instead of rows, do you need to change anything?
|
||||||
|
|
||||||
|
## Access an element of a list
|
||||||
|
|
||||||
|
Lists have elements stored at numerical indexes, starting at 0
|
||||||
|
|
||||||
|
```python
|
||||||
|
a = [1, 5, "some string", True, 5.6]
|
||||||
|
print(a[0]) #1
|
||||||
|
print(a[1]) #5
|
||||||
|
print(a[4]) #5.6
|
||||||
|
```
|
||||||
|
|
||||||
|
## Dictionaries
|
||||||
|
|
||||||
|
You can create JS style objects in python called dictionaries
|
||||||
|
- Dictionaries use array access syntax:
|
||||||
|
|
||||||
|
```python
|
||||||
|
my_car = {
|
||||||
|
"brand": "Ford",
|
||||||
|
"model": "Mustang",
|
||||||
|
"year": 1964
|
||||||
|
}
|
||||||
|
print(my_car["brand"])
|
||||||
|
```
|
||||||
|
|
||||||
|
You can also have lists in dictionaries and dictionaries in lists!
|
||||||
|
|
||||||
|
```python
|
||||||
|
a = [
|
||||||
|
{
|
||||||
|
"brand": "Ford",
|
||||||
|
"model": "Mustang",
|
||||||
|
"year": 1964
|
||||||
|
},
|
||||||
|
[4, 5, 6],
|
||||||
|
[7, 8, 9],
|
||||||
|
[10]
|
||||||
|
]
|
||||||
|
print(a)
|
||||||
|
|
||||||
|
b = {
|
||||||
|
"firstGrade": ["Bobby", "Kyle", "Suzy"],
|
||||||
|
"secondGrade": ["Jennifer", "Jasmine", "Javier"],
|
||||||
|
"thirdGrade": "Nobody, they all failed last year!"
|
||||||
|
}
|
||||||
|
print(b)
|
||||||
|
```
|
||||||
|
|
||||||
|
## Perform a set of commands depending on a situation
|
||||||
|
|
||||||
|
```python
|
||||||
|
a = 22
|
||||||
|
if a < 10:
|
||||||
|
print("a is less than 10")
|
||||||
|
elif a == 10:
|
||||||
|
print("a is 10")
|
||||||
|
else:
|
||||||
|
print("a is greater than 10")
|
||||||
|
```
|
||||||
|
|
||||||
|
The conditions can be
|
||||||
|
|
||||||
|
- `<` less than
|
||||||
|
- `>` greater than
|
||||||
|
- `<=` less than or equal to
|
||||||
|
- `>=` greater than or equal to
|
||||||
|
- `==` an exact match
|
||||||
|
- `!=` not equal to
|
||||||
|
|
||||||
|
You can also compare strings:
|
||||||
|
|
||||||
|
```python
|
||||||
|
a = 'oh hai!'
|
||||||
|
if a == 'oh hai!':
|
||||||
|
print('this works')
|
||||||
|
```
|
||||||
|
|
||||||
|
You can combine conditional statements:
|
||||||
|
|
||||||
|
check to see if both conditions are met:
|
||||||
|
|
||||||
|
```python
|
||||||
|
a = 1
|
||||||
|
b = 2
|
||||||
|
if a == 1 and b == 2:
|
||||||
|
print('y') # will print only when both a==1 AND b==2
|
||||||
|
else:
|
||||||
|
print('n') # will print if either condition is false
|
||||||
|
|
||||||
|
if a == 0 and b == 2:
|
||||||
|
print('y') # will print only when both a==1 AND b==2
|
||||||
|
else:
|
||||||
|
print('n') # will print if either condition is false
|
||||||
|
```
|
||||||
|
|
||||||
|
check to see if either condition is met:
|
||||||
|
|
||||||
|
```python
|
||||||
|
a = 2
|
||||||
|
b = 2
|
||||||
|
if a == 1 or b == 2:
|
||||||
|
print('y') # will print when either a==1 OR b==2
|
||||||
|
else:
|
||||||
|
print('n') # will print if both conditions are false
|
||||||
|
|
||||||
|
if a == 1 or b == 1:
|
||||||
|
print('y') # will print when either a==1 OR b==2
|
||||||
|
else:
|
||||||
|
print('n') # will print if both conditions are false
|
||||||
|
```
|
||||||
@ -0,0 +1,195 @@
|
|||||||
|
# Intro to Python pt. 2
|
||||||
|
|
||||||
|
## Video Link
|
||||||
|
|
||||||
|
[Intro to Python pt 2](https://generalassembly.wistia.com/medias/gbn51kpqgh)
|
||||||
|
|
||||||
|
## Lesson Objectives
|
||||||
|
|
||||||
|
1. Get user input
|
||||||
|
1. Repeatedly perform a set of commands
|
||||||
|
1. Use a for loop
|
||||||
|
1. Define a function
|
||||||
|
1. Create a class for an object
|
||||||
|
1. Have a class inherit from another
|
||||||
|
1. Create a factory for objects
|
||||||
|
|
||||||
|
|
||||||
|
## Get user input
|
||||||
|
|
||||||
|
You can get user input from the command like so:
|
||||||
|
|
||||||
|
```python
|
||||||
|
user_input = input("Please enter your name: ")
|
||||||
|
print("Hello, " + user_input + "!")
|
||||||
|
```
|
||||||
|
|
||||||
|
## Repeatedly perform a set of commands
|
||||||
|
|
||||||
|
```python
|
||||||
|
a = 10
|
||||||
|
while a < 20:
|
||||||
|
print("the value of a is currently: " + str(a))
|
||||||
|
a += 1
|
||||||
|
```
|
||||||
|
|
||||||
|
### ACTIVITY
|
||||||
|
|
||||||
|
1. Write a program that models this flow chart:
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
|
||||||
|
## Use a for loop
|
||||||
|
|
||||||
|
The process of looping through an array can be simplified with a `for` loop:
|
||||||
|
|
||||||
|
```python
|
||||||
|
foods = ['hot dogs', 'beer', 'bald eagles']
|
||||||
|
for food in foods:
|
||||||
|
print(food)
|
||||||
|
```
|
||||||
|
|
||||||
|
You can loop through a set of numbers using a `range`
|
||||||
|
|
||||||
|
```python
|
||||||
|
for x in range(0, 3):
|
||||||
|
print(x)
|
||||||
|
```
|
||||||
|
|
||||||
|
### ACTIVITIES
|
||||||
|
|
||||||
|
1. Given the following list [70, 95, 97, 55, 3, 24, 89, 97, 84, 11]
|
||||||
|
- Write a program that loops through each value in the list and prints it
|
||||||
|
- Write a program that loops through each value in the list and adds them all together
|
||||||
|
- Write a program that loops through each value in the list and prints the average
|
||||||
|
- Write a program that loops through each value in the list and prints the minimum
|
||||||
|
- Write a program that loops through each value in the list and prints the maximum
|
||||||
|
|
||||||
|
1. Combine all the programs from the previous step into one program that asks the user what operation they would like to do
|
||||||
|
|
||||||
|
1. Alter the last program so that it performs the operations for only numbers that are greater than a number specified by the user
|
||||||
|
|
||||||
|
## Define a function
|
||||||
|
|
||||||
|
If you have a routine that you run over and over again, you can define your own function:
|
||||||
|
|
||||||
|
```python
|
||||||
|
def greet():
|
||||||
|
print('hi')
|
||||||
|
|
||||||
|
greet()
|
||||||
|
```
|
||||||
|
|
||||||
|
Functions can take parameters which alter their functionality:
|
||||||
|
|
||||||
|
```python
|
||||||
|
def greet(name):
|
||||||
|
print('hi, ' + name)
|
||||||
|
|
||||||
|
greet('bob')
|
||||||
|
```
|
||||||
|
|
||||||
|
Functions can return values:
|
||||||
|
|
||||||
|
```python
|
||||||
|
def add(value1, value2):
|
||||||
|
return value1 + value2
|
||||||
|
|
||||||
|
print(add(1,3))
|
||||||
|
```
|
||||||
|
|
||||||
|
### ACTIVITIES
|
||||||
|
|
||||||
|
Create a calculator program that continually asks a user what operations they want to perform, until the user says 'quit'
|
||||||
|
|
||||||
|
## Create a class for an object
|
||||||
|
|
||||||
|
You can use a `class` or blueprint for objects that you'll use
|
||||||
|
|
||||||
|
```python
|
||||||
|
class Person:
|
||||||
|
def __init__(self, name, age):
|
||||||
|
self.name = name
|
||||||
|
self.age = age
|
||||||
|
|
||||||
|
def greet(self):
|
||||||
|
print("Hello, my name is " + self.name + ". My age is " + str(self.age))
|
||||||
|
|
||||||
|
me = Person("Hunter", 29)
|
||||||
|
me.greet()
|
||||||
|
sally = Person("Sally", 53)
|
||||||
|
sally.greet()
|
||||||
|
```
|
||||||
|
|
||||||
|
- `__init__` is a function that gets called when a new object is created. Make sure you use two underscores on either side of the `init` or it will break!
|
||||||
|
- `self` is the object that's created
|
||||||
|
|
||||||
|
## Have a class inherit from another
|
||||||
|
|
||||||
|
```python
|
||||||
|
class Person:
|
||||||
|
def __init__(self, name, age):
|
||||||
|
self.name = name
|
||||||
|
self.age = age
|
||||||
|
|
||||||
|
def greet(self):
|
||||||
|
print("Hello, my name is " + self.name + ". My age is " + str(self.age))
|
||||||
|
|
||||||
|
def work(self):
|
||||||
|
print("Boring...")
|
||||||
|
|
||||||
|
class SuperHero(Person): # tell it to inherit from Person
|
||||||
|
def __init__(self, name, age, powers):
|
||||||
|
super().__init__(name,age) # call Person's __init__()
|
||||||
|
self.powers = powers
|
||||||
|
|
||||||
|
def greet(self):
|
||||||
|
super().greet() # call Person's greet()
|
||||||
|
self.listPowers()
|
||||||
|
|
||||||
|
def listPowers(self):
|
||||||
|
for power in self.powers:
|
||||||
|
print(power)
|
||||||
|
|
||||||
|
def work(self): # override Person's work()
|
||||||
|
print("To action!")
|
||||||
|
|
||||||
|
superman = SuperHero('Clark Kent', 200, ['flight', 'strength', 'invulnerability'])
|
||||||
|
|
||||||
|
superman.greet()
|
||||||
|
superman.work()
|
||||||
|
```
|
||||||
|
|
||||||
|
## Create a factory for objects
|
||||||
|
|
||||||
|
```python
|
||||||
|
class Car:
|
||||||
|
def __init__(self, maker, model, serial):
|
||||||
|
self.maker = maker
|
||||||
|
self.model = model
|
||||||
|
self.serial = serial
|
||||||
|
|
||||||
|
class CarFactory:
|
||||||
|
def __init__(self, name):
|
||||||
|
self.name = name
|
||||||
|
self.cars = []
|
||||||
|
|
||||||
|
def makeCar(self, model):
|
||||||
|
self.cars.append(Car(self.name, model, len(self.cars)))
|
||||||
|
|
||||||
|
def listCars(self):
|
||||||
|
for car in self.cars:
|
||||||
|
print(car.maker + " " + car.model + " serial#: " + str(car.serial))
|
||||||
|
|
||||||
|
def findCar(self, serial):
|
||||||
|
for car in self.cars:
|
||||||
|
if(car.serial == serial):
|
||||||
|
return car
|
||||||
|
|
||||||
|
toyota = CarFactory('Toyota')
|
||||||
|
toyota.makeCar('Prius')
|
||||||
|
toyota.makeCar('Rav 4')
|
||||||
|
toyota.listCars()
|
||||||
|
print(toyota.findCar(1).model)
|
||||||
|
```
|
||||||
Loading…
Reference in new issue