Python programming
PYTHON PROGRAMMING
Python is a high-level, general-purpose and a very popular programming language.
Python programming language (latest Python 3) is being used in web development, Machine Learning
applications, along with all cutting edge technology in Software Industry.
Python Programming Language is very well suited for Beginners, also for experienced programmers with
other programming languages like C++ and Java.
It was initially designed by Guido van Rossum in 1991 and developed by Python Software Foundation
On 16 October 2000, Python 2.0 was released with many new features.
On 3rd December 2008, Python 3.0 was released with more testing and includes new
features.
It was mainly developed for emphasis on code readability, and its syntax all programmers
to express concepts in fewer lines of code.
Python is a programming language that lets you work quickly and integrate systems more efficiently
•
•
•
•
•
•
•
•
•
•
•
•
•
•
some facts about Python Programming Language:
Python is currently the most widely used multi-purpose, high-level programming language.
Python allows programming in Object-Oriented and Procedural paradigms.
Python programs generally are smaller than other programming languages like Java.
Python language is being used by almost all tech-giant companies like – Google, Amazon, Facebook, Instagram,
Dropbox, Uber… etc.
The biggest strength of Python is huge collection of standard library which can be used for the following:
Machine Learning
GUI Applications (like Kivy, Tkinter, PyQt etc. ) Web frameworks like Django (used by YouTube, Instagram, Dropbox) Image processing (like OpenCV, Pillow) Web scraping (like Scrapy, BeautifulSoup, Selenium)
Test frameworks
Multimedia
Scientific computing
Text processing and many more..
•
•
•
•
•
•
•
•
•
•
•
•
Advantages : Simple and easy to learn
Extensive support libraries(NumPy for numerical calculations, Pandas for data analytics etc) Open source User-friendly data structures High-level language Dynamically typed language(No need to mention data type based on the value assigned, it takes data type) Procedure oriented &Object-oriented language Portable and Interactive
Platform independent
Extensible
Interpreted Language
Portable across Operating systems
•
•
•
•
•
•
•
Applications : Graphic design, image processing applications, Games, and Scientific
Enterprise and Business applications Operating Systems Education
Database Access
Language Development Software Development
• 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 major Python versions: Python 2 and Python 3. Beginning with Python programming:
1) Finding an Interpreter:
we need to have an interpreter to interpret and run our
programs
Windows: There are many interpreters available freely to run Python scripts like IDLE
(Integrated Development Environment) which is installed when you install the python
software from http://python.org/downloads/
2) Writing our first program: #basic program
Print(“welcome to python programming”)
Python 2, “print” is not a function but a keyword and therefore can be used without parentheses. However, in Python 3, it is a function and must be invoked with parentheses.
• Without passing python script file to the interpreter, directly execute code to Python prompt.
Once you’re inside the python interpreter, then you can start.
>>> print("hello world")
hello world
Data types:
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.
•
•
•
•
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
>>> 20
20
>>> 0b10
2
>>> a=10
>>> print(a)
10
To verify the type of any object in Python, use the type() function:
>>> 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'>
>>> type(.4)
<class 'float'>
>>> 2.
2.0
Example:
x = 35e3
y = 12E4
z = -87.7e100
print(type(x))
print(type(y))
print(type(z))
Output:
<class 'float'>
<class 'float'>
<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("mrcet college")
mrcet college
>>> type("mrcet college")
<class 'str'>
>>> print('mrcet college')
mrcet college
>>> " "
' '
•
•
•
Suppressing Special Character:
Specifying a backslash (\) in front of the quote character in a string “escapes” it and causes
Python to suppress its usual special meaning. It is then interpreted simply as a literal single
quote character:
>>> print(“welcome to JPNCE (\') college")
welcome to JPNCE(') college
escape sequences which cause Python to suppress the usual
special interpretation of a character in a string:
>>> print('a\
....b')
a....b
>>> print('a\
b\
c')
abc
>>> print('a \n b')
a b
>>> print("mrcet \n college")
mrcet
college
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
Keywords :
These are the dedicated words that have special meaning and functions. Moreover, compiler defines these words. Moreover, it does not allow users to use these words.. Python compiler has the following keywords
•
•
•
•
•
Identifiers :
Identifiers represent the programmable entities. The programmable entities include
user-defined names, variables, modules, and other objects.
Rules:An identifier can be a sequence of lower case (or) upper case (or) integers (or) a
combination of any.
The identifier name should start with the lower case (or) upper case (It must not start
with digits)The identifier name should not be a reserved word.
Only Underscore (_) is allowed to use as a special character in identifier names.
The length of the identifier name should not be more than 79 characters
•
Literals :
Literals are used to define the data as a variable (or ) constants. Python has 6 literals
tokens
•
•
•
•
•
•
•
•
•
An operator is a symbol that will perform mathematical operations on variables or on values. Operators operate on operands (values) and return a result. Python has 7 types of operators that you can use:
Arithmetic Operators
Relational Operators
Assignment Operators
Logical Operators
Membership Operators
Identity Operators
Bitwise Operators
•
•
Types of Python Operators
1. Python Arithmetic Operators
•
•
•
•
Addition Operator
The addition operator [+] adds two values and gives their sum. num1=7
>>> num2=4
>>> num1+num2
>>> 7+4.1
11.1
Subtraction Operator The subtraction operator [-] subtracts second value from first and gives their difference.
>>> num1=7
>>> num2=4
>>> num1-num2
>>> 7-4.1
2.9000000000000004
•
•
•
Multiplication Operator
The multiplication operator [*] multiplies two values and gives their product. >>> num1=7
>>> num2=4
>>> num1*num2
>>> 7*4.1
28.699999999999996
Division operator
The division operator [/] divides one value by second and gives their quotient. >>> num1=7
>>> num2=4
>>> num1/num2
1.75
>>> 7/4.1
1.707317073170732
Exponentiation
The exponentiation operator [**] raises one value to power of second. >>> num1=7
>>> num2=4
>>> num1**num2
2401
>>> 7**4.1
2916.7685197377978
•
•
•
•
•
•
•
•
•
Floor Division
The floor division operator [//] divides one value by second and gives their quotient rounded to the next smallest whole number. >>> num1=7
>>> num2=4
>>> num1//num2
1
>>> 7**4.1
1.
Modulus
The modulus operator [%] divides one value by second and gives their remainder. num1=7
>>> num2=4
>>> num1%num2
3
>>> 7%4.1
2.9000000000000004
In this example, we floor-divided integers by integers and integers by floats.
• P
•
•
•
•
•
Now, let’s talk about relational operators. They are also called comparison operators and they compare values. Python has 6 relational operators:
> (Greater than)
< (Less than)
== (Equal to)
!= (Not equal to)
>= (Greater than or equal to)
<= (Less than or equal to)
a. Greater than
The greater than operator [>] returns True if the first value is greater than the second. >>> num1=7
>>> num2=4
>>> num1>num2
True
>>> 7>4.1
Less than
The less than operator [<] returns True if the first value is less than the second. >>> num1=7
>>> num2=4
>>> num1<num2
False
>>> 7<4.1
False
Equal to
The equal to operator [==] returns True if the first value is equal to the second. >>> num1=7
>>> num2=4
>>> num1==num2
False
>>> 7==4.1
False
Not equal to
The not equal to operator [!=] returns True if the first value is not equal to the second. >>> num1=7
>>> num2=4
>>> num1!=num2
True
>>> 7!=4.1
True
Greater than or equal to
The greater than or equal to operator [>=] returns True if the first value is greater than or equal to the second. >>> num1=7
>>> num2=4
>>> num1>=num2
True
>>> 7>=4.1
True
Less than or equal to
The less than or equal to operator [<=] returns True if the first value is smaller than or equal to the second. Example:
>>> num1=7
>>> num2=4
>>> num1<=num2
False
>>> 7<=4.1
False
>>> num1=7
>>> print(num1)
7
>>> num1=7
>>> num2=4
>>> num+=num2
>>> print(num1)
11
>>> num1=7
>>> num2=4
>>> num1-=num2
>>> print(num1)
>>> num1=7
>>> num2=4
>>> num1*=num2
>>> print(num1)
>>> num1=7
>>> num2=4
>>> num1/=num2
>>> print(num1)
1.75
>>> num1=7
>>> num2=4
>>> num1%=num2
>>> print(num1)
3
>>> num1=7
>>> num2=4
>>> num1**=num2
>>> print(num1)
2401
>>> num1=7
>>> num2=4
>>> num1//=num2
>>> print(num1)
1
•
•
•
•
•
•
Python Logical Operators
They can combine conditions. Python has 3 logical operators: and (Logical and)
or (Logical or)
not (Logical not)
Logical and
The logical and operator returns True if both values are True. Otherwise, it returns False. Example:
>>> True and False
False
>>> 3 and 4
Logical or
The logical or operator returns True if even one value is True. It returns False if both values are False. >>> True or False
True
>>> 3 or 4
3