KEMBAR78
Sumita Arora Class Xi Computer Science With Python PDF | PDF | Data Type | Python (Programming Language)
0% found this document useful (0 votes)
179 views6 pages

Sumita Arora Class Xi Computer Science With Python PDF

The document provides an overview of Python programming, detailing its features, shortcomings, and various data types including numbers, strings, lists, tuples, and dictionaries. It also covers operators, type conversion, error types, and exception handling in Python. Additionally, it explains the differences between errors and exceptions, and lists common built-in exceptions.

Uploaded by

alwina9310
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
179 views6 pages

Sumita Arora Class Xi Computer Science With Python PDF

The document provides an overview of Python programming, detailing its features, shortcomings, and various data types including numbers, strings, lists, tuples, and dictionaries. It also covers operators, type conversion, error types, and exception handling in Python. Additionally, it explains the differences between errors and exceptions, and lists common built-in exceptions.

Uploaded by

alwina9310
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

MAHARISHI VIDYA MANDIR MOGAPPAIR

Grade XI: Computer Science


Python Fundamentals

Python (a computer language) :


• Python is a powerful and high level language and it is an interpreted language. • It is
widely used general purpose, high level programming language developed by Guido van
Rossum in 1991. It is used for: software development, web development (server-side),
• Python has two basic modes: interactive and script.In interactive mode
(python.exe/py.exe), the result is returned immediately after pressing the enter key. In
script mode (IDLE), a file must be created and saved before executing the code to get
results.

Features of Python

• Easy to use – Due to simple syntax rule


• Interpreted language – Code execution & interpretation line by line • Cross-platform
language – It can run on windows,linux,macinetosh etc. equally • Expressive language
– Less code to be written as it itself express the purpose of the code.
• Completeness – Support wide rage of library
• Free & Open Source – Can be downloaded freely and source code can be modify for
improvement

Shortcomings of Python
1. Lesser libraries – as compared to other programming languages like
c++,java,.net 2. Slow language – as it is interpreted languages,it executes the
program slowly.

Tokens: The smallest individual unit in a program is known as a token. There are five types
of tokens allowed in Python. They are :

• Keywords (Reseserved words) example: for, del, elif, else etc.

• Identifiers : Variable names like balance, class names like Vehicle etc •

Literals : String, Numeric, Boolean, None etc

• Punctuators : Symbols like ‘#’, ‘(‘, ‘[‘, ‘=’ etc.

• Operators : Unary, Binary, Bitwise like ‘+’, ‘&’, ‘^’ etc

Data Types In Python


1. Number 2. String 3. Boolean 4. List 5. Tuple 6. Dictionary
Number In Python: It is used to store numeric values. Python has three numeric types: 1.
Integers: Integers or int are positive or negative numbers with no decimal point. Integers in
Python are of unlimited size. int() function converts any data type to integer.
e.g. a = "101" # string. b=int(a) # converts string data type to integer. c=int(122.4)
converts float data type to integer.
2. Floating point numbers: It is a positive or negative real numbers with a decimal point.
e.g. a = 101.2 b = -101.4
float() function converts any data type to floating point number.
e.g: a='301.4' #string
b=float(a) #converts string data type to floating point number.
3. Complex numbers: Complex numbers are combination of a real and imaginary
part.Complex numbers are in the form of X+Yj, where X is a real part and Y is imaginary
part. Eg: a = complex(5) # convert 5 to a real part
val and zero imaginary part. b=complex(101,23) #convert 101 with real
part and 23 as imaginary part.

String In Python: A string is a sequence of characters. In python we can create string


using single (' ') or double quotes (" ").Both are same in python. e.g. str='computer science'
print('str-', str)
Output str- computer science
print('str[1:3]-', str[1:3]) print string from postion 1 to 3
Output : str[1:3]- om
print('str[3:]-', str[3:]) # print string staring from 3rd char
Output- str[3:]- puter science
print('str *2-', str *2 ) # print string two times
Output- str *2-computer sciencecomputer science

Boolean In Python: It is used to store two possible values either true or false.
e.g. str="comp sc"
boo=str.isupper() - test if string contains upper case
print(boo)
Output: False
List In Python: List are collections of items and each item has its own index value. Tuple
In Python: List and tuple, both are same except ,a list is mutable python objects and tuple
is immutable Python objects. Immutable Python objects mean you cannot modify the
contents of a tuple once it is assigned.
e.g. of list : e.g. of tuple
list =[6,9] tup=(66,99)
list[0]=55 Tup[0]=3 # error message
print(list[0]) will be displayed
print(list[1]) print(tup[0])
OUTPUT print(tup[1]
55 OUTPUT
9 66

99

Dictionary In Python: It is an unordered collection of items and each item consist of a


key and a value.
e.g.
dict = {'Subject': 'comp sc', 'class': '11'}
print(dict)
print ("Subject : ", dict['Subject'])
print ("class : ", dict.get('class'))
Output
{'Subject': 'comp sc', 'class': '11'}
Subject : comp sc
class : 11

Operator: Operators are special symbols in Python that carry out arithmetic or logical
computation. The value that the operator operates on is called the operand.

Arithmetic Operator: used for mathematical operations

Comparison operators(Relational operator) -used to compare values

Logical operators: used to carry out logical operations


Bitwise operator :Used to manipulate bit values

Membership Operators: Test for membership in a sequence

Operators Precedence :highest precedence to lowest precedence table

Type conversion:The process of converting the value of one data type (integer, string, float,
etc.) to another data type is called type conversion. Python has two types of type
conversion. • Implicit Type Conversion
• Explicit Type Conversion
Implicit Type Conversion: In Implicit type conversion, Python automatically converts one
data type to another data type. This process doesn't need any user involvement. Explicit
Type Conversion: In Explicit Type Conversion, users convert the data type of an object to
required data type. We use the predefined functions like int(),float(),str() etc.

Errors and its Types:

What do you understand by Syntax errors and Semantics errors?


Ans: Syntax Errors: syntax error occur when rules of a programming language are
misused i.e. grammatical rule of Python is violated. e.g. X<-x*y if x=(x*y) etc.

Semantics error occur when statements are not meaningful. e.g. x * y = z this will result in
a semantical eoor as an expression cannot come on the left side of an assignment operator.

Why are logical errors harder to locate?


Ans: In spite of logical errors presence, program executes without any problems but the
output produced is not correct. Therefore, each and every statement of the program needs
to be scanned and interpreted. Thus the logical errors are harder to locate.

What is an Exception?
Exception in general refers to some contradictory or unusual situation which can be
encountered unexpectedly while executing the program. Unhandled exceptions will
cause Python to halt execution.

Why is Exception Handling is required?

Ans: Unhandled exceptions will cause Python to halt execution. The exception handling
is ideal for processing exceptional situations in a controlled way so that program ends
gracefully rather than abrupt crashing of the program.

What is the need for debugger tool?


Ans: Debugger tools are very useful especially if the code is big or the error is not very
clear, it becomes very difficult to manually figure out the origin and cause of the problem.
Debugger tools here prove very handy and useful. They show us the line by line
execution and its result on variables interactively and help a programmer get to the root
of the problem.

What are main error types? Which types are most dangerous and why?

Ans: Main error types are - (i)Compile-time errors (ii) Run-time errors (iii) Logical errors
Logical errors are most dangerous errors because these are most difficult to fix. The error
is caused by a mistake in the program‘s logic. You won‘t get an error message, because no
syntax or runtime error has occurred. You will have to find the problem on your own by
reviewing all the relevant parts of your code – although some tools can flag suspicious
code which looks like it could cause unexpected behaviour.

What is the difference between an error and exception?


Exception and Error: Exceptions are those which can be handled at the run time whereas
errors cannot be handled. An exception is an Object of a type deriving from the System.
Exception class. System Exception is thrown by the CLR (Common Language Runtime)
when errors occur that are nonfatal and recoverable by user.

Name some common built-in exceptions in Python.

Ans: Some Common built-in Exceptions in Python are: (i) EOFError (vi) TypeError (ii)
IOError (iii) NameError (iv) IndexError (vii) ValueError (viii) ZeroDivisionError Q.9 when
does these exception occur? (a) Type Error (b) Index Error (c) Name Error (v) ImportError
(ix) KeyError Ans: (a) Type Error: Raised when an operation or function is applied to an
object of inappropriate type. e.g. if you try to compute square-root of a string. (b) Index
Error: Raised when a sequence subscript or index is out of range e.g. from a string of
length 4 if you try to read a value of index 4 or more. (c) Name Error: Raised when an
identifier name is not found.

You might also like