A Taste of Python

Welcome to the world of Python.

Python is very simple language to learn, yet it is quite powerful.

Let’s get a taste of Python by looking at a couple of examples.

Python is Simple

Python is a very simple programming language and it is quite easy to learn.

Writing a hello-world program just takes a single line of code.

print("hello, world!")
hello, world!

It is very handy to use Python as a calculator.

print(1 + 2)
3

Python is Dynamically Typed

Python is a dynamically typed programming language, so you don’t need to declare type of variables.

x = 1
y = 2
print(x + y)
3

It is perfectly fine to reassign a variable to a value of a different type.

x = 1
print(x)

x = "foo"
print(x)
1
foo

While the variables do not have types associated with them, the values do have types. Python is strict about them and it doesn’t allow operations on incompatible datatypes.

1 + "2"
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[5], line 1
----> 1 1 + "2"

TypeError: unsupported operand type(s) for +: 'int' and 'str'

People often assume that a dynamically-typed language is also weekly-typed, which is the case for languages like Perl, PHP and even Javascript. Python is dynamically-typed, but also strongly-typed.

Python uses Indentation

Python uses indentation to identify the code that is part of a block.

marks = 50

if marks > 35:
    print("pass")
else:
    print("fail")
pass

Notice that Python doesn’t use the usual { and } characters to identify code blocks. It just uses indentation to identify the block of code that is part of compound statements like if, else, etc.

Here is another example:

numbers = [1, 2, 3, 4]

for n in numbers:
    print(n)
print("done")
1
2
3
4
done

Python is Expressive

Python has elegant data strucutres and many built-in functions.

Using them the right way leads to very elegant code.

For example, the following example computes the sum of squares of all even numbers below one million.

# sum of squares of all even numbers below one million
sum([n*n for n in range(1000000) if n % 2 == 0])
166666166667000000

Isn’t that almost like restating the problem?

Not impressed yet? Here is another gem to find the longest word in the english dictionary1.

# what is the lonest word in the dictioanary
max(open("/usr/share/dict/words"), key=len)
---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
Cell In[9], line 2
      1 # what is the lonest word in the dictioanary
----> 2 max(open("/usr/share/dict/words"), key=len)

File ~/.local/lib/python3.10/site-packages/IPython/core/interactiveshell.py:324, in _modified_open(file, *args, **kwargs)
    317 if file in {0, 1, 2}:
    318     raise ValueError(
    319         f"IPython won't let you open fd={file} by default "
    320         "as it is likely to crash IPython. If you know what you are doing, "
    321         "you can use builtins' open."
    322     )
--> 324 return io_open(file, *args, **kwargs)

FileNotFoundError: [Errno 2] No such file or directory: '/usr/share/dict/words'

Python is Productive

Python has an extensive standard library and many third-party libraries.

The following example find the most popular repositories on github.

import requests

url = "https://api.github.com/search/repositories"
params = {
    "q": "language:python"
}

data = requests.get(url, params=params).json()

for repo in data['items'][:10]:
    print(repo['full_name'])
public-apis/public-apis
donnemartin/system-design-primer
TheAlgorithms/Python
Significant-Gravitas/AutoGPT
jackfrued/Python-100-Days
AUTOMATIC1111/stable-diffusion-webui
huggingface/transformers
ytdl-org/youtube-dl
521xueweihan/HelloGitHub
nvbn/thefuck

Footnotes

  1. On unix machines, the words in the dictionary are usually available in the file /usr/share/dict/words.↩︎