<jupyter_start><jupyter_text>The basics of Python programming In this notebook the basics of Python programming are familiarized and basic examples are provided. Because programming is easiest to learn by doing, this notebook is recommended to be read with Jupyter Notebook (http://jupyter.org/). In Jupyter the examples in the notebook can be run and customated interactively. Also own notes and code can be added beside this text. There is also a short exercise at the end of this document.If you haven't installed Jupyter yet, you can also open the free test version at https://try.jupyter.org/.  Python – what is it? Python is a programming language which newest version at the moment is Python 3 released in 2008. Python is quite easy to learn since the syntax of it is relatively simple. However Python is capable for more complex programming too.In this notebook the basics of Python and some examples are introduced. The main point is to offer a compact overview of the most common things in Python in the sense of using it with CMS Open Data. This notebook won't cover every feature of Python. The official Python documentation is available at https://docs.python.org/3/.  Hello, world! Let's start with the traditional "Hello, world" example. Printing in Python is possible with the _print( )_ function. The function takes a printable object (e.g. a string, a number) as an argument inside the brackets.Try to run the code cell below by first clicking it active and then pressing _Ctrl_ + _Enter_.<jupyter_code>print('Hello, world!')<jupyter_output>Hello, world!<jupyter_text>You can try to write to the code cell below a code that will print your name. Try to run the code after writing.<jupyter_code># These #-separated lines are comments. They will not affect to the code.
# Write a code that will print your name.<jupyter_output><empty_output><jupyter_text>Variables in Python In programming one can save different values that are wanted to be used later to the __variables__. Different variable types are for example integer (int), floating-point number (float) and character string (string).In Python it is easy to create variables since user doesn't have to set the type of the variable. Only the name of the variable and the value to it are needed. For example let's create a variable named _particles_ and save the number 2700 to it:<jupyter_code>particles = 2700<jupyter_output><empty_output><jupyter_text>After doing that we can use the variable e.g. with printing the value saved to it. Note that now there are no quote marks inside the brackets of the _print( )_ function. The quote marks determine a string but now we want to print a variable so we will not use the quote marks. Of course you can modify the code and test what happens if they are added.<jupyter_code>print(particles)<jupyter_output>2700<jupyter_text>Saving a string to a variable is done similarly. Let's create a variable called _welcoming_ and let's save the string "Welcome to Python. Don't worry, it won't bite!". Let's also print that.<jupyter_code>welcoming = "Welcome to Python. Don't worry, it won't bite!"

print(welcoming)<jupyter_output>Welcome to Python. Don't worry, it won't bite!<jupyter_text>Calculating with Python With Python it is possible to do many kinds of calculations. Simplest calculations work by writing the calculation in the code and then running the code. The code will then return the result of the calculation.<jupyter_code>3 + 5
print(3 - 5)
print(3 * 5)

# In Python 2 the result of the division is given as an integer. Python 3 can also return a float.
# In Python 2 this can be fixed by writing "from __future__ import division" in the code cell before the calculation.

print(7 / 5)<jupyter_output>-2
15
1.4<jupyter_text>In more complex calculations variables and lists can be used. In addition to the basic operations there are many ready made mathematical functions and commands available by using __modules__. The usage of modules is handled in the section _Modules_ below.  Functions With __functions__ it is possible to do more complex things than e.g. printing or calculating one value. The function is a subprogram that will be called in the actual program to do the wanted thing. Practically the function is a piece of code that will do the task that it's written to do.For example below we will recall the _abs( )_ function of the Python that will return the absolute value of the number (or the variable) given to it. The value given to the function is called __an argument__.<jupyter_code>abs(-2)<jupyter_output><empty_output><jupyter_text>The function _abs( )_ is an example of a built-in function of Python. Built-in functions are always available in Python. The list of the Python built-in functions can be found at https://docs.python.org/3/library/functions.html.It is also possible to define functions by oneself. With Python 3 this can be done with the _def_ command. Let's for example define a function that will calculate a square root of an argument given to it and also print a short text. Let's name our function as _squaring_.<jupyter_code>def squaring(a):
    print(a*a)
    print("You just used the function!")<jupyter_output><empty_output><jupyter_text>Now we can use the function we created by calling it with the name of it and by giving some argument to it.<jupyter_code>squaring(4)<jupyter_output>16
You just used the function!<jupyter_text>Modules There are lots of different functions already done for different purposes. Often the built-in functions won't be enough, so more functions can be added with __modules__. Modules are practically code files that include functions.Modules can be imported with the _import_ command. For example let's import the _math_ module and use the function _sqrt( )_ of that module. When we call the function _sqrt( )_ we have to tell that we are calling it from the module _math_. This is done by writing the name of the module separated with the dot before the name of the function.<jupyter_code>import math

math.sqrt(9)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.048949892508681994, 'fraction_numerical': 0.006780221597486357, 'mean_word_length': 4.798657718120805, 'pattern_counts': {'":': 0, '<': 36, '<?xml version=': 0, '>': 36, 'https://': 3, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '241186', 'license': 'CC-BY-4.0', 'n_tokens_mistral': 1596, 'n_tokens_neox': 1530, 'n_words': 906, 'path': 'Introduction-to-jupyter/Python-basics.ipynb', 'repo_name': 'ShaliniPandurangan/cms-jupyter-materials-english-1.0', 'size': '6047'}