COMPUTER SCIENCE-XII-CH1p1

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 28

COMPUTER SCIENCE

with
PYTHON

Computer Science Department(Krishna Public School)


CHAPTER- 1
Sujeet Tiwari
Mr. Liju K John Review of Python Basics
Mrs Rubeena Mirza
Mrs Disha Dhupar
(PART-I)
Topics
1. Introduction
2. Structure of a python program
3. Variables and data types
4. Keywords
5. Mutable and immutable types
6. Operator and operands
7. Input and output(python’s built-in functions)
8. comments

Computer Science with PYTHON class XII Chaper-1 2


1.Introduction
• Free and open source programming
• Python is an interpreted, high-level, general-purpose programming
language.
• Python is dynamically typed and garbage-collected.
• It supports multiple programming paradigms, including procedural,
object-oriented programming.

Computer Science with PYTHON class XII Chaper-1 3


2.Structure of a python program
• A python program consist of several elements such as
• Statement
• Expressions
• Functions
• Comments
• Blocks

Computer Science with PYTHON class XII Chaper-1 4


2.Structure of a python program-Statements
Definition: the program code which does something, which perform
task known as statement

STATEMENT

Computer Science with PYTHON class XII Chaper-1 5


2.Structure of a python program-Expressions
Definition: Which represents something like number, String, or value

EXPRESSIONS

Computer Science with PYTHON class XII Chaper-1 6


2.Structure of a python program-Comments
Definition: Additional information about any line of code or statement
known as comment
It never participate in execution of program.
#HASH is used to write single line comment whereas ‘’’ ‘’’ (triple single)
or “”” “”” (triple double) quotation marks used as multiline comment

Computer Science with PYTHON class XII Chaper-1 7


2.Structure of a python program-Functions
Definition: Functions are set of instructions defined under a particular
name. to declare a function def keyword is used.
To work with function
1. Declare a function
2. Provide function definition
3. Call a function to use

Output of above program

Computer Science with PYTHON class XII Chaper-1 8


2.Structure of a python program-Block(s)
Definition: a block refers to a group of statement which are part of
another statement or function
Block always indent from root statement

Root statement

Indent content

Here root statement show the main statement of block, under which a block defined. And indent content is part of block
An block can have multiple statement8, expression, comments etc. block always start by : colon sign on root statement

Computer Science with PYTHON class XII Chaper-1 9


Find the answers of following questions
• How many modes you can use to work with python?
• Why python is interpreted?
• Who develop the python
• What are the python programming features
• Write some python library names

Computer Science with PYTHON class XII Chaper-1 10


3. Variables and data types
• Variable: Variable is like a container into computers memory which
allow us to allocate, change or delete our/user defined values with
the help of python program.
Need of variable:
When data/values allocated to memory It
assign a memory address like 0Xab123FF
A=10 Which is difficult to remember as well as On
When we
A 10 Print(A)
declare a It allocated to memory different machines address may differ So to
variable resolve this address problem variable Used.
Now we not need to remember memory
Address just use variable name instead.

Here, A is a variable in program


Computer Science with PYTHON class XII Chaper-1 11
3. Variables and data types
Data type: as variable use to hold data value, data types used to define
the type of data, it may be number like 123 or 123.45 or text like
“hello” etc.
In python we have following datatypes
Data Type

Numbers Sequence Sets Mapping None

Floating
Integer complex String Tuples Lists Dictionary
point

Boolean

Computer Science with PYTHON class XII Chaper-1 12


3. Variables and data types
Sr. DATA TYPE DESCRIPTION EXAMPLE VALUES
1 Integer To store whole numbers (decimal digit without fraction part) and can 1234
and long be positive or negative +258, -589 etc
2 Float Signify the real number, Number with fraction part, it also represented 125.58
by scientific notation with letter e, which signify 10th power 3.8e3 =>3800.0
3 Complex Pair of real and imaginary numbers, in form of a+bj where a and b is 2+3j etc
number the real part of complex number
4 Boolean It used to store the result in terms of TRUE (1) of FALSE (0) 0, 1 etc
5 None This is a special data type which used when we have unidentified value
to handle
6 Sequence An ordered collection of items, indexed by integer (both positive and “hello”
negative). For example String 0-h,1=e,2=l,3=l,4=0
7 Sets An unordered collection of values of any type with no duplicate entry
8 Mapping This data type is unordered and mutable. Like dictionary

Computer Science with PYTHON class XII Chaper-1 13


3. Variables and data types
• Dynamic Typing:
Term refers a silent feature of python, in python a variable can acquire
any data type based on its value.
Program Output
a=10
print(type(a))
a="Computer Science"
print(type(a))
a=10.5
print(type(a))
a=(1,2,3)
print(type(a))

Here you find that according to value variable showing different data type behaviour
Computer Science with PYTHON class XII Chaper-1 14
4.Keywords
• Keywords are the reserved words used by python interpreter to recognize
the structure of a program. These words interpreted by interpreter and
performed task according to their definition

>>> import keyword


>>> print(keyword.kwlist)
['False', 'None', 'True', 'and', 'as', 'assert', 'break', 'class', 'continue', 'def',
'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is',
'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with',
'yield']
Above shows available keyword into python

Computer Science with PYTHON class XII Chaper-1 15


5.MUTABLE and IMMUTABLE types
MUTABLE IMMUTABLE
Variables those value can be changed after they are Variables those value can not be changed after they
created and assigned are created and assigned

As on above example, variable l is a kind of list, first As shown on above example, variable a is a kind of
we initialized the list with values 1,2 and 3 afterword tuple, and trying to change value but we got error,
we try to change value 2 with 10, and it got changed mean it not allow to change
List, Dictionary Int, float, complex, bool, string, tuple

Computer Science with PYTHON class XII Chaper-1 16


6.Operator and operands
Operation: Activity and action or processing known as operation in other
word doing something which is cause of some result or output known as
operation
Ex: following statement
2+3 result 5
Here above statement 2+3 is a kind of operation which results 5
Operator: Statements or expressions which used to perform operation
known as operator
Ex: addition operation, conditional operation etc.
Operands: on which operator perform operation knows as operands
As on above example 2 and 3 are operands and + symbol is a kind of addition
operation. We can use any variable or constant as operand.
Computer Science with PYTHON class XII Chaper-1 17
6.Operator and operands
• There are following operator available in python
• Arithmetic Operator
• Assignment Operator
• Relational or Comparison Operator
• Logical Operator
• Identity Operator
• Bitwise Operator
• Membership Operator

Computer Science with PYTHON class XII Chaper-1 18


6.Operator and operands- Arithmetic Operator
Symbol Operator Description Example
+ Addition Add two numbers 5+6, a+b it will result addition
of given operands
- Subtraction Subtract two number 10-5, x-y
* Multiplication Used to find product of given operands 5*7, p*q
/ Division To find the quotient with fraction part 5/2, it will produce result 2.5
// Floor Division To find the quotient with integer part 5//2,it will produce result 2.0,
** Exponent To raise a number to the power of another number a**b it represent ab or 5**2 it
represents 52 result 25
% Modulus It used to find remainder on division operation 5%2 result 1

Remainder

Computer Science with PYTHON class XII Chaper-1 19


6.Operator and operands- Assignment Operator
Symbol Operator Description Example
+= Add and assign Add numbers and assign to left side variable A=10, B=20
A+=B, mean 10 and 20 be added and assign to
variable A, so new variable A value become 20

-= Subtract and assign Subtract two number and assign to left side A-=B or A-=10
variable

*= Multiply and assign Used to find product of given operands and assign A*=B or A*=10
to left side variable

/= Divide and assign To find the quotient with fraction part and assign A/=B, A/=5
to left side variable

//= Floor Division and To find the quotient with integer part and assign to A//=B, A//=2
assign left side variable

**= Exponent and To raise a number to the power of another number A**=B, A**=2
assign and assign to left side variable

%= Divide and assign It used to find remainder on division operation and A%=B, A%=5
remainder assign to left side variable
= Assignment Just used to assign value to left side variable A=10, mean value 10 assign to A or A=B, value of B assign to
variable a
Computer Science with PYTHON class XII Chaper-1 20
6.Operator and operands- Relational Operator
Symbol Operator Description Example
Relational operator perform compare operands and produces output in terms of TRUE or A=10,B=5
FALSE result
== Equality If both operand equal, it produce True otherwise A==B FLASE
false
!= Inequality If both operand are not equal, it produce True A!=B TRUE
otherwise false
< Less Than Produce TRUE, If left operand is smaller than right A<B FALSE
operand, in case both equal it produce false
<= Less than or Produce True if left operand is smaller or equal A<=B FALSE
Equal to
> Greater Than Produce TRUE, If left operand is Greater than right A>B TRUE
operand, in case both equal it produce false
>= Greater than Produce True if left operand is greater or equal A>=B TRUE
or Equal to Computer Science with PYTHON class XII Chaper-1 21
6.Operator and operands- Logical Operator
Symbol Operator Description Example
operator perform operation on basis of logic, AND OR NOT gate, in programming NON Zero (positive or
negative value)represent True and ZERO represents False
AND If both operand true, it produce True otherwise A=1,B=0 A AND B FLASE
false
A=1,B=1 A AND B TRUE

OR Produce TRUE, If any one operand is true A=1,B=0 A OR B TRUE


A=0,B=0 A OR B FALSE
NOT Produce TRUE, If operand produce false result A=1 NOT(A) False
Produce True if left operand is greater or equal A=0 NOT(A) TRUE

Computer Science with PYTHON class XII Chaper-1 22


7.Input and output(python’s built-in functions)
Input: this is the way by which user can supply his/her data to
application, in python input() is used for the same
• Input(): the input() accepts and return the user’s input as string and
stores it in a variable which assigned with the assignment operator.
• Note: by default it returns string, to take other numerical values int()
or eval() are used.
Syntax: Variable=input(“msg to show”)
a=input(“Enter your name”)
Code on IDLE

Computer Science with PYTHON class XII Chaper-1 23


7.Input and output(python’s built-in functions)
Type Casting: Type casting is a method which allows to convert a kind
of data into another type of data
Here in example,
First variable a shown property of string,
After we try to convert it into integer but
due to fractional part it not able to
convert, than we try to convert it into
float and it changed to float

So, while we are changing type of one


variable to another variable known as
type casting.

Computer Science with PYTHON class XII Chaper-1 24


7.Input and output(python’s built-in functions)
Type Casting: there are two type of type casting method available
Implicit conversion Explicit conversion
Also know as coercion, happens when data type When data type changed by programmers instruction
conversion is done automatically by python without
instructed by programmer
Ex: Ex:
N1=10 P=“10”
N2=10.5 Q=int(P)
Output=N1+N2, here Here, p is string type
N1 is integer and N2 is and by using int()
float so interpreter function it assign
assign Output as float integer value to Q
Default conversion done by interpreter itself Done by programmer wen required

Functions: Int(), float(), str(), chr(),unichr() are used as type casting functions
Computer Science with PYTHON class XII Chaper-1 25
7.Input and output(python’s built-in functions)
Output: function used to display output result on screen, in python
print() is used for the same
Print( value , Sep=‘ ‘ , end=“\n” )

This is the message which we want In case of multiple values, how one will How a line end, by default new
to display separate with other, by default blank space line expression works

Computer Science with PYTHON class XII Chaper-1 26


8.Comments
• Comments are statements in a script that are ignored by the
interpreter and therefore have no effect on the actual output of code.
• Comments make the code more readable and understandable
• A single line comment starts with hash symbol (#)
• #this is single line comment
• A multiline comment enclosed with ‘’’ ‘’’(triple single quoted) or “””
“””(triple double quoted)
• ‘’’ this is multiline comment’’’

Here, the error shows comment have no effect,


We declared variable myVar as comment and
trying to print its value
Computer Science with PYTHON class XII Chaper-1 27
Thankyou
Computer Science Department
(Krishna Public School, Koni, Bilaspur)
Sujeet Tiwari
Mr. Liju K John
Mrs Rubeena Mirza
Mrs Disha Dhupar

Computer Science with PYTHON class XII Chaper-1 28

You might also like