Day 1
Packages
- lots of useful functions
- add ons
- method 1
- import(package)
- package.function(x)
- method 2
- from package import function
- from package import * to import all from a package
- function(x)
- no need to call "math." before calling a function from the math package
- from package import function
Method 1 example
import math
math.sqrt(x)
- import = function
- math = package imported
- math.sqrt(x) takes the square root of x
- need to specify that you're using the math package by specifying "math." before you call any function
Method 2 example
# if you want to import one function
from math import sqrt
sqrt(x)
# if you want to import all functions from a library
from math import *
sqrt (x)
- specify which function you want to import from the math package
- don't need to call "math." when calling on any functions from the math package
Using IDLE
- What is IDLE?
- a text editor with syntax highlighting (for Python only)
- Using IDLE to write your first program
- Select "File", which should be on the top of your screen, then click "new file"
- Write your code in the new file
- Select "Run", which should be on the top of your screen, then select "Run Module
- If prompted to save the file, just click "ok" and save the file
- "why isn't anything I'm doing showing up on the screen?"
- remember that you have to call a print statement on the things that you've done in order for them to show up on the screen
Variables
- Python reads LINE BY LINE
- If you define a variable twice, it will only take the newest definition
- variable types
- string
- NOT interpreted by Python (Python doesn't try to find meaning from strings)
- integers/int
- whole numbers, can be negative
- float
- numbers with decimal places
- string
User Input
x = input("prompt")
- Gives the user the prompt "prompt", then allows the user to submit user input, then assigns the input to the variable x
- Note: the input will be automatically assigned as a string. If another type is required, you need to transform the input into the type desired
- e.g. int(input("prompt"))
Printing strings + variables together
print("string " , variable , " string")
>>> string variable string
- Separate the different elements using a comma
- Remember to leave spaces if needed, Python does not automatically account for spaces when adding strings and variables together
Homework
Find the most impressive Scratch/Minecraft computing project you've done before. Be ready to present this tomorrow.