Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
4 views

Getting Start with Python

The document provides an introduction to Python, highlighting its features, programming terminologies, and how to work with Python using the interpreter in interactive and script modes. It covers essential concepts such as identifiers, variables, comments, operators, expressions, statements, input/output functions, type conversion, debugging, and types of errors. Additionally, it includes examples to illustrate these concepts and their applications in Python programming.

Uploaded by

Kiran Raval
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Getting Start with Python

The document provides an introduction to Python, highlighting its features, programming terminologies, and how to work with Python using the interpreter in interactive and script modes. It covers essential concepts such as identifiers, variables, comments, operators, expressions, statements, input/output functions, type conversion, debugging, and types of errors. Additionally, it includes examples to illustrate these concepts and their applications in Python programming.

Uploaded by

Kiran Raval
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

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

Important Programming Terminologies


Instructions: A single step written to carry out an action
Programs: Set of Instructions
Software: Set of Programs
Source Code: A program written in High Level Language
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.

Working with Python


To write and run python programs we need Python Interpreter also called Python
IDLE.

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

• Allows us to write and execute more than one Instruction together.


• We can save programs (python script) for future use
• Python scripts are saved as file with extension ".py"

How to execute or run python program


• Open python IDLE
• Click 'File' and select 'New' to open Script mode
• Type source code and save it
• Click on 'Run' menu and select 'Run Module'

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.

Rules for naming Identifier


• 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.
• it should not be a python keyword or reserved word.
• We cannot use special symbols like !, @, #, $, % etc. in identifiers.

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

Example 2: 56 + (23-13) + 89%8 - 2*3


Evaluation: = 56 + (23 -13) + 89%8 - 2*3
= 56 + 10 + (89%8) - 2*3 #step 1
= 56 + 10 + 1 - (2*3) #step 2
= (56 + 10) + 1 - 6 #step 3
= (66 + 1) - 6 #step 4
= 67 - 6 #step 5
= 61 #step 6

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

How to input values in python?


In python we have input function for taking user input.
Syntax: Input([prompt])

How to display output in python


In python we have print function to display output.

8
Syntax: print([message/value])

Example: python program to input and output your name

var = input("Enter your name")


print("Name you have entered is", var)

Example: Addition of two numbers


Var1 = int(input("enter no1"))
Var2 = int(input("enter no2"))
Total = Var1 + Var2
print("Total = ", Total)

Example: Program to convert degree Celsius into


Fahrenheit
C = float ("Enter degree in Celsius"))
F = (9*C) /5 + 32
print ("Degree in Fahrenheit = ", F)

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

Example: program of explicit type conversion from string to int


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

Example: program of explicit type conversion from string to float


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 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

Example: Program to show implicit conversion from int to float


var1 = 10 #var1 is integer
var2 = 3.4 #var2 is float
res = var1 - var2 #res becomes float automatically after subtraction
print(res) #output - 6.6
print(type(res)) #output - class 'Float'

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

Logical errors refers to


• Incorrect execution of program
• Not producing desired output

Example:

If x=='a' or 'e' or'i' or 'o' or 'u': #line 1: Logical Error


print("Vowel")
Else:
Print("Consonant")

#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.

Example: "Division by zero"


x = int(input("Enter Dividend"))
y = int(input("Enter Divisor"))
print(x/y)

#if user enter 0 in y, it generate runtime error and program is terminated abruptly

12

You might also like