Blog 1 Python 1

CONTENTS

S.NO	TITLE	
1	INTRODUCTION BASICS	
2	CONDITIONAL STATEMENTS AND LOOPS	14
3	LISTS, TUPLES, SETS AND DICTIONARIES	22
4	FUNCTIONS	41
5	PYTHON ARRAYS	44
6	PYTHON CLASSES AND OBJECTS	47
7	INHERITANCE	50
8	ITERATORS	54
9	MODULES	58
10	DATES	62
11	JSON (JAVA SCRIPT OBJECT NOTATION)	65
12	REGEX (REGULAR EXPRESSION)	69
13	PIP (PYTHON PACKAGE MANAGER)	77
14	TRY..EXCEPT	80
15	FILE HANDLING	83
16	MySQL	89
17	MongoDB	111



 
PYTHON TUTORIAL

CHAPTER 1

INTRODUCTION BASICS


Extremely popular among developers, Python is a good general-purpose language that can be used in a variety of applications. For those with an understanding of English, Python is a very human-readable programming language, allowing for quick comprehension.

Because Python supports multiple styles including scripting and object-oriented programming, it is considered to be a multi-paradigm language that enables programmers to use the most suitable style to complete a project.

Increasingly used in industry, Python offers a lot of potential for those who would like to begin coding while also being a good choice for those looking to pickup an additional programming language.

Developed in the late 1980s and first published in 1991, Python was authored by Guidovan Rossum, who is still very active in the community.

Conceived as a successor to the ABC programming language, Python’s first iteration already included exception handling, functions, and classes with inheritance.

When an important Usenet newsgroup discussion forum called comp.lang.python was formed in 1994, Python’s userbase grew, paving the way for Python to become one of the most popular programming languages for open source development.

VERSIONS
PYTHON 2
PYTHON 2.7
PYTHON 3     - THIS IS CURRENT VERSION
PYTHON 3.6.5 

Software name
python-3.6.5.exe

After installation set PATH variable by

Right Click ThisPC from desktop -> Properties ->Advanced System Settings – Environment Variable.
a)	Create a variable named PATH and add the value C:\Users\ type  cmd and press enter
C:\users\admin>Python
Type the command Python. A >> sign will appear
Type exit() and return to command prompt.

In >> , we can enter python commands.
Or we type the python code in a file and name it with extension .py and run by
C:\users\admin>python myprog1.py 
And press enter.
1.1 What is Python?
Python is a popular programming language. It was created by Guido van Rossum, and released in 1991.
It is used for:
•	web development (server-side), 
•	software development, 
•	mathematics,
•	system scripting.
What can Python do?
•	Python can be used on a server to create web applications.
•	Python can be used alongside software to create workflows.
•	Python can connect to database systems. It can also read and modify files.
•	Python can be used to handle big data and perform complex mathematics.
•	Python can be used for rapid prototyping, or for production-ready software development.
Why Python?
•	Python works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc).
•	Python has a simple syntax similar to the English language.
•	Python has syntax that allows developers to write programs with fewer lines than some other programming languages.
•	Python runs on an interpreter system, meaning that code can be executed as soon as it is written. This means that prototyping can be very quick.
•	Python can be treated in a procedural way, an object-orientated way or a functional way.
Python Syntax compared to other programming languages
•	Python was designed for readability, and has some similarities to the English language with influence from mathematics.
•	Python uses new lines to complete a command, as opposed to other programming languages which often use semicolons or parentheses.
•	Python relies on indentation, using whitespace, to define scope; such as the scope of loops, functions and classes. Other programming languages often use curly-brackets for this purpose.
To know VERSION
python –version

The way to run a python file is like this on the command line:
C:\Users\Your Name>python helloworld.py
1.2 The Python Command Line
Type the following on the Windows, Mac or Linux command line:
C:\Users\Your Name>python
C:\Users\Your Name>python
Python 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:04:45) [MSC v.1900 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> print("Hello, World!")

C:\Users\Your Name>python
Python 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:04:45) [MSC v.1900 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> print("Hello, World!")
Hello, World!

Whenever you are done in the python command line, you can simply type the following to quit the python command line interface:
Exit()
To come out.

1.3 Execute Python Syntax
As we learned in the previous page, Python syntax can be executed by writing directly in the Command Line:
>>> print("Hello, World!")
Hello, World!
Or by creating a python file on the server, using the .py file extension, and running it in the Command Line:
C:\Users\Your Name>python myfile.py


1.4 Python Indentations
Where in other programming languages the indentation in code is for readability only, in Python the indentation is very important.
Python uses indentation to indicate a block of code.
if 5 > 2:
  print("Five is greater than two!")

Python will give you an error if you skip the indentation:
if 5 > 2:
print("Five is greater than two!")
1.5 Comments
Python has commenting capability for the purpose of in-code documentation.
Comments start with a #, and Python will render the rest of the line as a comment: 
#This is a comment.
print("Hello, World!")
1.6 Docstrings
Python also has extended documentation capability, called docstrings.
Docstrings can be one line, or multiline.
Python uses triple quotes at the beginning and end of the docstring:
Docstrings are also comments:
"""This is a 
multiline docstring."""
print("Hello, World!")
1.7 Python Variables
Creating Variables
Unlike other programming languages, Python has no command for declaring a variable.
A variable is created the moment you first assign a value to it.
x = 5
y = "John"
print(x)
print(y)

Variables do not need to be declared with any particular type and can even change type after they have been set.
x = 4 # x is of type int
x = "Sally" # x is now of type str
print(x)
Variable Names
A variable can have a short name (like x and y) or a more descriptive name (age, carname, total_volume). 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)
Remember that variables are case-sensitive
Output Variables
The Python print statement is often used to output variables.
To combine both text and a variable, Python uses the + character:
x = "awesome"
print("Python is " + x)
You can also use the + character to add a variable to another variable:

x = "Python is "
y = "awesome"
z =  x + y
print(z)

For numbers, the + character works as a mathematical operator:
x = 5
y = 10
print(x + y)

If you try to combine a string and a number, Python will give you an error:
x = 5
y = "John"
print(x + y)
1.8 Python Numbers
There are three numeric types in Python:
•	int
•	float
•	complex
Variables of numeric types are created when you assign a value to them:
x = 1    # int
y = 2.8  # float
z = 1j   # complex

To verify the type of any object in Python, use the type() function:
print(type(x))
print(type(y))
print(type(z))
Int
Int, or integer, is a whole number, positive or negative, without decimals, of unlimited length.
Example
Integers:
x = 1
y = 35656222554887711
z = -3255522

print(type(x))
print(type(y))
print(type(z))
Float
Float, or "floating point number" is a number, positive or negative, containing one or more decimals.
Example
Floats:
x = 1.10
y = 1.0
z = -35.59

print(type(x))
print(type(y))
print(type(z))

Float can also be scientific numbers with an "e" to indicate the power of 10.
x = 35e3
y = 12E4
z = -87.7e100

print(type(x))
print(type(y))
print(type(z))
Complex
Complex numbers are written with a "j" as the imaginary part:
Example
Complex:

x = 3+5j
y = 5j
z = -5j

print(type(x))
print(type(y))
print(type(z))

1.9 Type Casting

Specify a Variable Type
There may be times when you want to specify a type on to a variable. This can be done with casting. Python is an object-orientated language, and as such it uses classes to define data types, including its primitive types.
Casting in python is therefore done using constructor functions:
•	int() - constructs an integer number from an integer literal, a float literal (by rounding down to the previous whole number), or a string literal (providing the string represents a whole number)
•	float() - constructs a float number from an integer literal, a float literal or a string literal (providing the string represents a float or an integer)
•	str() - constructs a string from a wide variety of data types, including strings, integer literals and float literals
x = int(1)   # x will be 1
y = int(2.8) # y will be 2
z = int("3") # z will be 3

x = float(1)     # x will be 1.0
y = float(2.8)   # y will be 2.8
z = float("3")   # z will be 3.0
w = float("4.2") # w will be 4.2
x = str("s1") # x will be 's1'
y = str(2)    # y will be '2'
z = str(3.0)  # z will be '3.0'

Comments

Popular posts from this blog

Blog 2 Python 2

WEB DEVELOPMENT COURSE

PYTHON FULL STACK COURSE