Python 101: Introduction to Modern Python

smartjeff

Jeff Odhiambo

Posted on February 8, 2022

Python 101: Introduction to Modern Python

Did you know that python is the most rapidly growing programing language? Did you know you can build web applications, games, consoles, and more using python?
python has become a world famous and powerful programming language and has been ranked the best in the year 2022. It's friendly, simple, and easy to learn.

What is python?

Python is an interpreted, general, high-level programming language invented by Guido van Rossum and was conceived in the late 1980s and released in 1991.

What makes it simple?

since its syntax emphasizes readability thus easy and simple to read and write.

Sample code

printing hello world on the console in java and python
java:

public class HelloWorld{
  public static void main(String[] args){
    System.out.println("Hello World");
  }
}
Enter fullscreen mode Exit fullscreen mode

python:
print("Hello World")

What next?

Let's get started with checking if python is installed in your system. run python on your terminal or cmd.
if the output is similar to the one below then its installed otherwise we will need to install it.

Python 3.10.0 (tags/v3.10.0:b494f59, Oct  4 2021, 19:00:18) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
Enter fullscreen mode Exit fullscreen mode

Python installation steps

  1. Visit https://www.python.org/downloads/
  2. Select your Operating system(Windows,Linux/UNIX,MacOS,Other
  3. Select the release or version, click on it to download.
  4. Double click on the file to execute and install. For window mark "add to system paths" An alternative way to install python on Linux is to run sudo apt install python3 or sudo apt install python on the terminal.

For more check on the python documentation for more documentations.

Let's test our installed python

  1. Open Terminal on Linux or cmd on windows
  2. Run the python --version command to check if you installed the correct version. if python is installed, the output will be similar to as below:
C:\Users\jeff>python --version
Python 3.10.0

C:\Users\jeff>
Enter fullscreen mode Exit fullscreen mode

Print Hello World on the terminal or cmd by typing
python followed by print("Hello World")

Output:

C:\Users\jeff>python
Python 3.10.0 (tags/v3.10.0:b494f59, Oct  4 2021, 19:00:18) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> print("Hello World")
Hello World
>>>
Enter fullscreen mode Exit fullscreen mode

Next read input from the user, store it in a variable, and say hi to them.
Declare a variable name and assign a value from the user using = input("Enter your name >>> ") as shown below.

>>> name = input("Enter your name : ")
Enter your name : Jeff
>>> print("Hello Mr. " + name)
Hello Mr. Jeff
>>>
Enter fullscreen mode Exit fullscreen mode

Performing basic mathematical operations
Declare two-variables number_1 and number_2 and assign values 20 and 10 to each respectively as shown below

>>> number_1 = 20
>>> number_2 = 10
>>> print(number_1 + number_2)
30
>>> print(number_1 - number_2)
10
>>> print(number_1 / number_2)
2.0
>>> print(number_1 * number_2)
200
>>>
Enter fullscreen mode Exit fullscreen mode

In the above example we declared variables such as name, number_1, and number_2, but what are they.
Variables are names of storage locations whose values can be changed during program execution, they store values e.g. name stored the value jeff, number_1 stored 20 and number_2 stored 10.

There are various rules for naming variables:

  1. It should not have space, first name is wrong but first_name is correct.
  2. It should not contain reserved words such as def, class, etc.
  3. Should not begin numbers
  4. Should not contain special characters such as ,, ., ! etc.

Imagine writing 100+ lines of code, it will not be effective using the terminal. in this case, we will need a text editor such as sublime, visual studio codes, etc
To download the Visual studio code click on the download then select visual studio code that is compatible with your operating system.
To write these lines of code into a file. and a python file has an extension of .py.
For example, a HelloWorld file is saved as HelloWorld.py

To run this file run python HelloWorld.py to execute.

In python, some libraries can be imported while performing some operations e.g. math library for complex operations such as sin, cos, tan, log, raising to a given power, etc.

Use case:

>>> import math
>>> print(math.log(100))
4.605170185988092
>>> print(math.sin(30))
-0.9880316240928618
>>> print(math.cos(30))
0.15425144988758405
>>> print(math.tan(30))
-6.405331196646276
>>>
Enter fullscreen mode Exit fullscreen mode

The above examples are some of the basic simple operations that can be performed in Python. Other applications such as Web development, game development, AI, Machine learning among others.

💖 💪 🙅 🚩
smartjeff
Jeff Odhiambo

Posted on February 8, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related