Python is a pretty versatile language. For which applications can you use Python?
- You want to do some quick calculations.
- For your new business, you want to develop a database-driven website.
- Your boss asks you to clean and analyze the results of the latest satisfaction survey.
- All of the above.
Example code added on script.py
"""Where in other programming languages the indentation in code is for readability only, in Python the indentation is very important. Python uses indentation to indicate a block of code. """
To add comments to your Python script, you can use the # tag. These comments are not run as Python code, so they will not influence your result.
Python is perfectly suited to do basic calculations. Apart from addition, subtraction, multiplication and division, there is also support for more advanced operations such as:
- Exponentiation: . This operator raises the number to its left to the power of the number to its right. For example 42 will give 16.
- Modulo: %. This operator returns the remainder of the division of the number to the left by the number on its right. For example 18 % 7 equals 4.
The code in the script on the right gives some examples.
Suppose you have $100, which you can invest with a 10% return each year. After one year, it's 100×1.1=110 dollars, and after two years it's 100×1.1×1.1=121. Add code on the right to calculate how much money you end up with after 7 years.
Python is completely object oriented, and not "statically typed". You do not need to declare variables before using them, or declare their type. Every variable in Python is an object.
[This tutorial will go over a few basic types of variables] (https://www.learnpython.org/en/Variables_and_Types).
In the previous exercise, you worked with two Python data types:
- int, or integer: a number without a fractional part. savings, with the value 100, is an example of an integer.
- float, or floating point: a number that has both an integer and fractional part, separated by a point. factor, with the value 1.10, is an example of a float.
Next to numerical data types, there are two other very common data types:
- str, or string: a type to represent text. You can use single or double quotes to build a string.
- bool, or boolean: a type to represent logical values. Can only be True or False (the capitalization is important!).
Filip mentioned that different types behave differently in Python.
When you sum two strings, for example, you'll get different behavior than when you sum two integers or two booleans.