Skip to content

Latest commit

 

History

History
475 lines (282 loc) · 13.3 KB

Part1-Introduction-to-Python.md

File metadata and controls

475 lines (282 loc) · 13.3 KB

Python Programming
INTRODUCTION TO PYTHON

INTRODUCTION TO PYTHON

WHAT IS PYTHON 🤔

Python is a high-level open-source programming language, which means that both humans and machines can understand it. It is among the most widely used programming languages because of its easiness while working compared to other languages like C, C++, JAVA, etc

HISTORY OF PYTHON 👑

Python was originated in the late 1980s by a Dutch programmer Guido van Rossum for whom he was the benevolent dictator for life (BDPL) until July 2018. There were many python versions like python2 and python3 present most of them are using python3.

WHO CAN LEARN PYTHON. 💻 👦 👨

Any beginner interested in the beautiful world of programming can learn python because it is a programming language that everyone can understand without having knowledge in it.

WHY PYTHON? 🤔

Here are few Advantages of python because of which python is chosen by a wide range of people.

  1. Python is open source and is used for community development.

  2. It is easy to learn and understand.

  3. It portable and it is interactive.

  4. It is an object-oriented programming language and also a high-level programming language.

  5. It is a dynamically typed programming language that means no need to mention the data type based on the value assigned.

  6. It has extensive help of libraries like NumPy, pandas, so on...

APPLICATIONS OF PYTHON 📋

Web Development: Using libraries like Flask and Django websites can be developed.

Machine Learning: It is branch of Artificial Intelligence where it consists of vast range of libraries. 📊

Prototyping: It is nothing but the models which are used to explain the design of the application or system in brief to the clients.

GUI based applications: Python Libraries are used to develop Games or Scientific Applications. 🎮

INSTALLATION OF PYTHON 💻

System requirements for Python Installation:

  1. Operating system: Linux- Ubuntu 16.04 to 17.10, or Windows 7 to 10, with 2GB RAM (4GB preferable)
  2. You have to install Python 3.6 and related packages, please follow the installation instructions given below as per your operating system

Procedure to install Python 3 on Windows OS:

Install Python 3.8:

  1. To follow the installation procedure, you need to be connected to the Internet.
  2. Visit https://www.python.org/downloads/release/python-368/ 3.At the bottom locate Windows x86-64 executable installer for 64 bits OS and Windows x86 executable installer for 32 bits OS
  3. Click on the located installer file to download.
  4. After download completes, double click on the installer file to start the installation procedure.
  5. Follow the instructions as per the installer.

Important Note: After double clicking the installer, check mark the option “Add Python 3.8 to PATH”

Check if Python is Installed Correctly: ✔️

  1. Open the cmd window
  2. Run the command “python --version” or “python3 --version” in the cmd window and press Enter.
  3. You should get an Output Python 3.8 (or an output resembling this)

Install Numpy, Matplotlib and Ipython:

1.Open the cmd window (Click here to see how to open cmd window)

2.Type in the following commands followed by the Enter key:

  • python -m pip install numpy
  • python -m pip install ipython
  • python -m pip install matplotlib

Check if Numpy, Matplotlib and Ipython are Installed Correctly: ✔️

1.Open the cmd window (Click here to see how to open cmd window)

2.Run the command ipython3 and press Enter to open the IPython console.

3.You should get the following output (or an output resembling this):

Python 3.8 (default, Oct 22 2018, 11:32:17)

Type 'copyright', 'credits' or 'license' for more information

IPython 7.2.0 -- An enhanced Interactive Python. Type '?' for help.

In[1]: _

4.In the IPython console, type in the command import numpy, matplotlib

5.Press Enter

6.Type in the command %pylab

7.Press Enter

8.If all commands are executed without throwing any errors/exceptions as output (no other output will be displayed), then the packages are installed successfully.

The procedure to install Python 3 on Ubuntu Linux Os.

1.To follow the installation procedure, you need to be connected to the Internet.

2.Open the terminal by pressing Ctrl + Alt + T keys together.

3.Install Python 3.8:

a. For Ubuntu 16.04 and Ubuntu 17.04:

i. In the terminal, run the command sudo apt-get install python3.6

ii. Press Enter.

iii. The terminal will prompt you for your password, type it in to the terminal.

iv. Press Enter.

b. For Ubuntu 17.10 and above, the system already comes with Python 3.6installed by default

Check if Python is Installed Correctly: ✔️

1.Open the terminal by pressing Ctrl + Alt + T keys together.

2.Run the command python3 --version in the terminal and press Enter.

3.You should get an Output Python 3.6 (or an output resembling this)

Install Numpy, Matplotlib and Ipython:

1.Open the terminal by pressing Ctrl + Alt + T keys together.

2.In the terminal, run the command sudo apt-get install python3-tk

3.The terminal may prompt you for your password, type it in to the terminal.

4.Press Enter

5.Type in the following commands followed by the Enter key:

a. python -m pip install numpy

b. python -m pip install ipython

c. python -m pip install matplotlib

Check if Numpy, Matplotlib and Ipython are Installed Correctly: ✔️

1.Open the terminal by pressing Ctrl + Alt + T keys together.

2.Run the command ipython3 and press Enter to open the IPython console.

3.You should get the following output (or an output resembling this);

Python 3.8 (default, Oct 22 2018, 11:32:17)

Type 'copyright', 'credits' or 'license' for more information

IPython 7.2.0 -- An enhanced Interactive Python. Type '?' for help.

In[1]: _

4.In the IPython console, type in the command import numpy, matplotlib

5.Press Enter

6.Type in the command %pylab

7.Press Enter

8.If all commands are executed without throwing any errors/exceptions as output (no other output will be displayed), then the packages are installed successfully.

BASIC PYTHON SYNTAX 📝

1. Print()

All of us would have learned other programming language with evergreen example of printing "Hello world !!!". It's same for python, like any other programming language print function is the most important and baby step that you take while learning any particular language.

Print function prints the specified message or text on the screen, or other standard output device.

Lets begin by printing "Hello World !" example.

print("Hello World !")
Hello World !

String variables can be declared either by using single or double quotes.

Lets look at an other example

print "Hello World !"
  File "<ipython-input-29-512c8e33c649>", line 1
    print "Hello World !"
          ^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print("Hello World !")?

From the above example we can understand that paranthesis is a must else it will raise syntax error.

Why do we say print as function rather than statement? let's check.

type(print)

From the above example we can conclude that print is not a statement instead its a function.

Lets learn how to print strings,numbers ,basic calculation.

  • to print strings : print("Sentence/ string")
  • to print numbers : print(number)
  • to print basic operation : print(number operator number)
print("Welcome")
print(1)
print(2+3)
print(7-2)
Welcome
1
5
5

2.Value initialization

  • Python has no command for declaring a variable.
  • A variable is created the moment you first assign a value to it.
  • a variable is a label that you can assign a value to it
  • Use the variable_name = value to create a variable.

Lets learn how to initialize

  • for strings : variable= " string or a sentence"
  • for integers : variable=numbers
  • for operations : variable= number1 operator number2
x = 5
y = "Apple"
print(x)
print(y)
5
Apple

Casting

  • it is used to specify the datatype of a variable.
x = str(3)    # x will be '3'
y = int(3)    # y will be 3
z = float(3)  # z will be 3.0

You can get the data type of a variable with the type() function.

type(x)
str
x = 5
y = "Apple"
print(type(x))
print(type(y))
<class 'int'>
<class 'str'>

3.input() / value input

  • input() is a built-in function.
  • reads a line from the input (usually from the user).
  • This function takes exactly what is typed from the keyboard, convert it to string and then return it to the variable in which we want to store.

syntax : input(prompt)

prompt is optional, it is a string representing a default message before the input.

x = input('Enter your name:')
print('Good evening, ' + x)
Enter your name:world
Good evening, world

Lets learn how to take input for strings, integers and float variable.

  • for strings : variable=input(string)
  • for integers : variable=int(input())
  • for float : variable=float(input())

Example :

x = input("Enter the value for x: ") #lets give input string
print(x)
y = input("Enter the value for y: ") #lets give input integer
print(y)
z= input("Enter the value for z: ") #lets give input float
print(z)
Enter the value for x: world
world
Enter the value for y: 9
9
Enter the value for z: 6.0
6.0

4.Comments

  • Comments are used to explain python code
  • Comments are descriptions that help programmers to understand the intent and functionality of the program, and comments are completely ignored by the python interpreter.

We use "#" symbol at the beginning of each line to create multiple line comments.

x=1
y=2
z=3
print(x) #comment1
print(y) #comment2
print(z) #comment3
1
2
3

We can also use string literals for multi line comments, Python will ignore string literals that are not assigned to a variable.

"""
This is a comment
written in
more than just one line
"""
print("Hello, World!")
Hello, World!

Python Identifiers

  • Identifier is the name given to a variable,function,class, module or other objects.
  • An identifier can begin with an alphabet (A-Z or a-z), or an underscore(_) and can include any number of letters,digits,or underscores
  • Spaces are not allowed

Example :

  • MyName
    space is not allowed
  • My_Name
    cannot start with a digit, can use underscore(_)

Reserved Keywords 📃

  • Python has a list of reserved words known as keywords.
  • every keyword has a specific purpose and use

Here are the List of keywords in Python :

  • and
  • as
  • assert
  • break
  • class
  • continue
  • def
  • del
  • elif
  • else
  • except
  • False
  • finally
  • for
  • from
  • global
  • if
  • import
  • in
  • is
  • lambda
  • None
  • nonlocal
  • not
  • or
  • pass
  • raise
  • return
  • True
  • try
  • while
  • with
  • yield