PYTHON - Saikrishna Vadla
PYTHON - Saikrishna Vadla
BASICS
Python Language Introduction
● Python is a widely used general-purpose, high level programming language.
● It was created by Guido van Rossum in 1991 and further developed by the
Python Software Foundation.
● It was designed with an emphasis on code readability, and its syntax allows
programmers to express their concepts in fewer lines of code.
● Python is a programming language that lets you work quickly and integrate
systems more efficiently.
● There are two major Python versions:
1.Python 2 and
2.Python
3. Both are quite different.
¥ Beginning with Python programming:
1)Finding an Interpreter:
● Before we start Python programming, 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) that comes bundled with the Python
software downloaded from http://python.org/.
2) Writing our first program:
#Script begins ----- In Python, comments begin with a #. This statement is
ignored by the interpreter and serves as documentation for our code.
Print(“hello world”) ------To print something on the console, print() function is
used.
#script ends
Output: hello world
¥ Python Language advantages and applications:
● Python is a high level, interpreted and general purpose dynamic programming
language that focuses on code readability.
● It has fewer steps when compared to Java and C.
● It was founded in 1991 by developer Guido Van Rossum.
● It is used in many organizations as it supports multiple programming
paradigms.
● It also performs automatic memory management.
Advantages :
1) Presence of third-party modules
2) Extensive support libraries(NumPy for numerical calculations, Pandas for data
analytics etc)
3) Open source and community development
4) Easy to learn
5) User-friendly data structures
6) High-level language
7) Dynamically typed language(No need to mention data type based on value
assigned, it takes data type)
8) Object-oriented language
9) Portable and Interactive
10) Portable across Operating systems
Applications :
1) GUI based desktop applications(Games, Scientific Applications)
2) Web frameworks and applications
3) Enterprise and Business applications
4) Operating Systems
5) Language Development
6) Prototyping
Python 3 basics:
● Python was developed by Guido van Rossum in the early 1990s and its latest
version is 3.7.1, we can simply call it as Python3.
●Python 3.0 was released in 2008. and is interpreted language i.e it’s not compiled
and the interpreter will check the code line by line.
●This article can used to learn very basics of Python programming language.
●So before moving on further.. let’s do the most popular ‘HelloWorld’ tradition 😛
and hence compare Python’s Syntax with C, C++, Java ( I have taken these 3
because they are most famous and mostly used languages).
Note: Please note that Python for its scope doesn’t depend on the braces ( { } ),
instead it uses indentation for its scope.
Selection:
● Selection in Python is made using the two keywords ‘if’ and ‘elif’ and else
(elseif)
# Python program to illustrate
# selection statement
num1 = 34
if(num1>12):
print("Num1 is good")
elif(num1>35):
print("Num2 is not gooooo....")
else:
print("Num2 is great")
Output:
Num1 is good
Functions:
●You can think of functions like a bunch of code that is intended to do a particular
task in the whole Python script.
● Python used the keyword ‘def’ to define a function.
Syntax:
def function-name(arguments):
#function body
Program:
# Python program to illustrate
# functions
def hello():
print("hello")
print("hello again")
hello()
# calling function
hello()
Output:
hello
hello again
hello
hello again
● Now as we know any program starts from a ‘main’ function…lets create a main
function like in many other programming languages.
Program:
# Python program to illustrate
# function with main
def getInteger():
result = int(input("Enter integer: "))
return result
def Main():
print("Started")
# calling the getInteger function and
# storing its returned value in the output variable
output = getInteger()
print(output)
# now we are required to tell Python
# for 'Main' function existence
if __name__=="__main__":
Main()
Output:
Started
Enter integer: 5
Iteration (Looping):
● As the name suggests it calls repeating things again and again.
● We will use the most popular ‘for’ loop here.
Program:
# Python program to illustrate
# a simple for loop
for step in range(5):
print(step)
Output:
0
1
2
3
4
Modules:
● Python has a very rich module library that has several functions to do many
tasks.
● You can read more about Python’s standard library by Clicking here
● ‘import’ keyword is used to import a particular module into your python code.
● For instance consider the following program.
Program:
# Python program to illustrate
# math module
import math
def Main():
num = -85
# fabs is used to get the absolute
# value of a decimal
num = math.fabs(num)
print(num)
if __name__=="__main__":
Main()
Output:
85.0