# Some Language Features in Python

Last Updated on June 21, 2022

The Python language syntax is quite powerful and expressive. Hence it is concise to express an algorithm in Python. Maybe this is the reason why 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 are used to writing in C++ or Java and then transitioning to Python, likely your program is not Pythonic.

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

Kick-start your project with my new book Python for Machine Learning, including step-by-step tutorials and the Python source code files for all examples.

Let’s get started.

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

## Tutorial Overview

This tutorial is divided into two 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 the 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
<<, >> 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
• ternary operator: If you are familiar with the expression (x)?a:b in C, we write it as a if x else b in Python
• comparing if two things are equal can ether use == or is. The == operator is the same as other languages for equality, but is is stricter, reserved for whether the two variable points to the same object

In Python, we allow 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 numbers (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. Associative arrays (or hash tables) are called “dict.” We also have “tuple” as a read-only list and “set” as a 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 gives 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 of 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 lookup 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, in 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 the 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 generally not supposed to be directly used. As a convention (i.e., just a habit as no one is stopping you from doing it), we name internal variables with an underscore or double underscore as a prefix (by the way, double underscores 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 about. 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 clarify 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.

### Want to Get Started With Python for Machine Learning?

Take my free 7-day email crash course now (with sample code).

Click to sign-up and also get a free PDF Ebook version of the course.

## 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 the Python Standard Library documentation (https://docs.python.org/3/library/functions.html). Below are 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:

Compared 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 no direct equivalence to the following C for loop:

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

In a similar sense, there are some functions that manipulate a list (or list-like data structures, which Python calls 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.

The above only highlighted some key features in Python. Surely there is 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 the Standard Library for additional libraries that come with the Python installation:

For books, Learning Python from Lutz is an old but good primer. After that, Fluent Python can help you better understand the internal structure of the language. However, if you want something quick, Al Sweigart’s book can help you quickly pick up the language 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

## Get a Handle on Python for Machine Learning!

#### Be More Confident to Code in Python

...from learning the practical Python tricks

Discover how in my new Ebook:
Python for Machine Learning

It provides self-study tutorials with hundreds of working code to equip you with skills including:
debugging, profiling, duck typing, decorators, deployment, and much more...

### 14 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 #

• 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,

7. Vasanth Sadasivan February 11, 2022 at 5:11 pm #

The column headers are off in your pandas example, the x is over the index, might confuse readers.

• James Carmichael February 12, 2022 at 12:52 pm #

Thank you the feedback Vasanth!