# Some Language Features in Python

Last Updated on December 9, 2021

The Python language syntax is quite powerful and expressive. Hence it is concise to express an algorithm in Python. Maybe it is the reason it is popular in machine learning, as we need to experiment a lot in developing a machine learning model.

If you’re new to Python but with experience in another programming language, you will sometimes find Python syntax understandable but weird. If you used to write in C++ or Java and transitioning to Python, likely your program is not Pythonic.

In this tutorial, we will cover several common language features in Python that distinct itself from other programming languages.

Let’s get started.

Some Language Features in Python
Photo by David Clode, some rights reserved.

## Tutorial Overview

This tutorial is divided into 2 parts; they are:

1. Operators
2. Built-in data structures
3. Special variables
4. Built-in functions

## Operators

Most of the operators used in Python are same as the other languages. The precedence table is as follows, adopted from Chapter 6 of Python Language Reference (https://docs.python.org/3/reference/expressions.html):

Operator Description
(expressions…), [expressions…], {key: value…}, {expressions…} Binding or parenthesized expression, list display, dictionary display, set display
x[index], x[index:index], x(arguments…), x.attribute Subscription, slicing, call, attribute reference
await x Await expression
** Exponentiation
+x, -x, ~x Positive, negative, bitwise NOT
*, @, /, //, % Multiplication, matrix multiplication, division, floor division, remainder
+, – Addition and subtraction
<<, >> Shifts
& Bitwise AND
^ Bitwise XOR
| Bitwise OR
in, not in, is, is not, <, <=, >, >=, !=, == Comparisons, including membership tests and identity tests
not x Boolean NOT
and Boolean AND
or Boolean OR
if – else Conditional expression
lambda Lambda expression
:= Assignment expression

Some key differences to other languages:

• boolean operators are spelled out, while bitwise operators are characters &, ^ and |
• exponentiation uses 2**3
• integer division uses // and division / always gives you floating point values
• tertary operator: If you are familiar with the expression (x)?a:b in C, we write it as a if x else b in Python
• compare if two things are equal can ether use == or is. The == operator is same as other languages for equality but is is stricter, reserved for whether the two variable points to the same object

In Python, we allows concatenation in comparison operators. For example, to test if a value is between -1 and +1, we can do

but we can also do

## Built-in data structures

As in many other languages, we have integer and floating point data types in Python. But there are also complex number (e.g. 3+1j), boolean as constants (True and False), strings, as well as a dummy type None

But the power of Python as a language lies in the fact that there are container types built-in: Python arrays are called “list” and it will expand automatically and associative arrays (or hash tables) are called “dict”. We also have “tuple” as read-only list, and “set” as container for unique items. In C++ for example, you will need STL to give you these features.

The “dict” data structure is probably the most powerful one in Python and give us some convenience in writing code. For example, in the problem of image classification between dogs and cats, our machine learning model may give you only a value 0 or 1 and if you want to print the name, we can do:

In this case, we make use of the dict value_to_name as a look up table. Similarly, we can also make use of the dict to build a counter:

This will build a dict called counter that maps each character to the number of occurrences in the sentence

Python list also comes with powerful syntax. Unlike some other languages, we can put anything into a list:

and we can use + to concatenate lists. In the above, we use += to extend the list A.

Python list has slicing syntax. For example the above A, we can make A[1:3] to mean elements 1 and 2, i.e., [2, "fizz"] and A[1:1] is an empty list. Indeed we can assign something to a slice to insert or remove some elements. For example:

and then

Tuple has a similar syntax as list, except it is defined using parenthesis:

Tuple is immutable. It means you cannot modify it once it is defined. In Python, if you put several things together with commas to separate each other, it is assumed to be a tuple. The significance of this is that, we can swap two variables in a very clean syntax:

Finally, as you have seen in the examples above, Python strings support substitution on the fly. With the similar template syntax as printf() function in C we can use %s to substitute a string or %d to substitute an integer. We can also use %.3f to substitute a floating point number with 3 decimal places. Below is an example:

But this is just one of the many ways to do it. The above can also be achieved using f-string and format() method.

## Special variables

Python has several “special variables” predefined. __name__ tells the current namespace and __file__ tells the filename of the script. More will be found inside objects but almost all of them are not supposed to be directly used normally. As a convention (i.e., just a habit but not anyone stopping you from doing that) we name internal variables with underscore or double underscore as prefix (by the way, double underscore are pronounced as “dunder” by some people). If you’re from C++ or Java, these are equivalent to the private members of a class, although they are not technically private.

One notable “special” variable that you may often see in Python code is _, just an underscore character. It is by convention to mean a variable that we do not care. Why do you need a variable if you don’t care? That’s because sometimes you hold a return value from a function. For example, in pandas we can scan each row of a dataframe:

In the above, we can see that the dataframe has three columns “x”, “y”, and “z” and the rows are indexed by 0 to 3. If we call A.iterrows() it will give us the index and the row one by one, but we don’t care about the index. We can just create a new variable to hold it but not use it. To make it clear that we are not going to use it, we use _ as the variable to hold the index while the row is stored into variable row.

## Built-in functions

In Python, a small number of functions are defined as built-in while other functionalities are delivered in other packages. The list of all built-in functions are available in Python Standard Library documentation (https://docs.python.org/3/library/functions.html) and below is those defined in Python 3.10:

Not all are used every day but some are particularly notable:

zip() allows you to combine multiple lists together. For example

and it is handy if you want to “pivot” a list of list, e.g.,

enumerate() is handy to let you number a list of items, for example:

This is equivalent to the following if you do not use enumerate:

Compare to other languages, the for loop in Python is to iterate over a predefined range rather than computing the values in each iteration. In other words, there is not direct equivalence to the following C for loop:

and in Python we have to use range() to do the same:

In the similar sense, there are some functions that manipulated a list (or list-like data structures, which Python call it the “iterables”):

• max(a): To find the maximum value in list a
• min(a): To find the minimum value in list a
• sum(a): To find the sum of values in list a
• reverse(a): To iterate from list a from back
• sorted(a): To return a copy of list a with elements in sorted order

We will cover more on these in the next post.

## Further reading

The above only highlighted some key features in Python. Surely there are no more authoritative documentation than the official documentation from Python.org; all beginners should start with the Python tutorial and check the Language Reference for syntax details and Standard Library for additional libraries that comes with the Python installation:

For books, Learning Python from Lutz is an old but good primer. After that, Fluent Python can help you understand better on the internal of the language. However, if you want something quick, the book by Al Sweigart can let you pick up the language fast with examples. Once you get familiar with Python, you may want to learn some quick tips for a particular task from the Python Cookbook.

## Summary

In this tutorial, you discovered some distinctive features of Python. Specifically, you learned:

• The operators provided by Python
• Some use of the built-in data structure
• Some frequently-used built-in functions and why they are useful

### 12 Responses to Some Language Features in Python

1. Hugh Brown December 10, 2021 at 9:15 am #

For the counter example, this is better:

 from collections import Counter sentence = "Portez ce vieux whisky au juge blond qui fume" counter = Counter(sentence)

 

print(counter) 

• Adrian Tam December 10, 2021 at 1:55 pm #

Thanks but the point here is to demonstrate how a dict can be used as a scoreboard

2. Hugh Brown December 10, 2021 at 9:21 am #

> Compare to other languages, the for loop in Python is to iterate over a predefined range rather than computing the values in each iteration. In other words, there is not direct equivalence to the following C for loop

In what way are those not equivalent? In each, one has an integer i in scope throughout the loop that varies from 0 to 99 inclusive. Where’s the part where they are not equivalent?

• Adrian Tam December 10, 2021 at 1:55 pm #

Equivalence here is on the low level implementation. You do not have an evaluation and assignment at each loop. Python for loop is more like scanning a set rather than computing each step

3. petr December 12, 2021 at 4:31 am #

i=0
while i<10:
print(i)
i+=1
pass

4. briand06 December 13, 2021 at 6:28 am #

Great post, as always. Thanks!

• Adrian Tam December 15, 2021 at 6:00 am #

Thanks. Glad you like it.

• James Carmichael December 21, 2021 at 11:40 pm #

Hi Brian…You are very welcome! Please let us know if you have any questions we may help you with.

Regards,

5. j December 17, 2021 at 7:02 pm #

Your print statements would be much more readable if you used format strings

• Adrian Tam December 19, 2021 at 1:35 pm #

Thanks for the suggestion.

6. Prashanth December 25, 2021 at 4:11 pm #

Thank you for your post, Great refresh and reminder for making things easy by pythonifying it

• James Carmichael December 26, 2021 at 6:33 am #

Thank you for the feedback and kind words Prashanth! Let us know which areas of machine learning most interest you.

Regards,