Python Simplified by Imran
Python Simplified by Imran
It can be used :-
web development (server-side),
software development,
handle big data and complex mathematics
system scripting.
connect to database systems
Why Python
Python works on different platforms
Simple syntax Example: print("Hello, World!")
Similar to the English language.
Python runs on an interpreter system (executes very fast )
It allows Object-oriented Programming
It can be written using Thonny, Pycharm, Netbeans or
Eclipse
How to install Python?
1. print("Hello, World!")
2. if 5 > 2:
print("Five is greater than two!")
if 5 > 6:
print("Five is less than six!")
Variable in Python
x=5
y = "Hello, World!"
print(x)
print(y)
Python Variable
How to declare Variable name
Start with a letter or the underscore character
Not 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)
Variable: Multiple ways
Camel Case
myVariableName = "Linguistics”
Pascal Case
MyVariableName = " Linguistics”
Snake Case
my_variable_name = “Linguistics"
Assigning multi values to multi Variables
print() function is often used to print the values stored in the variables
x = "Python is awesome"
print(x)
In the print() function, multiple variables are separated by a comma.
+ operator can also be used to print the multiple variables
x = "Python"
y = "is"
z = "awesome"
print(x, y, z)
Global Variable and Local Variable
Global
x = "awesome"
Syntax
Variable
VariableName = “Statement1” def myfunc():
Local x = "fantastic"
def FunctionName():
VariableName = “Statement2” Variable print("Python is " + x)
print(“Statements” + VariableName) myfunc()
FunctionName()
x=5
y = "Hello, World!"
print(x) #This is a comment. This will print the value of
x
print(y)
Comments in Python
Lets see this code
#print("Hello, World!")
print("Cheers, Mate!")
You can get the data type of a variable with the type() function.
x=5
y = “Mohan"
print(type(x))
print(type(y))
One type to another type
x=1
y = 2.8
z = 1j
print(a)
print(b)
print(c)
Assigning String to a Variable
Or a=„Linguistics‟
print(a)
Assign multiline String to a Variable
Use three double quotes for multi line statements
Example
Syntax Example
list_name = [“str1", “str2", “str3"] list1 =
["apple", "banana", "cherry"]
print(list_name) list2 = [1, 5, 7, 9, 3]
list3 = [True, False, False]
print(list2)
List items are indexed and you can access them by referring to the
index number:
Indexing starts with zero (0). So, the first item‟s index is 0, the
second item‟s index is 1 and so on.
Example
print(mylist[1])
print(mylist[2:5])
A Program to check the type of the list
To append elements from another list to the current list, use the extend() method.
To add an item to the end of the list, use the append() method:
Example
Insert "watermelon" as the third item:
list1 = [“ABC", “DEF", “JKL"]
list1.insert(2, “GHI")
print(list1)
Arithmetic operators are used with numeric values to
perform common mathematical operations:
Operator Name Example
+ Addition x+y
- Subtraction x-y
* Multiplication x*y
/ Division x/y
% Modulus x%y
** Exponentiation x ** y
// Floor division x // y
Some program using operators
i = 256*256
print('The value of i is', i)
J= 100+101
Print(„the value of J is‟, J)
Output:
When you run a condition in an if statement, Python
returns True or False:
a = 200
print(10 > 9) b = 33
print(10 == 9) if b > a:
print("b is greater than a")
print(10 < 9) else:
print("b is not greater than a")
Python - 3
Python Conditions and If statements
Python supports the usual logical conditions from mathematics:
Equals: a == b
Not Equals: a != b
Less than: a<b
Less than / equal to: a <= b
Greater than: a>b
Greater than/equal to: a >= b
for x in range(100):
print(x)
for y in range(1,101)
print(y)
# Iterating over a set
print("\n Set Iteration")
set1 = {1, 2, 3, 4, 5, 6}
for i in set1:
print(i),
Displaying table in Python using for
# Multiplication table (from 1 to 10) in Python
num = 12
num = int(input("Display multiplication table of? "))
x=0
while (x < 3):
x=x+1
print("Hello friend")
Printing number series in Python
Example
# Fibonacci series
a, b = 0, 1
while a < 10
print(a) ... a, b = b, a+b
break()
It control out of the loop. It is used with both the while and the for
loops, especially with nested loops to quit the loop. It terminates
the inner loop and control shifts to the statement in the outer loop.
age = “\n Please enter your age: ”
while True:
age = input
if age >= 18:
break
else:
print (“You‟re not eligible to vote”)
continue()
It is used to continue running the program even after the
program encounters a break during execution.
def my_function():
print("Hello this is my function")
my_function()
if next_calculation == "no":
# break the while loop if answer is no
break
else:
print("Invalid Input")
Python Library for Natural language processing
NumPy TensorFLOW
Metaplotbit SciPY
Pandas spiCy / NLTK
Seaborn OpenCV
Scikitlearn Keras
PyTorch
Presented and instructed by
Imran Ali
Research Scholar, Department of Linguistics
Banaras Hindu University, Varanasi (India)