PYTHON BASICS
A. PERIYA NAYAKI,
ASSISTANT PROFESSOR,
KAMARAJ COLLEGE OF ENGINEERING
AND TECHNOLOGY,
MADURAI
• Python is a high level, interpreted , interactive and
object-oriented scripting language.
• It is a highly readable language.
• Unlike other programming languages, python provides
an interactive mode similar to that of a calculator.
PYTHON OVERVIEW
• The code written in python is automatically
compiled to byte code and executed.
• Python can be used as a scripting language.
• Features such as nested code blocks, functions,
classes, modules and packages.
• It make use of object-oriented programming
approach.
CONSOLE or COMMAND SHELL
The interactive environment where we are
interacting with the python interpreter is
called the console or command shell.
Practice
Now, try to interact with the interpreter by
entering a simple expression, 5+2, on the
console
Practice
Now, type the following text at the python prompt
Note: If you want to display a message on the console,
you will need to keep your message within quotes.
Print “Hi Friends, Welcome to Python Laboratory”
Or
Print ‘Hi Friends, Welcome to Python Laboratory’
COMMENTS
• It is used by the programmer to explain the piece
of code to others as well as to himself in a simple
language.
• Python uses the hash character (#) for
comments.
PYTHON IDENTIFIERS
• A python identifier is the name given to a
variable, function, class, module or other
object.
• It can include any number of letters, digits or
underscores
• Spaces are not allowed
• It will not accept @, $,% as identifiers
• Python is a case-sensitive language.
• Hello and hello are different identifiers.
VALID NOT VALID
MyName My Name (Space is not allowed)
My_Name 3dig (cannot start with a digit)
Your_Name Your#Name (Only alphabetic character,
Underscore(_) and numeric are allowed)
Declaring a Variable
• A variable holds a value that may change.
• The process of writing the variable name is called Declaring the
variable.
• In Python, variables do not need to be declared explicitly in order
to reserve memory spaces as in other programming languages.
• When we initialize the variable in python, python interpreter
automatically does the declaration process.
Initializing a Variable
• The general format of assignment statement is as follows:
• Equal sign (=) is known as assignment operator.
• An expression is any value, text or arithmetic expression
• Variable is the name of the variable.
• The value of the expression will be stored in the variable
Variable = expression
Standard Data Types
• The data stored in the memory can be of
many types.
• Example:
PERSON
NAME
ADDRESS
AGE >20
Alphabetic
Alphanumeric
Yes or No
(Boolean)
String
• Single quotes or double quotes are used to
represent strings
• A string in python can be a series or a
sequence of alphabets, numerals and special
characters.
• Similar to C, the first character of a string has
an index 0.
• Slice Operator ([] and [:]) –Subset of the string
• Concatenation Operator (+) – Combine two or
more than two strings
• Repetition Operator (*) - Repeat the same
string several times
Practice
>>> string1=“Hi Friends” # Store String Value
>>>string1 # display string value
>>> string1 + “How are You” #use of + operator
>>> string1 *2 #use of * operator
Practice
>>>string1
>>>string1[1] #display 1st index element
>>>string1[0:2] #display 0 to 1st index element
>>>string1=“HelloWorld”
>>>string1[1:8:2]
#display all the alternate characters
between index 1 to 8
LIST
• List is the most used data type in python.
• It can contain the same or different type of
items.
• To declare a list in python->Separate items
using commas and enclose them within
square brackets([])
Tuple
• Similar to a list, tuple is also used to store
sequence of items.
• Like a list, a tuple consists of items separated
by commas.
• Tuples are enclosed within parentheses
Difference between tuple and list
LIST Tuple
Items are enclosed within square
brackets[]
Items are enclosed within parentheses ()
Lists are mutable Tuples are immutable
Dictionary
• It is same as the hash table type.
• The order of elements in a dictionary is
undefined
• Unordered collection of key-value pairs.
• When we have large amount of data, the
dictionary data type is used.
• Keys and values can be of any type in
dictionary
• Items in dictionaries are enclosed in the curly-
braces {}
• Separated by the comma (,)
• A colon is used to separate key from value
• A key inside the square bracket [] is used for
accessing the dictionary items.
SETS
• It is an unordered collection of data known as
set.
• It does not contain any duplicate values or
elements.
• Union, Intersection, Difference and Symmetric
Difference Operations which are performed
on sets
• Union (|): It performs on two sets returns all
the elements from both the sets.
• Intersection (&) : It performs on two sets
returns all the elements which are common in
both the sets
• Difference (-): It performs on two sets set1
and set2 returns the elements which are
present in set1 but not in set2.
• Symmetric Difference (^): It performs on two
sets returns the element which are present in
either set1 or set2 but not in both.
Input() function
• Input() function interprets the input provided
by the user, i.e. if user provides an integer
value as input then the input function will
return its integer value.
• On the other hand, if the user has input a
string, the function will return a string
Practice
>>>name = input(“What is your name?”)
>>>print (“Hello”+name)
>>>age=int(input(“Enter your age?”))
>>>age
>>>hobbies=input(“What are your hobbies?”)
>>>hobbies
Raw_input() function
• It takes the input from the user but it does not
interpret the input and also it returns the
input of the user without doing any changes,
i.e. raw.
• Then, we can change this raw input into any
data type which is needed for our program.