Python Tutorials 2 – Conditions and Loops

Hello Guys !! Hope our previous article regarding the Introduction to Python helped you all in some aspect. Futher,we would like all of you to provide us your useful feedbacks regarding the tutorials so that we can improve on our skills and methods.

Python-Tutorials-Conditions-and-Loops
Python Tutorials 2 – Conditions and Loops

Now here is the article regarding the CONDITIONALS $ LOOPS in Python.

CONDITIONALS

As most of us are aware of C/C++/JAVA ,so we all have some prior knowledge of Conditionals in programming.The most used ones are if and switch statements.

The if,if-else and the switch statements in C/C++/JAVA require the use of {} for the line of codes where as it is quite different in the case of Python.

Example:

#!/usr/bin/python

 

var1 = 100

if var1:

print “1 – Got a true expression value”

print var1

else:

print “1 – Got a false expression value”

print var1

 

var2 = 0

if var2:

print “2 – Got a true expression value”

print var2

else:

print “2 – Got a false expression value”

print var2

 

print “Good bye!”

They are actually  block of codes called suites in Python Documentation and they simply consist of code that is indented under the level of whatever it is that’s controlling it.Four space of indenting is considered standard in Python.

Also,we can use the if conditional as:

Print (“foo” if a<b else “bar”)

SWITCH CASE

The switch statement in Python is same as C/C++/JAVA.The example is given below.

switch(n) {

case 0:

printf(“You typed zero.\n”);

break;

case 1:

case 9:

printf(“n is a perfect square\n”);

break;

case 2:

printf(“n is an even number\n”);

case 3:

case 5:

case 7:

printf(“n is a prime number\n”);

break;

case 4:

printf(“n is a perfect square\n”);

case 6:

case 8:

printf(“n is an even number\n”);

break;

default:

printf(“Only single-digit numbers are allowed\n”);

break;

}

 

LOOPS

A loop statement allows us to execute a statement or group of statements multiple times and following is the general form of a loop statement in most of the programming languages:

Python programming language provides following types of loops to handle looping requirements.

Loop Type Description
while loop Repeats a statement or group of statements while a given condition is true. It tests the condition before executing the loop body.
for loop Executes a sequence of statements multiple times and abbreviates the code that manages the loop variable.
nested loops You can use one or more loop inside any another while, for or do..while loop.

 

Loop Control Statements:

 

Python supports the following control statements.the best of your health.

Control Statement Description
break statement Terminates the loop statement and transfers execution to the statement immediately following the loop.
continue statement Causes the loop to skip the remainder of its body and immediately retest its condition prior to reiterating.
pass statement The pass statement in Python is used when a statement is required syntactically but you do not want any command or code to execute.

 

The details regarding the coding part of the looping statements would be given in our next tutorial.

Feedbacks are awaiting.Till the:

“Stay Happy : Stay Foolish : Stay Blessed “

Python Tutorials

We are very happy to start a new section “Python Tutorials “on crazylearner.  Hope you will enjoy our Free python Tutorials online.In the section of Python Tutorials , our First  objective is to clear all the initial informations regarding the language like: Why python, its perquisites ,obtaining python for your computer.

Although Python is a high-level language, it is not English or some other natural human language. The Python translator does not understand “add the numbers two and three”. Python is a formal language with its own specific rules and formats, which these tutorials will introduce gradually, at a pace intended for a beginner.

Python-Tutorials-free-online-beginner-course-programming-language
Python Tutorials

 

Why Python?

The first and the most important question is to ask yourself that why should you go for PYTHON and not any other language?

Well,  Python is one of the easiest languages to learn and use, while at the same time being very powerful. It is used by many of the most highly productive professional programmers. A few of the places that use Python extensively are Google, the New York Stock Exchange, Industrial Light and Magic, etc.

Prerequisites of learning python

  •  You need a basic understanding of programming
  •  You need experience writing working code in a modern programming language.
  • Perl,PHP,Java,JavaScript,C++.
  •  You need a basic understanding of Object Oriented Programming as Python is an Object Oriented Scripting language.
  •  You need a Text Editor.

Obtaining python for your computer

If you do need a copy of Python, go to the Downloads page linked to http://www.python.org. Be careful to choose the version for your operating system and hardware.

Windows

You just need to execute the installer, and interact enough to agree to all the default choices. Python works in Windows as well as on Apples and in the free operating system Linux. The latest stable version is 3.3

OS X

Double-click on the installer. Find and run the MacPython.mpkg that is inside. Follow the defaults for installation.

Linux

Python is generally installed, though Idle is not always installed. Look for something like ‘idle-python’ (the name in the Ubuntu distribution).

Hope our very first post regarding the language helped you to understand the basics of the language.Next we would post about the coding (conditionals and loops).

Till then “Stay Happy,Stay Foolish,Stay Blessed”.  🙂