Getting Start with Python
Getting Start with Python
Features of Python
• High Level
• Free and Open Source
• Case Sensitive
• Interpreted
• Platform Independent
• Rich library of functions
• Dynamic
• General Purpose
Execution Mode
We can use Python Interpreter in two ways:
• Interactive mode
• Script mode
Interactive Mode
• Instant execution of individual statement
1
• Convenient for testing single line of code
• We cannot save statements for future use
Script Mode
Python Keywords
• These are predefined words which a specific meaning to Python Interpreter.
• These are reserve keywords
• Keywords in python are case sensitive
Identifier
2
Identifiers are name used to identify a variable, function or any other entities in a
programs.
Variables
• It can be referred as an object or element that occupies memory space which can
contain a value.
• Value of variable can be numeric, alphanumeric or combination of both.
• In python assignment statement is used to create variable and assign values to it.
Comments
• These are statement ignored by python interpreter during execution.
• It is used add a remark or note in the source code.
• It starts with # (hash sign) in python.
Operators
These are special symbols used to perform specific operation on values.
Different types of operators supported in python are given below:
3
• Arithmetic operator
• Relational operator
• Assignment operator
• Logical operator
• Membership operator
Arithmetic Operator
Relational Operator
4
Logical Operator
5
Assignment Operator
6
Membership Operator
Operator Precedence
Expressions
• An expression is combination of different variables, operators and constant which is
always evaluated to a value.
• A value or a standalone variable is also considered as an expression.
7
Examples:
200
4.0 + 6.22
Var
Var + 13
41 + 3*11 — 77/7
"SP" + " " + "College"
Evaluation of Expression
Example1: 23 + 12 + 18
Evaluation:
= (23 + 12) + 18 #step 1
= 35 + 18 #step 2
= 53 #step 3
Statement
A statement is unit of code that the python interpreter can execute.
Example:
• var1 = var2 #assignment statement
• x = input ("enter a number") #input statement
• print ("total = ", R) #output statement
8
Syntax: print([message/value])
Type Conversion
• Type conversion refers to converting one type of data to another type.
• Type conversion can happen in two ways:
Explicit conversion
Implicit conversion
Explicit Conversion
• Explicit conversion also refers to type casting.
• In explicit conversion, data type conversion is forced by programmer in program.
Syntax:
(new_data_type) = (expression)
9
Example: Program of explicit type conversion from float to int
x = 12
y=5
print(x/y) #output - 2.4
print(int(x/y)) #output - 2
Implicit conversion
• Implicit conversion is also known as coercion
• In implicit conversion data type conversion is done automatically.
• Implicit conversion allows conversion from smaller data type to wider size data type
without any loss of information
10
Debugging
Process of identifying and removing errors (also called bugs) produced in program is
called debugging.
Errors
Error refers to mistake that occurs while writing a program and due to which program
may not execute or may not generate desired output.
Types of error Errors occurring in programs can be categorized as :
• Syntax error
• Logical error
• Runtime error
Syntax Errors
• Syntax errors refers to writing code against programming rules of python such as
using wrong function name, wrong indentation, wrong expressions etc.
• Program can not be executed until it is syntactically incorrect
Examples:
(7 + 11 #absence of right bracket
X = inpt("enter a number") #wrong function name
Logical Error
Example:
#line1: is logically written wrong as the program will only display 'a' as vowel and rest
will be displayed as consonant, because they will not be compared.
11
It should be written as:
If x=='a' or x=='e' or x=='Ï' or ×=='0 or X=='u':
print("Vowel")
Else:
Print("Consonant")
Runtime Error
Runtime Error causes abrupt termination of program during its execution. It appears
during execution of program only.
#if user enter 0 in y, it generate runtime error and program is terminated abruptly
12