Getting Started with Python
Getting Started with Python
Python is a General-Purpose high-level Programming language used for developing application, softwares.
Features of Python
• High Level
• Case Sensitive
• Interpreted
• Platform Independent
• Dynamic
• General Purpose
Machine Code: set of instructions in form of binary language (0 and 1) which is understood by computer
Compiler: program which converts code written in high level language into machine language
Interpreter: it is also a program which converts code written in high level language into machine language. An
interpreter processes the instructions one by one.
To write and run python programs we need Python Interpreter also called Python IDLE.
Execution Mode
• Interactive mode
• Script mode
Interactive Mode
Script Mode
Python Keywords
Identifier
Identifiers are name used to identify a variable, function or any other entities in a programs.
• The name should begin with an alphabet or and underscore sign and can be followed by any combination of
characters a-z, A-Z, 0-9 or underscore.
• It can be of any length but we should keep it simple, short and meaningful.
Variables
• It can be referred as an object or element that occupies memory space which can contain a value.
• In python assignment statement is used to create variable and assign values to it.
Data types
These are keywords which determine the type of data stored in a variable. Following table show data types used in
python:
Comments
Operators
These are special symbols used to perform specific operation on values. Different types of operators supported in
python are given below:
• Arithmetic operator
• Relational operator
• Assignment operator
• Logical operator
• Identity operator
• Membership operator
Arithmetic Operator
Relational Operator
Assignment Operator
Logical Operator
Identity Operator
Membership Operator
Operator Precedence
Expressions
• An expression is combination of different variables, operators and constant which is always evaluated to a
value.
Examples:
• 200
• 4.0 + 6.22
• Var
• Var + 13
• 41 + 3*11 – 77/7
• “HITCOM” + “YouTube”
Evaluation of Expression
Example1:
23 + 12 + 18
Evaluation:
Example 2:
Evaluation:
Statement
Example:
Syntax:
Input([prompt])
Syntax :
print([message/value])
Example :
Type Conversion
• Explicit conversion
• Implicit conversion
Explicit Conversion
Syntax:
(new_data_type) = (expression)
x = 12
y=5
print(x/y) #output – 2.4
print(int(x/y)) #output – 2
x = input(“enter a number”)
print(x+2) #output – produce error “can only concatenate str to str
x = int(input(Enter a number”))
print(x+2) #output – will display addition of value of x and 2
x = ’10.2’
y = ’12.3’
r = x+y
print(r) #output – 10.212.3
r = float(x) + float(y)
print(r) #output – 22.5
Implicit conversion
• Implicit conversion allows conversion from smaller data type to wider size data type without any loss of
information
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
• Syntax error
• Logical error
• Runtime error
Syntax Errors
• Syntax errors refer to writing code against programming rules of python such as using wrong function name,
wrong indentation, wrong expressions etc.
Examples:
• If x>y:
print(x) #wrong indentation
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. It should be written as:
Runtime Error
Runtime Error causes abrupt termination of program during its execution. It appears during execution of program
only.
x = int(input(“Enter Dividend”))
y = int(input(“Enter Divisor”))
print(x/y) #if user enter 0 in y, it generates runtime error and program is terminated abruptly