Python Tutorial

Ad Code

Python Tutorial




What is python language

Python is general purpose, dynamic, object-oriented, interpreted and a high-level programming language known for its simple and easy-to-read syntax. This means that the code is much easier to write and we do not need to specify types in advance.

Python is used in web development, data analysis, artificial intelligence, machine learning, automation, and many more areas. It is also beginner-friendly, so it is quite popular for programmers.

Another major benefit of Python is that it has many libraries and frameworks available, such as Pandas (for data analysis), Django (for web development), and TensorFlow (for AI and machine learning).

Python was invented by Guido van Rossum in 1991 at CWI in Netherland. The idea of Python programming language has taken from the ABC programming language or we can say that ABC is a predecessor of Python language.

Why to use Python:

The following are the primary factors to use python in day-to-day life:

1. Python is object-oriented

Structure supports such concepts as polymorphism, operation overloading and

multiple inheritance.

2. Indentation

Indentation is one of the greatest feature in python

3. It’s free (open source)

Downloading python and installing python is free and easy

4. It’s Powerful

 Dynamic typing

 Built-in types and tools

 Library utilities

 Third party utilities (e.g. Numeric, NumPy, sciPy)

 Automatic memory management

5. It’s Portable

 Python runs virtually every major platform used today

 As long as you have a compactable python interpreter installed, python

programs will run in exactly the same manner, irrespective of platform.

6. It’s easy to use and learn

 No intermediate compile

 Python Programs are compiled automatically to an intermediate form called

byte code, which the interpreter then reads.

 This gives python the development speed of an interpreter without the

performance loss inherent in purely interpreted languages.

 Structure and syntax are pretty intuitive and easy to grasp.

7. Interpreted Language

Python is processed at runtime by python Interpreter

8. Interactive Programming Language

Users can interact with the python interpreter directly for writing the programs

9. Straight forward syntax

The formation of python syntax is simple and straight forward which also makes it popular.

Writing first program:

# Script Begins

      Statement1

      Statement2

      Statement3

# Script Ends

Installation:

There are many interpreters available freely to run Python scripts like IDLE (Integrated
Development Environment) which is installed when we install the python software
from http://python.org/downloads/

Steps to be followed and remembered:

       Step 1: Select Version of Python to Install.

       Step 2: Download Python Executable Installer.

       Step 3: Run Executable Installer.

       Step 4: Verify Python Was Installed On Windows.

       Step 5: Verify Pip Was Installed.

       Step 6: Add Python Path to Environment Variables (Optional)


Working with Python

Python Code Execution:

Python’s traditional runtime execution model: Source code you type is translated to byte
code, which is then run by the Python Virtual Machine (PVM). Your code is automatically
compiled, but then it is interpreted.
There are two modes for using the Python interpreter:
    • Interactive Mode
    • Script Mode

Python Data type

The data stored in memory can be of many types. For example, a student roll number is
stored as a numeric value and his or her address is stored as alphanumeric characters. Python
has various standard data types that are used to define the operations possible on them and
the storage method for each of them.

In Python, numeric data type is used to hold numeric values.
Integers, floating-point numbers and complex numbers fall under Python numbers category. They are defined as int, float and complex classes in Python.

Int:
Int, or integer, is a whole number, positive or negative, without decimals, of unlimited
length.
>>> print(24656354687654+2)
24656354687656
>>> print(20)
20
>>> print(0b10)
2

We can use the type() function to know which class a variable or a value belongs to.
 >>> type(10) 
<class 'int'>
>>> a=11
>>> print(type(a))
<class 'int'>

Float:
Float, or "floating point number" is a number, positive or negative, containing one or more
decimals.
Float can also be scientific numbers with an "e" to indicate the power of 10.
>>> y=2.8
>>> y
2.8
>>> y=2.8
>>> print(type(y))
<class 'float'>

Boolean:
Objects of Boolean type may have one of two values, True or False:
>>> type(True)
<class 'bool'>
>>> type(False)
<class 'bool'>

String: 
1. Strings in Python are identified as a contiguous set of characters represented in the quotation marks. Python allows for either pairs of single or double quotes. 
    • 'hello' is the same as "hello". 
    • Strings can be output to screen using the print function. For example: print("hello"). 

>>> print("RK Tutorial") 
RK Tutorial 
>>> type("RK Tutorial") 
<class 'str'>

If we want to include either type of quote character within the string, the simplest way is to
delimit the string with the other type. If a string is to contain a single quote, delimit it with
double quotes and vice versa:

Escape Sequence

Usual Interpretation of Character(s) After Backslash

“Escaped” Interpretation

\'

Terminates string with single quote opening delimiter

Literal single quote (') character

\"

Terminates string with double quote opening delimiter

Literal double quote (") character

\newline

Terminates input line

Newline is ignored

\\

Introduces escape sequence

Literal backslash (\) character


List:

    · It is a general purpose most widely used in data structures

    · List is a collection which is ordered and changeable and allows duplicate members. (Grow and shrink as needed, sequence type, sortable).

    · To use a list, you must declare it first. Do this using square brackets and separate values with commas.

    · We can construct / create list in many ways.

Ex: >>> list1=[1,2,3,'A','B',7,8,[10,11]]

>>> print(list1)

[1, 2, 3, 'A', 'B', 7, 8, [10, 11]]

What is Variables:

Variables are nothing but reserved memory locations to store values. This means that when
we create a variable you reserve some space in memory.

Rules for Python variables:

     • A variable name must start with a letter or the underscore character

     • A variable name cannot start with a number

     • A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )

     • Variable names are case-sensitive (age, Age and AGE are three different variables)

Python also defines expressions only contain identifiers, literals, and operators. So,

Identifiers: Any name that is used to define a class, function, variable module, or object is
an identifier.

Literals: These are language-independent terms in Python and should exist independently in
any programming language. In Python, there are the string literals, byte literals, integer
literals, floating point literals, and imaginary literals.

Operators: In Python you can implement the following operations using the corresponding
tokens.

Python ─ Basic Operators

Operators are the constructs which can manipulate the value of operands.

·         Python Arithmetic Operators

Assume variable a holds 10 and variable b holds 20, then:

Operator

 Description

Example

+

Addition Adds values on either side of the operator.

a + b = 30

-

Subtraction Subtracts right hand operand from left hand operand.

a – b = -10

*

Multiplication Multiplies values on either side of the operator

a * b = 200

/

Division Divides left hand operand by right hand operand

b / a = 2

% Modulus

Divides left hand operand by right hand operand and returns remainder

b % a = 0

·         Python Comparison Operators

These operators compare the values on either sides of them and decide the relation among them. They are also called Relational operators.

Operator

 Description

Example

==

If the values of two operands are equal, then the condition becomes true.

(a == b) is not true.

!=

If values of two operands are not equal, then condition becomes true.

(a != b) is true.

<> 

If values of two operands are not equal, then condition becomes true.

(a <> b) is true. This is similar to != operator.

> 

If the value of left operand is greater than the value of right operand, then condition becomes true.

(a > b) is not true.

< 

If the value of left operand is less than the value of right operand, then condition becomes true.

(a < b) is true.

>=

If the value of left operand is greater than or equal to the value of right operand, then condition becomes true.

(a >= b) is not true.

<=

If the value of left operand is less than or equal to the value of right operand, then condition becomes true.

(a <= b) is true.

 

·         Python Assignment Operators

Operator

 Description

Example

=

Assigns values from right side operands to left side operand

c = a + b assigns value of a + b into c

+=

It adds right operand to the left operand and assign the result to left operand

c += a is equivalent to c = c + a

-=

It subtracts right operand from the left operand and assign the result to left operand

c -= a is equivalent to c = c - a

*=

It multiplies right operand with the left operand and assign the result to left operand

c *= a is equivalent to c = c * a

/=

It divides left operand with the right operand and assign the result to left operand

c /= a is equivalent to c = c / a

 




Post a Comment

0 Comments

Ad Code