Python
Python
Python
Prerequisites
1.1 Numbers
- Addition (+): 10 + 5 = 15
- Subtraction (-): 10 - 5 = 5
- Multiplication (*): 10 * 5 = 50
- Power (**): 10**5 = 100000 (10*10*10*10*10)
- Division (/): 10 / 5 = 2.0, 14 / 5 = 2.8 (note: this always returns a float)
- Integer Division (//): 10 // 5 = 2, 14 // 5 = 2 (this always returns an int)
- Modular Division (%): 10 % 5 = 0 (the remainder of a division)
- Operations can be combined to make bigger operations.
+ Order of operations: () > ** > * > / > % > + -
Examples: 8-1+2+5 = 14, 8-(1+2)+5 = 10, 3*2**3 = 3*8 = 24
II. Introduction to String
2.1. Strings
- Concatenation (+):
+ Example: "hey" + "there" = "heythere"
- Multiplication (*):
+ Example: "cool"*3 = "coolcoolcool"
+ Note: number to multiply must be an int.
- Indexing:
“string”[0] = “s”, “string”[2] = “r”, “string”[1:4] = "tri"
- Like numbers, operations can be combined together.
+ Order of operations: * > +
Examples: "a"*6 + "bcd" = "aaaaaabcd"
III. Introduction to Variables
3.1. Variables
- Assign:
+ var='value', a=10, longvarname="a string", pi=3.14, ...
- Each variable must contain some value in order to be operated on.
+ Examples:
a=6, b=5: a + b = 6 + 5 = 11
a='its', b='fun': a + b = 'its' + 'fun' = 'itsfun'
a='nice', b=3: a*b = 'nicenicenice'
3.2. Variable Operations
- In order to make a script file, you need to make sure file name
extensions are enabled.
+ Google "How to enable file name extensions" and do what the guide
says, it'll be handy later on.
4.1. Scripts
- Script files run from top to bottom, however, once you've run a script in
IDLE, you may notice that you're not seeing any output whatsoever.
- print() solves this issue by printing whatever you place in () so you can see.
+ For example: # This prints a string:
print('My Dog')
variable='nice'
# Also supports variables:
print(variable)
# Numbers work, too:
print(3.14)
V. Your first command - print()
- Using commas as separators, print() can also be used for printing more
complex combinations.
+ Examples: print('Pi is',3.14)
a=5
print('We have',a,'ice cubes')
a=8
print('We now have',a,'ice cubes')
V. Your first command - print()
- The input() command writes a quote on the screen, and asks for your
input.
Example: input('write your thing here:')
- What you've inputted will be the input command's result, and can be
saved into variables or operated on.
VI. The input() command
Example:
# This code adds 'I say: ' to what you've written,
# and prints it:
x=input('Please add something here')
prints('I say: '+x)
- Booleans are types of data that only has 2 values: True or False.
- Booleans are encountered when conditions are made.
- Linking Conditions: Conditions can be linked with and, or, not to
make better conditions.
8.2. The if command
9.1 Lists
- List is basically a list of data. Syntax: [<any data, seperated by a colon (,)>]
Examples:
[1,2,3,4]
['this','is','a','list','of','strings']
['this','list','has','strings','and','numbers',101]
['list of lists work, too',['like this',20]]
9.2. List Operations
- Editing in an index:
x=[1,2,3,4]
x[1] = 6
# x is now [1,6,3,4]
x[-1] = 3
- Insert (inserts a value at the index, pushing everything else to the right)
x=[1,6,4]
# x.insert(index,value)
x.insert(2,5)
# x is now [1,6,5,4]
9.2. List Operations
- Pop (take a value with an index k from the list out of that list)
x=[10,20,30,40]
m=x.pop(2)
# 30 (x[2]) is now taken out of the list.
# assigning m as the operation
# actually stores the result into m
# which means m is now 30.
# and list after pop is [10,20,40]
9.3. Tuples
10.1. Set
- A Set is a collection of items. Unlike list and tuples, all items in a set must be
unique, and they are not ordered, which means indexing is not allowed in a set.
- Syntax: {<items go here>}
- Examples:
{1,2,4}
{'item',2,3,'another item'}
{{'a set can contain a set'},'this is true'}
Note: to make an empty set, use set(), not {}.
Reasons why will be mentioned in the Dictionaries section.
10.2. Set Operations
- Difference (-): For A-B: Finds the set in set A, but not in set B
# this returns {3,4}
{2,3,4,5}-{2,5}
# returns {1,2,3}
{1,2,3}-{4,5}
- Symmetric Difference(^): The set with items in one of the two sets, but not
both: # {1,2,4,5}
{1,2,3}^{3,4,5}
# {1,2,3,4,5,6}
{1,2,3}^{4,5,6}
XI. Dictionaries
11.1. Dictionary
- The act of changing a data from one type to another is considered a type
conversion.
- To do this: simply use the type's name, and cover the value you wish to
convert with curly brackets ().
Example:
# Converts 20 in string form into integer
int('20')
# Converts 40 in integer form to float:
float(40)
# the key difference between int and float
# float always have a dot in its value.
XII . Type Conversions
Note: if nothing is set as the convert value, it creates an empty value of that
type.
int() = 0, set() = {} (empty set), ...
type names:
int: Integer
float: Floating point values
str: String
dict: Dictionary
set: Set
XIII. Loops: for and while
longfunc()
# param1 is now 'bone', param2 is default
longfunc('bone')
# param2 is now 'doggo', param1 is default
longfunc(param2='doggo')
# param1 is 'cat', param2 is 'pig'
longfunc('cat','pig')
# mixed style:
longfunc('cat',param2='pig')
14.2. Parameters
longfunc()
# param1 is now 'bone', param2 is default
longfunc('bone')
# param2 is now 'doggo', param1 is default
longfunc(param2='doggo')
# param1 is 'cat', param2 is 'pig'
longfunc('cat','pig')
# mixed style:
longfunc('cat',param2='pig')
14.3. Return Value
- In Python, all operations have some form of return value, which is helpful if
the function is supposed to act as some part of a bigger operation.
- Note that when a function has returned its value,
the function stops running at the line which return is called.
- All functions that don't have a return command returns a None.
14.3. Return Value
- In Python, all operations have some form of return value, which is helpful if
the function is supposed to act as some part of a bigger operation.
- Note that when a function has returned its value,
the function stops running at the line which return is called.
- All functions that don't have a return command returns a None.
Example:
# This function returns a number, added by 20.
def raise(value=30)
return value+20
# This returns a 100, as raise(50) returns a 70
30+raise(50)