1, the introduction

Python is a high-level programming language for general-purpose programming. It is an open source, interpreted, object-oriented programming language. Python was created by Dutch programmer Guido van Rossum. The Name of the Python programming language comes from the British sketch comedy series Month Python’s Flying Circus. The first version was released on February 20, 1991.

1.1 Why Python?

It is a programming language that closely resembles a human language and is therefore easy to learn and use. Python is used by a variety of industries and companies (including Google), and it has been used to develop Web applications, desktop applications, systems administration, and machine learning libraries.

1.2 Environment Settings

Install Python

To run Python scripts, you need to have Python installed. Let’s download Python. If you are a Windows user. Click the button circled in red.

If you are a macOS user. Click the button circled in red.

To check if Python is installed, write the following command on your device terminal.

Python version –

As you can see from the terminal, I am currently using Python version 3.7.5. Your Python version may be different from mine and should be version 3.6 or higher. It would be great if you could see the Python version. Python is installed on your machine. Go on to the next video.

Python Shell

Python is an interpreted scripting language, so no compilation is required. This means that it executes code line by line. Python comes with a Python Interactive Shell. It is used to execute a single Python command and get the result.

The Python Shell waits for Python code from the user. When you type the code, it interprets the code and displays the results on the next line. Open a terminal or command prompt (CMD) and write:

Python
Copy the code

The Python interactive shell is open, and it is waiting for you to write a Python script. You will write a Python script next to this symbol >>> and then click Enter. Let’s write our first script on a Python script shell.

With the above step complete, you can write your first Python script on the Python interactive shell. How do we turn off the Python interactive shell? To close the shell, >> write the exit() command next to this symbol and press Enter.

Now you know how to open the Python interactive shell and how to exit it.

If you write a script that Python understands, Python will give you the result, otherwise it will return an error. Let’s make a deliberate error and Python will return an invalid syntax error.

As you can see from the Error returned, Python is smart enough to know that the Error we made was Syntax Error: invalid Syntax. Using x as multiplication in Python is a syntax error because x is not a valid syntax in Python. We use the asterisk * instead of x for multiplication. The error returned clearly shows what you want to fix.

The process of identifying and eliminating errors from a program is called debugging. Let’s debug it with * instead of x.

Fix syntax errors

Our bug was fixed, the code ran, and we got the results we expected. As a programmer, you see these errors every day, so be good at debugging and understand the types of errors you are facing. Sometimes the errors you might encounter are SyntaxError, IndexError, NameError, ModuleNotFoundError, KeyError, import error, AttributeError, type error, ValueError, ZeroDivisionError, etc.

Let’s practice using the Python interactive shell. Go to your terminal or command prompt and type the word python.

The Python interactive shell has been opened. Let’s do some basic math: addition, subtraction, multiplication, division, modulus, exponent.

Before writing any Python code, let’s do some math:

2 + 3 = 5 3-2 = 1 3 * 2 = 6 3/2 = 1.5 3 ^ 2 = 3 x 3 = 9

In Python, we have the following additional operations:

3%2 = 1 => means to find the remainder 3 // 2 = 1 => means to remove the remainder

Let’s change the mathematical expression above to Python code and write a comment at the beginning of the shell.

We can leave some text in our code to make our code more readable. Comments in Python start with the hash# symbol.

 # comments start with hash
 # This is a Python comment because it begins with the (#) symbol
Copy the code

Math on the Python shell

Write exit() on the shell to close the open shell, open it again, and practice writing text on the Python shell.

Write strings on the Python shell

Python 1.3 basis

Python syntax

Python scripts can be written in the Python shell or code editor. Python files have a.py extension.

Python indentation

Indentation is white space in text. Many languages use indentation to improve code readability, but Python uses indentation to create code blocks. In other programming languages, braces are used to create blocks of code instead of indentation. One of the most common mistakes in writing Python code is the indentation error.

annotation

Comments are important to make code more readable and to leave comments in our code. Python does not run the comment part of our code. Any text in Python that begins with hash(#) is a comment.

Example: single-line comment

# This is the first comment
# This is the second comment
# Python is eating the world
Copy the code

Example: multi-line comments

If not assigned to a variable, triple quotes can be used for multi-line comments

"" "This is a multi-line comment and a multi-line comment requires multiple lines. Python is eating the world." ""
Copy the code

The data type

There are several types of data types in Python. Let’s start with the most common. Other sections will cover the different data types in detail. For now, let’s walk through the different data types and get familiar with them.

digital

  • Integer: integers (negative, zero, and positive) Example:… -3, -2, -1, 0, 1, 2, 3…
  • Floating point: decimal number example… -3.5, -2.25, -1.0, 0.0, 1.1, 2.2, 3.5…
  • Complex examples 1 + j, 2 + 4J

String A collection of one or more characters under single or double quotation marks. If a string has more than one sentence, then we use triple quotes.

Example:

'Asabeneh' 
'Finland' 
'Python' 
'I love teaching' 
'I hope you enjoyed the first day of the 30DaysOfPython Challenge'
Copy the code

Boolean The Boolean data type is a True or False value. T and F should always be uppercase.

Example:

   True   Is the light on? ifIf the indicator is on, the value isTrue False  Is the light on? If closed, the value is False
Copy the code

Lists Python lists are ordered collections that allow you to store items of different data types. Lists are similar to arrays in JavaScript.

Example:

[ 0 , 1 , 2 , 3 , 4 , 5 ]   # all are the same data type - number list
[ 'Banana' , 'Orange' , 'Mango' , 'Avocado' ] # List of all the same data types - A strings (fruit)
[ 'Finland' , 'Estonia' , 'Sweden' , 'Norway' ] # All the same data types - list of strings (country)
[ 'Banana' , 10 , False , 9.81 ]# Different data types in the list - strings, integers, Booleans, and floating point numbers
Copy the code

Dictionaries Python dictionary objects are unordered collections of data in the form of key-value pairs.

Example:

{
 'first_name' : 'Asabeneh' ,
 'last_name' : 'Yetayeh' ,
 'country' : 'Finland' , 
 'age' : 250 , 
 'is_married' : True ,
 'skills': ['JS' , 'React' , 'nodes'.'Python']}Copy the code

Tuples A tuple is an ordered collection of different data types (such as lists), but once created, a tuple cannot be modified. They are immutable.

Example:

( 'Asabeneh' , 'Pawel' , 'Brook' , 'Abraham' , 'Lidiya' ) # the name
Copy the code
(Earth, Jupiter, Neptune, Mars, Venus, Saturn, Uranus, Mercury)# planetsCopy the code

Collections COLLECTIONS are collections of data types similar to lists and tuples. Unlike lists and tuples, collections are not ordered collections of items. As in mathematics, a set in Python stores only a single item.

We’ll look at each Python data type in detail in a later section.

Example:

{ 2 , 4 , 3 , 5 }
{ 3.14 , 9.81 , 2.7 } Order is not important in the set
Copy the code

Checking data types

To check the data types of some data/variables, we use the type function. In the following terminals, you will see different Python data types:

Python file

First open your project folder, 30DaysOfPython. If you do not have this folder, create a folder called 30DaysOfPython. In this folder, create a file called Helloworld.py.

To see our results when the Python shell prints without print, we should use the built-in function *print(). In print () the built-in function has one or more arguments printed as follows (” arument1 “, “argument 2”, “argument 3”). See the example below.

Example:

The file name is helloworld.py

# Day 1-30DaysOfPython Challenge

print ( 2  +  3 )              # addition (+)
print ( 3  -  1 )              # subtract (-)
print ( 2  *  3 )              # multiplication (*)
print ( 3  /  2 )              # division (/)
print ( 3  **  2))# index (* *)Print (3  %  2 )              # the modulus (%)Print (3  //  2)             # floor division operator (//)

Check the data type
print ( type ( 10 ))           # Int 
print ( type ( 3.14 ))         # Float 
print ( type ( 1  +  3j ))       # Complex number 
print ( type ( 'Asabeneh' ))   # String 
print ( type ([ 1 , 2 , 3 ]))    # List 
print ( type ({ 'name' : 'Asabeneh'})) # dictionaryPrint (type ({ 9.8 , 3.14 , 2.7 }))     # setPrint (type (( 9.8 , 3.14 , 2.7 )))     # tuples
Copy the code

To run the Python file, check the following figure. You can run the Python file by running the green button on Visual Studio Code or by typing Python Helloworld.py in the terminal.

For more on Python learning experiences, follow me and keep me updated.