Skip to content

Commit 49da4c6

Browse files
committed
init commit
1 parent a64fb3e commit 49da4c6

13 files changed

+497
-0
lines changed

10. basic Functions intro.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
'''
2+
Hello everyone and welcome to another python 3 basics video. In this video we
3+
will be discussing the basics of a function.
4+
5+
The idea of a function is to assign a set of code, and possibly variables,
6+
known as parameters, to a single bit of text. You can think of it a lot like
7+
why you choose to write and save a program, rather than writing out the
8+
entire program every time you want to execute it.
9+
'''
10+
11+
# To begin a function, the keyword 'def' is used to notify
12+
# python of the impending function definition, which is what def
13+
# stands for.
14+
15+
# from there, you type out the name you want to call your function.
16+
# it is important to choose a unique name, and also one that wont conflict
17+
# with any other functions you might be using. For example, you wouldn't
18+
# want to go calling your function print.
19+
20+
21+
# so here we've called our function example. After the name of the function,
22+
# you specify any parameters of that function within the parenthesis
23+
# parameters act as variables within the function, they are not necessary
24+
# to create a function, so first let's just do this without any parameters.
25+
26+
def example():
27+
# functions just run whatever code is encased with them.
28+
print('this code will run')
29+
z = 3 + 9
30+
print(z)
31+
32+
# now if we just run this, we see nothing happens. We have to actually call
33+
# this function to execute, because all we've done so far is just define the
34+
# function and what it does. To run it, you can either type out the function in
35+
# the console like so:
36+
37+
# or you can add it to the actual script itself:
38+
39+
example()
40+

11. Function Parameters.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
'''
2+
Welcome to another python 3 basics video, in this video we will carryon with
3+
functions. In the last video you were shown a very basic function, without
4+
any parameters
5+
6+
In this video, lets include a parameter, and give this function more...
7+
functionality.
8+
'''
9+
10+
11+
# changed name to simple math, to better describe our intentions
12+
'''
13+
Now we've specified 2 parameters for our function, calling them num1
14+
and num2, for number 1 and number 2.
15+
16+
Now, we carry on writing our function, where we can specify what we
17+
desire to do with num1 and num2.
18+
19+
in our case, we want to do simple addition.
20+
'''
21+
def simple_addition(num1,num2):
22+
answer = num1 + num2
23+
print('num1 is', num1)
24+
# so here the answer variable will be filled with whatever
25+
# num 1 plus num 2 is.
26+
print(answer)
27+
# then at the end, we want to print out the answer to the client
28+
29+
30+
'''
31+
so now we run this, and when we want to do some simple_addition...
32+
'''
33+
34+
simple_addition(5,3)
35+
# here we will do 5 + 3, for an answer of 8
36+
37+
'''
38+
There is no limit to the amount of variables you can have. The only thing
39+
you will want to look out for at this point is the order of the variables,
40+
41+
as well as the quantity.
42+
43+
You can protect yourself from order by doing the following in your calling:
44+
'''
45+
46+
simple_addition(num1=3,num2=5)
47+
# or more clearly #
48+
simple_addition(num2=3,num1=5)
49+
50+
# in this case, if you are clear in your specification, it does not matter
51+
# the order. Most people, however, do not write out the variables like that,
52+
# they just maintain the order.
53+
54+
55+
#finally, it is important to use the proper quantity of variables.
56+
57+
# will not work, too many vars
58+
simple_addition(3,5,6)
59+
60+
# will not work, too few vars
61+
simple_addition(3)
62+
63+
64+
65+
66+
'''
67+
That's it for this video, in the next video I willbe covering default variable
68+
assignments.
69+
70+
'''
71+
72+
73+
74+
75+
76+

12. Function Parameter Defaults.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
'''
2+
Hello again and welcome to another python 3 basics video. In this video we
3+
will be covering default function parameters
4+
'''
5+
6+
# so normally, you write a function like so:
7+
def simple(num1,num2):
8+
pass
9+
10+
# What you can do, however is:
11+
12+
def simple(num1, num2=5):
13+
# what this does is specify a "default parameter" just in case
14+
# one is not specified.
15+
# this is useful so all parameters dont need to be called
16+
# every single time. Generally, this is used for modules.
17+
# an example would be a module that makes windows for users.
18+
pass
19+
20+
21+
# so here, the user must specifiy width and height, but a font of times
22+
# new roman, for example, is the default so they dont have to say that
23+
# every single time.
24+
25+
def basic_window(width,height,font='TNR'):
26+
# let us just print out everything
27+
print(width,height,font)
28+
29+
30+
31+
# now, when we call basic_window, we can break a rule we established
32+
# earlier:
33+
34+
# see, only two parameters, when we require 3
35+
basic_window(350,500)
36+
37+
# we can do this because there is a default if font is not specified. Should
38+
# a user wish to specify however, they can do
39+
40+
basic_window(350,500,font='courier')
41+
42+
# here, it is just important that you place any parameters with default values
43+
# at the very end, to avoid trouble when calling the function down the road.

13. Global and Local Vars.py

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
'''
2+
Welcome to another python 3 basics video, in this video we're going to now
3+
discuss the concept of global and local variables.
4+
5+
When users begin using functions, they can quickly become confused when it comes
6+
to global and local variables... getting a the dreaded variable is not defined
7+
even when they clearly see that it is... or so they think.
8+
9+
These terms of global and local
10+
correspond to a variable's reach within a script or program.
11+
12+
A global variable is one that can be accessed anywhere
13+
14+
A local variable is the opposite, it can only be accessed within its frame.
15+
16+
The difference is that global variables can be accessed locally, but not modified
17+
locally inherently.
18+
19+
A local variable cannot be accessed globally, inherently.
20+
Now, dont worry about committing
21+
that to memory right now, I think it makes a lot more sense when you just
22+
see and do it, so let's do that.
23+
24+
'''
25+
# this variable has no parent function, but is actually NOT a global variable.
26+
# it just so happens that it is committed to memory before the function is called
27+
# so we are able to iterate, or call it out, but we cannot do much else.
28+
29+
x = 6
30+
31+
def example():
32+
# z, however, is a local variable.
33+
z = 5
34+
# this works
35+
print(z)
36+
37+
example()
38+
# this does not, which often confuses people, because z has been defined
39+
# and successfully even was called... the problem is that it is a local
40+
# variable only, and you are attempting to access it globally.
41+
42+
print(z)
43+
44+
# next up is an example that i've seen cause even more trouble, and that's
45+
# the attempt to play with a global variable locally. The reason why this
46+
# is so troubling is because you can access it... you just cannot play
47+
# with it, and this often frustrates people for a while.
48+
49+
50+
x = 6
51+
52+
def example2():
53+
# works
54+
print(x)
55+
print(x+5)
56+
57+
# but then what happens when we go to modify:
58+
x+=6
59+
60+
# so there we attempted to take the x var and add 6 to it... but now
61+
# we are told that we cannot, as we're referencing the variable before
62+
# its assignment.
63+
64+
'''
65+
So now you know the rules, what can we do about it?
66+
'''
67+
x = 6
68+
69+
def example3():
70+
# what we do here is defined x as a global variable.
71+
global x
72+
# now we can:
73+
print(x)
74+
x+=5
75+
print(x)
76+
77+
78+
79+
80+
'''
81+
So that is all for global and local, though I will show 1 last thing.
82+
83+
Sometimes you want a sort of global variable as a starting point, but
84+
you do not actually wish to modify the "global" variable outside of the
85+
functions themselves. You can just do the following:
86+
'''
87+
88+
def example4():
89+
globx = x
90+
# now we can:
91+
print(globx)
92+
globx+=5
93+
print(globx)
94+
95+
96+
# and that's it!
97+
98+
# hopefully that will help some of you from pulling your hair out for 30 minutes
99+
# trying to figure out what the heck is going on to reality. This is something
100+
# that snagged me pretty good when i was starting out.

14. modules.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
'''
2+
At this point, you've got all the basics necessary to start employing modules.
3+
4+
We still have to teach classes, among a few other necessary basics, but now
5+
would be a good time to talk about modules.
6+
7+
8+
IF you are using linux, installing python modules is incredibly stupid easy.
9+
For programming, linux is just lovely when it comes to installing packages
10+
for just about whatever. I believe mac allows similar treatment, though I've
11+
not done it myself.
12+
13+
When I was first starting out with python, installing modules was one of the
14+
most difficult things, for a few reasons.
15+
16+
mainly, with windows, there are quite a few methods for installation of modules.
17+
you've got pip install setuptools, download and click'n'drag, or setup.py
18+
19+
At the time of starting python, a large part of my troubles was that I didn't
20+
actually understand the process of getting a module, and this is obviously very
21+
frustrating.
22+
23+
Python is going to look in a few places for modules.
24+
25+
That's going to be site-packages and the script's directory for the most part.
26+
There are some other places you can use, but let's leave them out of it. Knowing
27+
this allows you yourself to make your own modules, in the form of just a script
28+
if you want, putting that in the same directory as your main script, and then
29+
using import to bring it on board, you can also place multiple scripts in a dir
30+
and use that. Once you begin to familiarize yourself with this, and understand
31+
how and why things work, it will help you a lot.
32+
33+
Enough on that though, let's install stuff.
34+
35+
So I will show linux installation first, because it takes about 10 seconds to
36+
show
37+
38+
39+
40+
41+
now for python, the accepted method these days is setup.py
42+
43+
So when you download a python module,
44+
45+
http://www.pyqtgraph.org/
46+
47+
48+
49+
... now finally, you can use downloaders, here is a large stash
50+
of easy to use python installers for windows:
51+
http://www.lfd.uci.edu/~gohlke/pythonlibs/
52+
53+
'''
54+
55+
56+
57+
58+
File renamed without changes.

3. simple maths.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
1+3
2+
4*4
3+
5-2
4+
5/2
5+
6+
7+
8+
#exponents
9+
4**4
10+
11+
4.5*2
12+
13+
#Integers whole numbers
14+
15+

4. variables.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'''
2+
Hello and welcome to my variables tutorial video. In this video I will teach
3+
you about variables, which you will quickly find yourself using extremely
4+
frequently.
5+
6+
Variables act as a placeholder for whatever you place in them, and, can be
7+
changed... so they are... you guessed it... VARIABLE!
8+
'''
9+
10+
11+
exampleVar = 55
12+
print(exampleVar)
13+
14+
#cannotDo = Hey!
15+
16+
canDo = 'Hey!'
17+
print(canDo)
18+
19+
canContainOperations = 5/4
20+
print(canContainOperations)
21+
22+
#canEvenContainFunctions = # but more on that later....

5. while loop.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
3+
condition = 1
4+
5+
while condition < 10:
6+
print(condition)
7+
condition += 1
8+
9+
10+
condition = '2'
11+
12+
while condition > 5:
13+
print 'test'
14+
15+
# Another favorite of many people... the infinite loop #
16+
17+
while True:
18+
print('doing stuff!!')
19+
20+
21+
22+
# control+c to break script!
23+
24+
25+
26+
27+
28+

0 commit comments

Comments
 (0)