Posted on

Learn Python Episode #20: Return Values from Functions

Get The Learn to Code Course Bundle!
https://josephdelgadillo.com/product/learn-to-code-course-bundle/

Enroll in The Complete Python Course on Udemy!
https://www.udemy.com/python-complete/?couponCode=PYTHONWP

Welcome back everyone. This is the final tutorial in the subsection for functions, and we’re going to be talking about return values from functions. Again, this may not be new to some of you as it is a fairly basic concept. So, in the past videos we have been using functions to print something out, but what if we want to return that value and do something else with it? Let’s go ahead and define a function.

def do_math(num1, num2):
return num1 + num2

do_math(5, 7)

Now when we run this bit of code, it’s not going to print anything in our Python console because we didn’t tell it to. Let’s add on to this.

def do_math(num1, num2):
return num1 + num2

math1 = do_math(5, 7)
math2 = do_math(11, 34)

print("First sum is", math1, "and the second sum is", math2)

When we run this we get the First sum is 12 and the second sum is 45. So, again, this a very simple example, but we will expand on this when we build our calculator.

Web – https://josephdelgadillo.com/
Subscribe – https://goo.gl/tkaGgy
Follow for Updates – https://steemit.com/@jo3potato

Posted on

Learn Python Episode #19: Infinite Arguments

Get The Learn to Code Course Bundle!
https://josephdelgadillo.com/product/learn-to-code-course-bundle/

Enroll in The Complete Python Course on Udemy!
https://www.udemy.com/python-complete/?couponCode=PYTHONWP

In this video we are going to discuss passing an infinite number of arguments in to a function. So, let’s go ahead and write a function.

def print_people(*people):

So, here we have a function called print_people, and the asterisk tells this argument that it’s going to be an array of all the arguments that are passed in to the function. This may result in 1, 100, 1,000, etc. arguments, and we will see how this works in a moment. With an array we need some way to loop over it, and so we’re going to be using a for statement.

for person in people:
print("This person is", person)

Basically, the for loop allows us to iterate over the people list and pass the values into our function as arguments. Again, if you’re familiar with other programming languages this is not a new concept, it’s just a slightly different way of doing it. Let’s call the print_people function and pass it some names.

print_people("Nick", "Dan", "Jack", "King", "Smiley")

What we’re doing here is passing 5 arguments. Now if we knew we were always going to expect 5 arguments, we could accept each individual argument as it’s own variable. However, if we don’t know the total number of values, we are going to create an array stored in the variable called people. So, that’s how to include an infinite, or flexible, number of parameters. In the next tutorial we will cover return values.

Web – https://josephdelgadillo.com/
Subscribe – https://goo.gl/tkaGgy
Follow for Updates – https://steemit.com/@jo3potato

Posted on

Learn Python Episode #18: Keyword Arguments

Get The Learn to Code Course Bundle!
https://josephdelgadillo.com/product/learn-to-code-course-bundle/

Enroll in The Complete Python Course on Udemy!
https://www.udemy.com/python-complete/?couponCode=PYTHONWP

Alright! So, let’s talk about keyword arguments. The first keyword, or boolean, that we are going to cover is None. Basically, this is the equivalent to null in other languages. So, let’s go ahead and write our previous function.

def print_something(name = "Someone", age = "Unknown"):
print("My name is", name, "and my age is", age)

print_something(None, 27)

There isn’t a way to pass in an argument that isn’t first one being used unless we use keyword arguments. We can also define the variable when we call the function.

def print_something(name = "Someone", age = "Unknown"):
print("My name is", name, "and my age is", age)

print_something(age = 27, name = "Nick")

By using keyword arguments, we can specify which value is supposed to go in a particular variable.

Web – https://josephdelgadillo.com/
Subscribe – https://goo.gl/tkaGgy
Follow for Updates – https://steemit.com/@jo3potato

Posted on

Learn Python Episode #17: Default Arguments

Get The Learn to Code Course Bundle!
https://josephdelgadillo.com/product/learn-to-code-course-bundle/

Enroll in The Complete Python Course on Udemy!
https://www.udemy.com/python-complete/?couponCode=PYTHONWP

In this video we were going to discuss arguments a bit further, and we were going to move on to keyword arguments. Before we do that however, it is going to make more sense to cover default arguments first. So, let’s go ahead and write a function.

def print_something(name, age):
print("My name is ", name, + " and my age is " + age)

print_something("Nick", 27)

This won’t run. Remember, when we concatenate strings we must convert integers to strings as well. While we could use the str function to fix our code, it is much easier to separate the four pieces of data with commas.

def print_something(name, age):
print("My name is", name, "and my age is", age)

print_something("Nick", 27)

The results will be the same as if we converted the integer to a string and concatenated the four strings together. So, what happens if we only want to pass our function 1 of the 2 values? This is where we can use default arguments.

def print_something(name = "Someone", age = "Unknown"):
print("My name is", name, "and my age is", age)

print_something("Nick")

When we run this bit of code, Python will print out My name is Nick and my age is Unknown. As you can see, Python will default to the argument we provided it if no data is given.

Web – https://josephdelgadillo.com/
Subscribe – https://goo.gl/tkaGgy
Follow for Updates – https://steemit.com/@jo3potato

Posted on

Learn Python Episode #16: Adding Arguments to a Function

Get The Learn to Code Course Bundle!
https://josephdelgadillo.com/product/learn-to-code-course-bundle/

Enroll in The Complete Python Course on Udemy!
https://www.udemy.com/python-complete/?couponCode=PYTHONWP

In this video we’re going to expand on functions, and we’re going to learn how to implement arguments into our functions. The following function is completely static.

def my_function():
print("This is a function!")
print("A second string.")

my_function()

We could call this function 10 times and each time it’s going to print out the same strings. So, how can we make our functions more dynamic? The way to do this is by adding arguments, and arguments go in the parenthesis at the end of the function name. So, let’s add two arguments in to our function.

def my_function(str1, str2):
print(str1)
print(str2)

my_function()

Now if we try to run this we would get an error. Python is looking for the values str1 and str2 and they are undefined. Let’s go ahead and define them.

def my_function(str1, str2):
print(str1)
print(str2)

my_function("This is argument 1", "This is the second argument which is also a string.")

Now when we call my_function it will print out the two strings. Let’s call the function again and print out two different strings.

def my_function(str1, str2):
print(str1)
print(str2)

my_function("This is argument 1", "This is the second argument which is also a string.")
my_function("Stringy", "Hello World")

As you can see, Python will print out all four strings.

Web – https://josephdelgadillo.com/
Subscribe – https://goo.gl/tkaGgy
Follow for Updates – https://steemit.com/@jo3potato

Posted on

Learn Python Episode #15: User-Defined Functions

Get The Learn to Code Course Bundle!
https://josephdelgadillo.com/product/learn-to-code-course-bundle/

Enroll in The Complete Python Course on Udemy!
https://www.udemy.com/python-complete/?couponCode=PYTHONWP

Let’s get into user-defined functions. Now, every programming language has functions, however they are going to look different depending on the language. If you have any experience with PHP or JavaScript, functions in Python are going to look similar. So, we are now past the need to use terminal, and from here on out we are going to use our IDE. In addition to functions, we are going to cover a few of the PEP guidelines. PEP is basically a style guide for the Python language, and the first thing we need to do is drop down two lines from the top of the script. To begin defining a function in Python, all you need to do is type the word “def”. When naming functions we must be mindful of the PEP guidelines. With Python you will be using snake case, words separated with underscores, when naming your function.

def my_function():

Within the parenthesis we can pass parameters into our function, then we end the line with a colon. So, now we have a function but it doesn’t perform any actions, yet.

def my_function():
    print("This is my function!")
    print("A second string.")

my_function()

Now our function will print out two strings. Notice that our IDE automatically indents 4 spaces. When you indent in Python you are creating block of code. When we skip a line and remove the indentation, we are telling Python that our function is completed. Lastly, we will call our function. In the next video we will talk about arguments and how use them inside your function.

Web – https://josephdelgadillo.com/
Subscribe – https://goo.gl/tkaGgy
Follow for Updates – https://steemit.com/@jo3potato

Posted on

Learn Python Episode #14: Builtin Functions

Get The Learn to Code Course Bundle!
https://josephdelgadillo.com/product/learn-to-code-course-bundle/

Enroll in The Complete Python Course on Udemy!
https://www.udemy.com/python-complete/?couponCode=PYTHONWP

In this video we are going to talk about a few of the built-in functions available in Python, and then we’re going to get into defining our own functions, and the different parts of a function. So, these are some basic (very basic) functions that we need to learn about. We are not going to get into any specific modules like the math module, or anything like that, I’m just going to demo a few basic functions. A few of these we have already use earlier in the tutorial series.

print
str
int
bool
len
sorted

In the next video we will begin building our own functions.

Web – https://josephdelgadillo.com/
Subscribe – https://goo.gl/tkaGgy
Follow for Updates – https://steemit.com/@jo3potato

Posted on

Learn Python Episode #13: Variables

Get The Learn to Code Course Bundle!
https://josephdelgadillo.com/product/learn-to-code-course-bundle/

Enroll in The Complete Python Course on Udemy!
https://www.udemy.com/python-complete/?couponCode=PYTHONWP

In this video we are going to cover variables in Python, and as you may have guessed, a variable is an object that is variable in nature. So, why do we use variables in our code? Well, when we explicitly write strings they are not reusable. If I were to simply write “This is a sting,” I would be unable to reuse that line of code later on in my script. So, one reason we use variables is for re-usability. Another reason we use variables is for passing values into functions, but we will get into that in a later video. The nice thing about Python is that we don’t need to declare or define the type of variable. Creating a variable is as simple as this:

greeting = "hello world"

When you hit enter in your terminal or command prompt, Python will not output a response. However, when we call our new variable, Python will print out “hello world”

print(greeting)

Now that the greeting variable is stored in memory, we can make changes to it.

greeting = greeting.split (" ")[0]

If we print(greeting) now it will returns hello. We can write the following to return hello nick:

print(greeting + " Nick")

In the next video we’re going to be talking about some of the built-in functions available to you in Python.

Web – https://josephdelgadillo.com/
Subscribe – https://goo.gl/tkaGgy
Follow for Updates – https://steemit.com/@jo3potato

Posted on

Learn Python Episode #12: Dictionaries

Get The Learn to Code Course Bundle!
https://josephdelgadillo.com/product/learn-to-code-course-bundle/

Enroll in The Complete Python Course on Udemy!
https://www.udemy.com/python-complete/?couponCode=PYTHONWP

In this video we will cover dictionaries in Python. In the previous video when we created a list we used square brackets and comma separated values. To create a dictionary we will use curly brackets instead of the square brackets. In a previous video I may have mentioned that we do not use curly brackets in Python, and that was a lie. What I meant is that we don’t need to wrap code blocks in curly brackets like PHP or JavaScript. So, let’s go ahead and define a dictionary.

{"name": "Nick", "age": is 27, "hobby": "code"}

As you can see, instead of just comma separated values we used in the list, the dictionary uses a key and a value separated by a colon. You can access an item in the dictionary like so:

{"name": "Nick", "age": is 27, "hobby": "code"}{"name"}

Python will then print out “Nick.” Dictionaries will be very important when we get to the JSON part of the course, because we will need to parse though a ton of data.

Web – https://josephdelgadillo.com/
Subscribe – https://goo.gl/tkaGgy
Follow for Updates – https://steemit.com/@jo3potato

Posted on

Learn Python Episode #11: Lists (Arrays)

Get The Learn to Code Course Bundle!
https://josephdelgadillo.com/product/learn-to-code-course-bundle/

Enroll in The Complete Python Course on Udemy!
https://www.udemy.com/python-complete/?couponCode=PYTHONWP

In this video we are going to discuss lists and what they are in Python. So, if you have experience with programming in other languages, let’s say PHP, you know to create an array, and what an array is. For those who do not have experience programming, an array is a way of keeping data organized and within a single construct. For single-dimensional arrays, we can implement it as a list in Python. To create a list in Python we use square brackets:

["Movies", "Games", "Python"]

This becomes a list that has 3 indexes. To call the first item in the list you write the following:

["Movies", "Games", "Python"][0]

Remember, when programming the first ID of the item in a list or an array will be 0. We can also concatenate a list item with a string.

print("I Like" + ["Movies", "Games", "Python"][0])

If we were to change the index to number to 1 it would print out “I Like Games”. So, that is what a list is in Python. It’s just a way to create a collection under one variable. In the next video we will cover dictionaries.

Web – https://josephdelgadillo.com/
Subscribe – https://goo.gl/tkaGgy
Follow for Updates – https://steemit.com/@jo3potato