Tag Archives: python

Python: For and While Loop

So repetition on Python is very similar to other programming languages, you have the choice of a For and While loop.

For Loop

If you are familiar with foreach statement in C# then this will be easy to understand. Below I have a list of numbers from 1 to 5, then I use the for statement to get each number from numbers and print it.

numbers = [1, 2, 3, 4, 5]

for number in numbers:
    print number

While Loop

While loop code snippet below I have created a list of numbers from 1 to 5 and I have an index variable of 0. I will be using the index to iterate through the numbers list and at the same time I increment the index. The while loop will be validating if the index is less than 5, if it is true continue to print the number. If the index is greater than 5 exit out of the while loop.

numbers = [1, 2, 3, 4, 5]

index = 0

while index < 5:
    print numbers[index]
    index += 1

Download the source code for this blog from my GitHub repository:

https://github.com/csharpguy76/LearnPython

Python: Functions

Creating a function in Python allows you to reuse code throughout your project. You can create a library of useful functions that can make your coding simplified.

To create a function use the def statement follow by the name of the function and then an open parenthesis and then follow by a list of parameters and then a closing parenthesis and a colon. Then the next line you press TAB to indent your function’s code.

Your function can return a value by specifying the return statement follow by some value.

Create a new file called Functions.py with the following code, or enter the code in your interpreter:

def add(a, b):
   return a + b

result = add(2,5)
print result

In the code above I have defined my function called Add with two parameters a and b which will take in an integer value. Then in the function body I am adding a and b together and return the result.

The next line I call the function Add and give it a value of 2 and 5 to be added and then assign the returned value to a variable called result, then I use the print statement to display the result. The expected value return should be displayed is 7.

Download the source code for this blog from my GitHub repository:

https://github.com/csharpguy76/LearnPython

Python: Comments

It’s always a good practice to add comments to your code, it describes the functionality of the code. It’s also useful for making small notes like To Do’s so that you can refer back to it later.

In Python there are two ways of using comments in your code.

For single line comments you use the # and follow by a comment:

# This is a single line comment

For multiple line or block comments you use the triple double quotes:

"""
this is a multi-line comment
line 1
line 2
"""

Python: Install and Your First HelloWorld

I have decided that I will blog about programming in Python and before we can start coding we need to install the interpreter on our operating system. Python is available on Linux, Windows and Mac OS X and you can download the installer from here.

If you are an Ubuntu user like myself use the following command in your terminal to install Python:

sudo apt-get install python

After you have installed Python, go and open your favourite text editor and enter the following code and save it as HelloWorld.py:

print "Hello, World"

From a command line or terminal enter the following command to execute the script to see the the output result:

python HelloWorld.py

Ok now you have written your first Python code!

Writing Python Scripts on Android Mobile

I found a cool app on Google Play recently, QPython 3 which allows you to write Python scripts and run it under a console on your Android mobile phone. I tried typing using the built in keyboard, but if you’re a coder like me and need to type fast you can still connect a USB or Bluetooth Keyboard to your mobile phone.

wpid-tempFileForShare.jpg

In the console window you can still enter your code and run it on the fly or you can choose to write your script in their text editor called QEdit from the menu.

wpid-tempFileForShare.jpg Console Screen.

wpid-tempFileForShare.jpgQEdit – Text Editor