As shown earlier, hello calls the function you declared above.
Functions are a little like walled gardens: This is known as variable scope. If you want a function to work with any outside data, you'll need to pass that data to the function. This is accomplished using arguments — and no, functions don't argue with each other! An argument is a variable that you pass to a function; the function can then access the value of that variable inside its walls. The above function defines two arguments, a and b , separated by commas between the parentheses. When you call the function, the interpreter sets the value of a and b to the value of the variables you pass in.
In the example above, the statement add 1,2 sets a and b to 1 and 2, respectively. The function then prints out the sum of the two numbers you passed in. The above example prints out the result of the calculation — but what if you wanted to do something with the result of that calculation? What if you wanted to add other values to the result of the function?
In the above example, your function adds the two numbers as before, but the return statement then passes the sum of the two numbers back to the calling statement. That means the print statement above takes the value returned by add 2,2 and adds it to 1, which will give the final value of 5. While loops are similar to for loops. For loops continue until you reach the end of a list, but while loops will repeat indefinitely until a given condition evaluates to False.
One thing to watch out for with while loops is creating an infinite loop. Whoa — Terminal is going pretty crazy, isn't it?
What the heck happened there? If you look closely, you'll see that it was impossible for the while loop conditional: True , to ever evaluate to False , so the block executed as fast as the CPU could crunch numbers. The moral of this story: It is possible to use True as your while loop conditional, especially when you might not know how many times your loop should execute. However, you need to use a little trick to exit out of the loop.
That's better! The above code prints the numbers from 0 to 10 and exits. The trick here is to use the break statement which immediately exits out of any loop.
You can also use this technique if you're in the middle of a for loop and need to exit prematurely for whatever reason. One cool thing about Python is that it's very easy to receive input in the form of text from the user. Input is any external data provided to a program, such as text or other directions which affect the behavior of a program. The above code first asks the user for input; once the user types in their answer to the question the program assigns it to the variable name.
Once that's done, the program then concatenates the string "Hello, " with the contents of the variable name. First, you capture two values input by the user and assign them to the a and b variables respectively. Then you convert the input values into integers and add them together. Why the conversion step? If you didn't convert the strings to integers, what do you think would happen?
That's right — the program would print out the concatenation of the two strings, which is not what you want! A module is a group of Python functions which you can reuse in your programs. Importing a module is the equivalent of taking the all the code from the module and putting it right into your program so you can use all of the functions whenever you want, but without cutting and pasting a single line of code!
There are lots of modules available in the Python community, but right now you'll just touch on the random module. You've gained a lot of knowledge by this point, enough to put together a small program that exercises everything you've learned so far! This is where all of your hard work learning to code Python pays off. You're going to create your very own guessing game!
First off, you'll need a better way to run programs than typing them directly into the Python interpreter, statement by statement. This creates an empty file called guess. Once you have some code in your Python file, you can execute your program by typing python guess. You'll see a text editor appear where you can type in your new program. Click the File menu again, and choose Save. Name the file guess. The guessing game will generate a random number, then use a loop to ask the player repeatedly for their guess.
If the user guesses the number, it'll exit the loop; otherwise the program will notify the user if their guess was too high or too low and continue to ask the player for guesses until they get it right.
The above code imports the random module, then generates a random number between 0 and and stores it in the variable number. Next, you need to ask the player for their guess. Add the following code to the bottom of your program:. This code, as you can probably guess, asks for user input and assigns it to the variable guess. Remember, that variable is now set to a string — you'll need to convert that string to an integer. Add the following code to your program, right after the guess statement you added above:.
What happens? You want the program to continue looping until the person guesses correctly. You'll add this functionality by using a variable named running. When the user guesses the correct number, the program will set running to False and the while loop will stop.
Python is a fairly old language created by Guido Van Rossum. Large standard libraries to solve common tasks Python has a number of standard libraries which makes life of a programmer much easier since you don't have to write all the code yourself. Object-oriented Everything in Python is an object. Likewise, when you run Python code, it automatically converts your code to the language your computer understands. This code, as you can probably guess, asks for user input and assigns it to the variable guess. Just follow the instructions and click on Next buttons and it will install Python on your Mac.
Next, indent the remainder of the program by one level so the while loop recognizes this as the code block for the loop. Finally, you need to add the logic that sets running to False once the user wins. Run your program, and try it out now. Write the following lines there and save it. On Ubuntu Now save it. Your very first program in Python.
For MAC you can download Python 3. After downloading, double click it. Just follow the instructions and click on Next buttons and it will install Python on your Mac. Below is the success screen. Now search for IDLE app. Write the following line there and save it. Below image shows IDLE app in action. Well, for the very beginner IDLE is a standard platform for coding. There are a lot of IDEs available. But for now IDLE is enough. So sit tight and get ready for the next part.
If you have come this far, it means that you liked what you are reading. Why not reach little more and connect with me directly on Google Plus , Facebook or Twitter.
I would love to hear your thoughts and opinions on my articles directly. Recently I started creating video tutorials too, so do check out my videos on Youtube. Comment Policy: Please submit comments to add value to the post. Comments like "Thank You" and "Awesome Post" will be not published. Your email address will not be published. Leave this field empty. You are here: Table of Contents 1 Python Tutorial for Beginners 1.