Session 01
Python Basics
SINET EDUCATION
SINET EDUCATION [Link]
Python is a high-level, interpreted, interactive and object-oriented
scripting language. Python was designed to be highly readable which
uses English keywords frequently where as other languages use
punctuation and it has fewer syntactical constructions than other
languages.
SINET EDUCATION [Link]
Python is Interpreted: This means that it is processed at runtime by the
interpreter and you do not need to compile your program before
executing it. This is similar to PERL and PHP.
Python is Interactive: This means that you can actually sit at a Python
prompt and interact with the interpreter directly to write your programs
Python is Object-Oriented: This means that Python supports Object-
Oriented style or technique of programming that encapsulates code
within objects
SINET EDUCATION [Link]
Python is Beginner's Language: Python is a great language for the
beginner programmers and supports the development of a wide range
of applications from simple text processing to WWW browsers to
games.
Python is a popular programming language. It was created in 1991 by
Guido van Rossum.
SINET EDUCATION [Link]
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.
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.
SINET EDUCATION [Link]
Python feature highlights
include:
Easy-to-learn: Python has relatively few keywords, simple structure, and
a clearly defined syntax. This allows the student to pick up the language
in a relatively short period of time.
Easy-to-maintain: Python's success is that its source code is fairly easy-
to-maintain.
A broad standard library: One of Python's greatest strengths is the bulk
of the library is very portable and cross-platform compatible on UNIX,
Windows and Macintosh.
SINET EDUCATION [Link]
Interactive Mode: Support for an interactive mode in which you can
enter results from a terminal right to the language, allowing interactive
testing and debugging of snippets of code.
Portable: Python can run on a wide variety of hardware platforms and
has the same interface on all platforms.
Extendable: You can add low-level modules to the Python interpreter.
These modules enable programmers to add to or customize their tools
to be more efficient.
Databases: Python provides interfaces to all major commercial
databases.
SINET EDUCATION [Link]
GUI Programming: Python supports GUI applications that can be
created and ported to many system calls, libraries and windows
systems, such as Windows MFC, Macintosh and the X Window system of
Unix.
Scalable: Python provides a better structure and support for large
programs than shell scripting.
SINET EDUCATION [Link]
Tokens
The smallest individual unit in a program is known as a token. There are
five types of tokens :
Keywords
Identifiers
Literals
Punctuators
Operator
SINET EDUCATION [Link]
KEYWORDS
Keywords are the words that convey a special meaning to the language
compiler. These are reserved for special purposes and must not be used
as normal identifier names
SINET EDUCATION [Link]
IDENTIFIERS
Identifiers are the fundamental building blocks of a program and are
used as the general technology for the names given to different parts of
the program such as variables, object, functions, classes, arrays etc.
•Identifiers can have alphabets, digits, underscore.
•They must not be a keyword or Boolean literal or null literal .
•They can be of any length.
•It is case sensitive.
•They must not begin with digits .
SINET EDUCATION [Link]
LITERALS (CONSTANTS)
Literals are data items that are fixed data values. There are six types of
literals:
Integer Literals ·
◦ Decimal
◦ Octal
◦ Hexadecimal
Floating Literals
Boolean Literals
Character Literals
String Literals
The Null Literals
SINET EDUCATION [Link]
print()
print() is a standard output function used to display output/result
Syntax: print(arg1,arg2,…)
Eg:
print(“Welcome !")
print(“Good” + “Morning”)
print(5 + 6)
print (“How” , “ are” , “ you?” )
print ("Happy" , " New Year" , 2022 )
SINET EDUCATION [Link]
#This is a comment, it begins with a # sign
To comment out multiple lines of code using three single quotes in a row to
delimit the comments.
‘’’
This
is
a
multiline
comment.
‘’’
SINET EDUCATION [Link]
Data Types
Variables can store data of different types, and different types can do
different things.
Python has the following data types built-in by default, in these
categories
Text Type : string
Numeric Types : int, float, complex
Sequence Types : list, tuple, range
Boolean Type : bool
Binary Types : bytes,bytearray, memoryview
SINET EDUCATION [Link]
Variable
Variables is a named memory location ,it can hold a value,vary during the
program execution.
Eg;
Name
Roll_no
Roll_no1
No1=1
SINET EDUCATION [Link]
Name = “Fathima”
Institute=“SINET”
Place=“Perinthalmanna”
Age=25
print(Name,Institute,Place,Age)
SINET EDUCATION [Link]
Input()
Input() is a standard input function used to read values from user . It is
return a string value.
Synatx:
str = input(argument)
Eg:
a=input(“Enter your name:”)
print(type(a))
b=input(“Enter a no:”)
print(type(b))
SINET EDUCATION [Link]
A = input("Enter a number")
B = 2022
C = 200.50
print("Data Type of A is: ")
print(type(A))
print("Data Type of B is: ")
print(type(B))
print("Data Type of C is: ")
print(type(C))
SINET EDUCATION [Link]
age = input("How old are you")
# now receiving input from the key board
print ("You are " + age + " years Old")
"""
Tripple Quoted String... Comments
Python is easy
and powerful
"""
SINET EDUCATION [Link]
age=17
height=5.5
name="Danish"
print("Your Name is : "+name)
print("Your Age : " + str(age))
print("Your Height is : " +str(height))
SINET EDUCATION [Link]
Addition of two numbers
name = input("Enter your Name Pls")
a = int(input("Enter a Number"))
b = int(input("Enter second Number"))
c = a+b
print("Hi " + name + " See your results ")
print("The result is “, c)
SINET EDUCATION [Link]
a = input("Enter two numbers")
b = input()
print("You entered:")
print(a)
print(b)
SINET EDUCATION [Link]
Swap two numbers
num1 = input('Enter your First Number..');
num2 = input('Enter your Second Number..');
print("Number in Num1 is" + num1 + " and in Num2 is " + num2)
print("Swapping the values....")
temp = num1
num1 = num2
num2 = temp
print("Now Number in Num1 is " + num1 + " and in Num2 is " + num2)
SINET EDUCATION [Link]
Average of three numbers
num1 = int(input("Enter three numbers Pls.."))
num2 = int(input())
num3 = int(input())
avg= (num1+num2+num3)/3
print("The average is " ,avg)
print("Thank you!")
SINET EDUCATION [Link]