Data Science and Artificial Intelligence

Python Statements

In simple terms, statements are used to tell Python what your programs should do. In this blog, we will discuss Python statements.

Before we proceed further, we should understand what expressions are and how to evaluate them.

An expression is a combination of values, variables, and operators. If you type an expression in the command line, the interpreter evaluates it and displays the result.

Expressions are commonly used as statements in two situations:

  1. For printing values at the interactive prompt

Python displays the results of expressions that are typed in the interactive command line. Technically, these are expression statements too; they serve as a shorthand for typing print statements

For example,

  1. For calls to functions and methods

Expressions can be used to call methods and functions in python statements.

Python Statements

Some of the important statements in Python are as follows:

    • Printing statement

    • Assignment statements

    • Hadoop

      Conditional statements

    • General loops

    • Loop exit and continue

Print Statement

In Python, print is used to print something. Technically, printing converts objects to their textual representations and sends the resulting text to either standard output or another file-like stream.

When you type a print statement in the command line, Python executes it and displays the result. The result of a print statement is a value.

For example,

Assignment Statements

Python assignment statement is used to assign objects to names. Normally, you write the target (may be a name or an object component) of an assignment on the left side of an equal sign, and the object (may be an arbitrary expression that computes an object) to be assigned, on the right side.

When you type an assignment statement on the command line, Python executes this but does not produce a result.

For example,

In the above example X1 is the assignment statement which will not produce any result till we use print statement.

Conditional Statements

Conditional statement is a set of rules that are executed if a certain condition is met. Below are the conditional statements in Python:

if, if/else, if/elif/else

Consider the following if statement, coded in a C-like language:

if (x > y) {

x = 1;

y = 2;

}

This might be a statement in C, C++, Java, JavaScript, or similar. Now, look at the

equivalent statement in the Python language:

if x > y:

x = 1

y = 2

Note:

  • In Python, blocks are delimited by indentation.

  • Colon (:) is used at the end of lines containing control flow keywords.

  • In Python, parentheses are optional

Example of if statement

Example of if/else statement

Example of if/elif/else statement

General Loops

In computer programming, a loop is a sequence of instructions that are continually repeated until a certain condition is reached; a program that interacts with a user in a console window.

Whether you’re accepting inputs to send to a database, or reading numbers to be used in a calculation, you need to code a loop that reads one or more inputs from a user typing on a keyboard, and prints a result for each. In other words, you need to write a classic read/evaluate/print loop program.

We will discuss different types of loops.

While Loop

A Python while loop behaves quite similarly to common English usage. For example,

While your tea is too hot, add a chip of ice. Presumably you would test your tea. If it were too hot, you would add a little ice. If you test again and it is still too hot, you would add ice again. As long as you test and find it to be true that your tea is too hot, you would add more ice. Python has a similar syntax:

while condition:

indentedBlock

a = 10

while a > 0:

print a

a -= 1

For example,

For loops

The for statement in Python differs from that in C or other languages. Rather than iterating over an arithmetic progression of numbers (like in Pascal), or giving the user the ability to define both the iteration step and halting condition (like in C), Python’s for statement iterates over the items of any sequence (a list or a string), in the order that they appear in the sequence.

For example,

words = [‘Acadgild’, ‘Python’, ‘Data Science’]

for w in words:

print w, len(w)

range() function

If you need to iterate over a sequence of numbers, the built-in function range() comes in handy. It generates lists containing arithmetic progressions:

For example,

To iterate over the indices of a sequence, you can combine range() and len() as follows:

Break, Continue and the Loop Else

Now that we’ve seen a few Python loops in action, it’s time to take a look at two simple

statements that have a purpose only when nested inside loops—the break and continue statements. We will also study the loop else clause here, because it is intertwined with break, and Python’s empty placeholder statement.

break

Jumps out of the closest enclosing loop (past the entire loop statement)

continue

Jumps to the top of the closest enclosing loop (to the loop’s header line)

Loop else block

Runs if and only if the loop is exited normally (i.e., without hitting a break)

“break” and “continue” statements

For example,

break is used to exit a for loop or a while loop, whereas continue is used to skip the current block, and return to the “for” or “while” statement.

Below are few examples:

# Prints out 0,1,2,3,4

count = 0

while True:

print count

count += 1

if count >= 15:

break

# Prints out only even numbers – 0,2,

for x in range(10):

# Check if x is even

if x % 2 != 0:

continue

print x

“else” clause for loops

Unlike languages like C, we can use else for loops. When the loop condition “for” or “while” statement fails, then the code part in “else” is executed. If break statement is executed inside for loop, then the “else” part is skipped. Note that “else” part is executed even if there is a continue statement.

For example,

# Prints out 1,2,3,4

for i in range(1,10):

if(i%5==0):

break

print i

else:

print “Loop is terminated because of break but not due to condition”

We hope this blog helped you in understanding Python statements.

If you have any queries, please reach out to us on [email protected]

Keep visiting our site www.acadgild.com for more updates on Python and other technologies. Click here to learn android App Development.

Spark

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Articles

Close