Augmented Assignment Python Wrapped

Comments that contradict the code are worse than no comments. Always make a priority of keeping the comments up-to-date when the code changes!

Comments should be complete sentences. If a comment is a phrase or sentence, its first word should be capitalized, unless it is an identifier that begins with a lower case letter (never alter the case of identifiers!).

If a comment is short, the period at the end can be omitted. Block comments generally consist of one or more paragraphs built out of complete sentences, and each sentence should end in a period.

You should use two spaces after a sentence-ending period.

Header Comments


Header comments appear at the top of a file. These lines typically include the filename, author, date, version number, and a description of what the file is for and what it contains. For class assignments, headers should also include such things as course name, number, section, instructor, and assignment number.

Example: # course: cs108 # laboratory: 1 # date: 09/10/09 # username: mageejo # name: John Magee # description: a basic start-up program.

Function Comments


Function comments are like the description part of a header comment, but contain information specific to what a function does. These comments should also include a description of the purpose and expected input arguments, the expected output values, and how error conditions are handled.

Put a line of comment characters before each function definition to help visually separate each function within your code.

Example:
############################################################## # calcHypotenuse(a, b) # This function solves Pythagorean theorem a^2 + b^2 = c^2 # for the value of c. # inputs: a and b are the lengths of sides of a right triangle. # returns: the length of the hypotenuse. def calcHypotenuse(a, b): # function body here...

Block Comments


Block comments generally apply to some (or all) code that follows them, and are indented to the same level as that code. Each line of a block comment starts with a # and a single space (unless it is indented text inside the comment).

Paragraphs inside a block comment are separated by a line containing a single #.

Inline Comments


Use inline comments sparingly.

An inline comment is a comment on the same line as a statement. Inline comments should be separated by at least two spaces from the statement. They should start with a # and a single space.

Inline comments are unnecessary and in fact distracting if they state the obvious. Don't do this:

x = x + 1 # Increment x But sometimes, this is useful: x = x + 1 # Compensate for border

In this interaction, you’ll get to learn about Python statement, expression, and the difference between them. Next, we’ll explain how to use multi-line statement and indentation in Python programming.

Also, we’ll try to answer questions like “Why is indentation so important in Python?“, “How many spaces is an indent in Python?” and so on.

+ Read:  Variable, Identifier and Keyword in Python

Here is the list of topics we are going to cover in this tutorial.

Python Statement

What is a Statement?

A statement in Python is a logical instruction which Python interpreter can read and execute. In Python, a statement could be an expression or an assignment statement.

The assignment statement is fundamental to Python. It defines the way an expression creates objects and preserve them.

Let’s now find out more details on this topic.

What is an Expression?

An expression in Python is a logical sequence of numbers, strings, objects, and operators. A value in itself is a valid expression and so is a variable.

Using expressions, we can perform operations like addition, subtraction, concatenation and so on. It can also call to a function to evaluate results.

Examples

# Using Arithmetic expressions >>> ((10 + 2) * 100 / 5 - 200) 40.0 # Using functions in an expression >>> pow(2, 10) 1024 # Using eval in an expression >>> eval( "2.5+2.5" ) 5.0

Back to top

Simple Assignment Statement

In the basic assignment, we create new variables, assign and change values. This statement provides an expression and a variable name as a label to preserve the value of the expression.

# Syntax variable = expression # LHS <=> RHS

Let’s now take a close look at three types of assignment statements in Python and see what’s going on under the hood.

Case-1: Right-hand side (RHS) is a simple expression.

Let’s consider the most basic form of assignment in Python.

>>> test = "Learn Python"

Python will create a string “Learn Python” in memory and assigns the name “test” to it. You can confirm the memory address with the of a built-in function known as id().

>>> test = "Learn Python" >>> id(test) 6589040

The number is the address of the location where the data lives in memory. Now, here comes a few interesting points which you should know.

1. If you create another string with the same value, Python will create a new object and assign it to a different location in memory. So this rule would apply to most of the cases.

>>> test1 = "Learn Python" >>> id(test1) 6589104 >>> test2 = "Learn Python" >>> id(test2) 6589488

2. However, Python will also allocate the same memory address in the following two scenarios.

  • The strings don’t have whitespaces and contain less than 20 characters.
  • In case of Integers ranging between -5 to +255.

This concept is known as Interning. Python does it to save memory.

Back to top

Case-2: Right-hand side (RHS) is a current Python variable.

Let’s take up the next type of assignment statement where the RHS is a current Python variable.

>>> another_test = test

The above statement won’t trigger any new allocation in memory. Both the variables would point to the same memory address. It’s like creating an alias to the existing object. Let’s validate this by using the id() function.

>>> test = "Learn Python" >>> id(test) 6589424 >>> another_test = test >>> id(another_test) 6589424

Case-3: Right-hand side (RHS) is an operation.

In this type of statement, the result would depend on the outcome of the operation. Let’s analyze it with the following examples.

>>> test = 2 * 5 / 10 >>> print(test) 1.0 >>> type(test) <class 'float'>

In the above example, the assignment would lead to the creation of a “float” variable.

>>> test = 2 * 5 >>> print(test) 10 >>> type(test) <class 'int'>

In this example, the assignment would lead to the creation of an “int” variable.

Back to top

Augmented Assignment Statement

You can combine arithmetic operators in assignments to form an augmented assignment statement.

Check out the below examples for augmented assignment statement.

x += y

The above statement is a shorthand for the below simple statement.

x = x + y

Next one is a bit clearer example where we are appending new elements to the tuple.

>>> my_tuple = (10, 20, 30) >>> my_tuple += (40, 50,) >>> print(my_tuple) (10, 20, 30, 40, 50)

Next example is using a list of vowels. It is demonstrating the addition of missing vowels to the list.

>>> list_vowels = ['a','e','i'] >>> list_vowels += ['o', 'u',] >>> print(list_vowels) ['a', 'e', 'i', 'o', 'u']

Back to top

Multi-line Statement in Python

Usually, every Python statement ends with a newline character. However, we can extend it over to multiple lines using the line continuation character (\).

And Python gives us two ways to enable multi-line statements in a program.

Explicit line continuation

When you right away use the line continuation character (\) to split a statement into multiple lines.

Example

# Initializing a list using the multi-line statement >>> my_list = [1, \ ... 2, 3\ ... ,4,5 \ ... ] >>> print(my_list) [1, 2, 3, 4, 5]# Evalulate an expression using a multi-line statement >>> eval ( \ ... " 2.5 \ ... + \ ... 3.5") 6.0

Back to top

Implicit line continuation

Implicit line continuation is when you split a statement using either of parentheses ( ), brackets [ ] and braces { }. You need to enclose the target statement using the mentioned construct.

Example

>>> result = (10 + 100 ... * 5 - 5 ... / 100 + 10 ... ) >>> print(result) 519.95

Another Example

>>> subjects = [ ... 'Maths', ... 'English', ... 'Science' ... ] >>> print(subjects) ['Maths', 'English', 'Science'] >>> type(subjects) <class 'list'>

Back to top

Python Indentation

Many of the high-level programming languages like C, C++, C# use braces { } to mark a block of code. Python does it via indentation.

A code block which represents the body of a function or a loop begins with the indentation and ends with the first unindented line.

How many spaces is an indent in Python?

Python style guidelines (PEP 8) states that you should keep indent size of four. However, Google has its unique style guideline which limits indenting up to two spaces. So you too can choose a different style, but we recommend to follow the PEP8.

Why is indentation so important in Python?

Indentation contributes heavily in making Python code readable. In the examples of the previous sections, you might have seen us writing simple expression statements which didn’t have the indentation.

However, for creating compound statements, the indentation will be utmost necessary.

Example

def demo_routine(num): print('I am a demo function') if num % 2 == 0: return True else: return False num = int(input('Enter a number:')) if demo_routine(num) is True: print(num, 'is an even number') else: print(num, 'is an odd number')

Now, also see a scenario when undesired indentation causes an error. So let’s try indenting a simple expression statement.

>>> 6*5-10 File "<stdin>", line 1 6*5-10 ^ IndentationError: unexpected indent

Back to top

Quick Wrap up

If you are planning to be a professional programmer who believes in clean coding practice, then knowing about Python statement, expression, and indentation was much needed.

To get the most out of this tutorial, do run the given examples using Python console in the interactive mode. It has a reasonable command history capability, so you can use the up-arrow key to recover a previous statement.

If you learned something new today, then don’t mind sharing it further. And, follow us on our social media (Facebook/Twitter) accounts to get quick updates.

Best,

TechBeamers

One thought on “Augmented Assignment Python Wrapped

Leave a Reply

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