-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemo-1.py
More file actions
172 lines (126 loc) · 4.03 KB
/
Copy pathdemo-1.py
File metadata and controls
172 lines (126 loc) · 4.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# -*- coding: utf-8 -*-
"""
Created on Tue Jun 8 20:33:16 2021
@author: Daniel Mishler
"""
# Goal: fight two monsters
# Will take six weeks
# Week 1: the basics
# The only week where we do not directly work towards the above goal.
# What is Python?
# Python is a coding language designed for people who don't understand computers
# However, it is still useful to experts.
# What is a function?
# something that takes zero or more inputs and does something.
# In python, all functions are called with ()
# This means print() is a function, but print will not work
# What is a variable
# Everything else that is not defined as a function or some other
# Token in Python is reserved to you the user as a variable.
# int: integer
# str: string (one or more words, usually denoted by quotes)
# float: a real number, usually not an integer. Stands for "floating point"
# All lines with a "#" in front are ignored
print(10)
# Whenever you run this file, all the things in this file are run
# Because of this, it's usually good practice not to have a lot of things
# in a single file.
# Variables and variable names
myVariable = 1
# What to name my variables?
# "print" is a terrible name for your variable, for example
# Because print() is already a function!
# If your variable name highlights itself, change it!
# XXXX: my Variable = 2
# Camel Case: myVariable
# Pascal Case: MyVariable
# Snake Case: my_variable
mySecondVariable = 2
print(myVariable)
print(mySecondVariable)
print() # Just adds a line of space
myVariable = 3
print(myVariable)
print(mySecondVariable)
print() # Just adds a line of space
myVariable = mySecondVariable
# The equals sign takes what is on the right and stores it
# in what is on the left without changing what is on the right
print(myVariable)
print(mySecondVariable)
print() # Just adds a line of space
mySecondVariable = 6
print(myVariable)
print(mySecondVariable)
print() # Just adds a line of space
# Golden rule of variables:
# A variable will NEVER change unless you explicitly change it
# whenever you write a function, it's available for all time after
# it is defined.
# The "argument" could be anything.
def Yell(argument):
# Begin the scope
# Everything at this indentation is part of the function
# "Yell" will take a string and print it uppercased.
# The string will be called "argument"
# This is a new variable that only exists in the scope of the function
print(argument.upper())
# Python is full of googling and stack overflow.
# Strings all have a built-in "method" called upper(). They
# have other methods too!
# Returning to this indentation level means the function is complete.
Yell("Hello")
def Cubed(number):
# Take a number and cube it
answer = number*number*number
# We could "print" the answer, but usually unless you're
# specifically told to print, we use "return" instead.
return answer
myVariable = Cubed(2)
print(myVariable)
print(mySecondVariable)
print() # Just adds a line of space
myVariable = Yell("hello")
print(myVariable)
print(mySecondVariable)
print() # Just adds a line of space
"""
def Spell(number):
# Take a 1, 2, or 3. And print "One", "Two", or "Three"
if number == 1:
# == means comparison!
print("One")
if number == 2:
print("Two")
if number == 3:
print("Three")
"""
def Spell(number):
# Take a 1, 2, or 3. And print "One", "Two", or "Three"
if number == 1:
# == means comparison!
print("One")
elif number == 2:
print("Two")
elif number == 3:
print("Three")
# More variable types
# int
# floats
# strings
# list
myList = [1,2,3]
# arrays exist, but just pretend everything is a list for now.
print(myList)
print(myList[1]) # key takeaway: We index from 0 here
print(myList[0])
print(myList[2])
print(myList[-1])
# tuple
myTuple = (4,5)
# Think of a tuple as a nerfed list.
print(myTuple[1])
print(myTuple[-1])
# Tuples are thought of as ordered pairs (or triplets)
# dictionary. They exist but won't be important today
# "Raised to the power of": **