0% found this document useful (0 votes)
43 views17 pages

CH 5 Computer

Computer based notes maded by hands

Uploaded by

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

CH 5 Computer

Computer based notes maded by hands

Uploaded by

ridvanshrathore
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 17
Coding in Python gqudents! yeneed to give Instructions to the coy fe Mo accomplish certain tasks, upaprogram of the most important mputer, to begin the process of 5 A set of predefined instructions in logic ing problems manner makes One pogramis written ina computer language, like BASIC. "+4", Python, etc, A comput: f age consists of codes which are combined in accordance with a predefined syntax. that conveys the instructions to the components (ALU) of CPU, aspects of computer field is to code and make ‘Program lnthis section, we will discuss about how to solve problems by developing simple petpuns with help of he compuserInguage Python! ten h #ython has evolved into a very useful language as it is simple and is being used in many idssuhocin Rebocs Seite Purp (Use by NAGA) Seteh Engines (Coogon YouTube, Hardware Testing (Being used by Intel, Cisco, HP, etc.), GUI Interface ‘Popularly used in GIMP), 3D Animation (Maya), etc. "atures of Python Programming Language: simple English. The keywords of Python are in simple Englis e wi ypes of Operating System hence / Python library is portable and compatible with all ypes of Operating S itis known as platform independent programming languagi ee © Python is ani anguage ie, Python interprets and executes the code, line Python is an interpreted language sr yrhon, easy to debug byline, This also makes program of Python, ‘ a, etc ‘7 Itcan be easily integrated with C, C++, Jav: To work in ‘Interactive Mode’: ® Click on Window" key from the key! ‘Start Screen’ appears on the screen. rote Lis es + Python 3.11 Module Does (6-8) + OLE @ython 3.11640 Search ne et Python se we sits (2) Click on ‘Python’. python download python enn compiler 2 python compiler P prone P python ttoia 2 python waschoote ™ python Docuent) PD pyredan (cea “Python Shell window appears on the scr Fle Est_Shat_eb [Fveton 3.1 tent Window Hep (1) Type the command at the Python prompt. | | Output is (3) | displayed. | ‘board. Python 3.11 (64-bit) unaeadmeitor Open fieaeaton Pntoatber a) Type ‘Python’ on the search bar. een. [eeesme aera) RE vibe ea Ee WENT oe oF Micense()" gor more angoreaeion, Q Press ‘Enter key. tes toto \- Ctrt+F6 key combination is used to restart the Python Shell. Python Shell is the Layout screen where we type Python command. 0 Script Mode Inscript mode, we type the set of codes in more than one line in a file and then use the interpreter to execute the complete program from the file. Working in interactive mode is convenient for beginners and for testing small modules of code, as we can test them immediately for each line while in script mode, we can code aprogram with more number of lines and can also save, edit and reuse the code for future use. Python, in interactive mode, is good enough to learn, experiment or explore, but the only drawback is that we cannot save the statements for further use and we have to retype all the statements to re-run them. Tocreate and run a Python script, we will use the following steps in ‘IDLE, if the script mode is not available by default with IDLE environment. ve Open ‘Python Shell then click on ‘File’ menu. 2 Ese =e Fle tat Set ebup Often Window Hep Click on the ‘New File’ option. Type the command 'print' conmand is used to display Shetl', 1f atl the statements or conmands are written in separgys’tn then it 8 optional to put semécoton {3} at the end of each or conmand however ét £4 compulsory to put semicolon (5) at the each statement or conmand when more than one commands on stat, are written in the single Line. the output on the screen i.e, » Ling tate = | Click on ‘File’ menu and select ‘Save" option to save the program or script, Say 4, dialog box appears on the screen. s New folder a: oO msec . Brv0vet at ' & Ei beitep sf th © beers Fontenot me = he (1) enter Opener | ‘Type the Python rin } file name. fev | Q - . ‘eatin Click on ‘Save’ | Teen Dani SERED Marae eee w Click on ‘Run Module’ option from ‘Run’ menu or press ‘5’ from the keyboard execute the program. 1 set ese py Cine ANOUDeaep rp ode BI) ae Format The output will be displayed in the Python Shell, \- | “py’is the extension of the file saved in Python. || CHARACTER SET: Character set in Python consist of valid characters such as alphabets, digits or special characters that are recognized by the library of Python Language. Alphabets AB, Z Or a,b, een ¥Z 0,1,2,3,4,5,6,7,8,9 Characters “1@#%"* &*()_-+-|\Ol hs *'0,.2/,«,» White Spaces Blank space, Horizontal tab, New line, etc. allest individual unit in a program is referred toas Tokens’. Tokens are also known sical Elements or Lexical Units. It can be categorized as: [Tokens ® Tens) ~— (Lrnaustos Cpe tage Conta Y 09) oer) Far iterated Character Ce Petstiral Frag Cretart Toga ‘aAsgent sting Comma @ Relational Operator Relational operators a logical expressions. It is Rene re those operators which are used to compare arithmetica, - ‘also known as ‘Comparison Operator’. i Seeteein |) ‘Heaning GOeuei Meer) es Oeses os True if left operand is greater Greater than : tear 7 Tue if left operand is less than Less than . ie acd Equal to = True ifboth operands are equal | asp Not equal to ig True if operands are not equal an Tue if left operand is greater Cee eal than or equal to the right arb True if left operand is less than Less than or equal to 7 Dern a @ Logical Operator Logical operators are used when there isa situation for evaluating two or more relation! expressions. In Python, there are three logical operators which is commonly used it logical ‘and’, logical ‘or’ and logical ‘not’. Logical AND Re tareeri snr ryt Cee Xelp Dereeeli9 Logical OR Logical NOT i. Logical ‘and’ operator This operator is written as ‘and’. It combines tw expression evaluates to ‘True’ only if both the con ‘© expressions into one. The result ditions satisfy to ‘True’. Logical ‘and’ Operator Bicimeuisite) False False False True Logical ‘or’ operator erator is written as ‘or’. It combines two ¢xpressions into one, The resulting 1H OP nevaluates to True’ when either of the conditions satisfy to Trae! og Logical ‘or’ Operator Beane |_False | False | False it True True eaten False | Sarena! | True “| True True ii. Logical ‘not’ operator ii his operator is written as ‘not’, It works with single expression or operand, i. it is a unxy operator. This operator Negates or reverses the truth value of the expression. In «her words, if the expression is ‘True’, then the use of ‘not’ expression makes it ‘False’ a and vice versa. Logical ‘not’ Operator (Punctuators fucuitors are used as marks or as separators in a program, The characters used as pmetuators are**#\()[] {J@,:.>+; IIVARTABDES: \atables are the data or vah ana tues which can change during the execution of a program. Itis « given to the location in memory in which the value is stored during the execution 2 program, Rules for defining a Variable: © Avatiablename may bethe combination of alphabets, digitsor underscore, Generally, ‘efitst character in the variable name is an alphabet. Blank space ina variable name. 's cannot be used in a vari . a \iable names are case sensitive, ie. (for example, ‘amount’ is not same as . SMouNT) a 2 Thena ¢ be used as the name of variables tlponthe nee variable can be classified as ‘Numeric’, ‘Sri ly, oo the type of data (Data Type), v a | tle and ‘Dictionary’. ~ Anum "Umber Pe ignated identity that stores ‘ric variable stores a numeric value. It is a designated identity that store variable stores a ‘ ler arte Ts, during the execution of program. For examp! # Documentation Section Declaration Section Commands & Expressions | @ Documentation Section The documentation section consists of a set of comment lines with the name of the program and other details relating to it. We can also write name of the programmer and the date on which the program was created or updated. It is not necessary to include this section in a program. However, using comment lines makes a program look user friendly © For Single Line Comment: Comment line always starts with the symbol #. Example: My First Program © For Multiple lines Comment: Comment should be enclosed between Pair of triple apostrophe “”, Example =: = “My First Program’ @ Declaration Section In declaration section, values can be assigned to variables and literals, However, Python's variables and literals do not need explicit declaration. The declaration occuts automatically when you assign a value to a variable or literal. The equal sign (-) is used to assign values to variables ‘The operand to the left of the - operator is the name of the variable and the operand to! the right of the - operator is the value stored in the variable. Example + marks-100 #Here, ‘marks’ is the name of variable and 100 is the value assigned to it @ Commands It is that portion of the program in which commands execute it. In Python, there are various keyword or statements that id are written to make program ., For helps to execute same task- : example: print statement is used to display the output. aca Print ‘message’ [Or] print variable name Example print Welcome to MICA - Output Welcome to MICA @ Expression An expression represents something which P ci ython evaluates and then produces res" yr the form of value. sample sume a0 Consider the following Illustrations and Explanations: istration I: WAP (Write a Program) in Python to print “Hello World” Ppt el get els (SHE a Te) UUISY ‘ello World Program fLine 1 pint (Hello World #Line 2 po © Line] is Comment as it is written within # symbol. & Line 2 is the ‘Command! section of the program. ‘print’is an in-built Python command which displays the output on the screen, Here, itis used to display ‘Hello World’ on the sereen, Illustration II: WAP in Python to print sum of two numbers ie. 5 and 10. PUTS STE trted ereeyegectat Oielelentnal ep zaetorey +#Sum of two numbers #Line 1 uml= #Line 2 num2-10; #Line 3 sum=num1+num2; #Line 4 print (sum); #Line 5 Ea Line 1 is Comment as it is written within # symbol. Line 2 and Line 3 declare and initialize the value of variable num] and num2, Line 4 is sum-numl+num2, here this mathematical expression has been used to find the sum of variable ‘numI’ and “num2" whose value is stored in the variable ‘sum’, & Line 5 is print (sum), here ‘print’ has been used to display the calculated sum on the screen. 1 {lustration Il: WAP in Python to find area of rectangle whose value of length and breadth ‘Sem and 4 cm respectively. #Line 1 #Line 2 #Line 3 area_rectangle=length*breadth; #Line 4 print (area_rectangle}; #Line 5 | Ea | | & Line lis Comment as it is written within # symbol. & Line 2and Line 3 declare and initialize values of variables length and breadth of Tectangle & Line 4 is area rectangle-length*breadth, here this mathematical expression has beey used to find the product of variables ‘length’ and ‘breadth’ whose value is stored in the variable ‘area rectangle. © Line 5 is print (area_rectangle), here ‘print’ has been used to display the calculated are, of rectangle on the screen, Illustration IV: WAP in Python to find square of a number ewe aseee #Square of Number #Line 1 num=6; #Line 2 square=num*num; #Line 3 print (square); #Line4 © Line lis Comment ast is written within i symbol © Line 2 declare and initialize the value of variable ‘num’. Line 3 is square-num*num, here this mathematical expression has been used tofind tit cquare of number whose vali stored inthe vasiable sommee, & Line 4 is used to display the square of number ‘6 on the screen. Illustration V: WAP in Python to get quotient and remainder when 43 is divided 6 Serer en unseat #Division of a Number me dividend-43; Ae divisor6; es quotient=dividend /divisor, ned remainder~dividend’divisor, ame print (quotient,remainder); tine 6 © Line 1 is Comment as itis written with in # symb © Line 2 and Line 3 declare and initialize the value ate dividend and divisor: lividend an ia Line 4 is quotient-dividend/divisor, here this mathematical expression has been used to find the quotient whose value is stored in the variable ‘quotient’. Line 5 is remainder-dividend9divisor, here this mathematical expression has been used tofind the remainder whose value is stored in the variable ‘remainder” Line 6 is used to display the value of quotient and remainder on the screen. v WAP to print the following message on the Computer screen. “You cannot believe in God until you believe in Yourself” vw WAP to calculate area of triangle whose base is 12 cm and height is 6 cm. vw WAP to find cube of a number ‘4’. v@ WAP to calculate the Simple Interest where, Principal-"1000, Rate-10% and Time-4 Years. .® WAP to find perimeter of rectangle whose value of length and breadth is 5 cm and 4 cm respectively. vw WAP to find sum and difference of 20 and 5. This statement is used to accept (input) the value to the variable given by the user. Syntax: variable name - data_type(input (‘message to be displayed”)); Example: num + int(input (“Enter value of num-")); Output Enter value of num= the variable num from the user.) (The above statement accepts the value for 5‘ ‘ avo numbers and display their sum. Illustration VI: Write a program to accept USSEy Moar en mane 7 #Line 1 "um]=int(input(“Enter value of first number= #ine2 Pum2=int{input(“Enter value of second number=")i Ne Sum = num] + num2; #Line 4 Print ‘cum of hd and Se is Yod"9lrumt num? sur * Notes: -s4d is used to display integer values-] ; ; Cr, 2 Line tis used to input first integer pune ae Ine 2 is used to input second integer ™ B Line 3is used to add two number and store it in variable sum. B Line 4 ised to print message on the shell CoLapi oj Enter value of first number=25 Enter value of second number=30 Sum of 25 and 30 is 55 Mlustration VIE: WAP in Python to find the area of a rectangle when the value of length p and breadth ‘b’is given by the user, SERENE anTS en FFint(input(“Enter the length=")); #Line 1 b=int{input(“Enter the breadth="); #Line 2 Area =1* b; #Line3 print(“Area of rectangle is: 94d”%(Area)); #Line 4 Line 1 is used to input the length of rectangle, & Line 2 is used to input the breadth of rectangle. & Line 3 is used to calculate area of rectangle, & Line 4 is used to print the message on the shell. OUTPUT Enter the length=80 Enter the breadth=20 Area of rectangle is 1600 Illustration VIII: WAP in Python to. ‘get quotient and remainder on dividing two numbers Consider the second number to be lesser than the first 7 Giro oma oe aee nen num1=int(input(“Enter first number=")), #Line 1 num2=int(input("Enter second number=")}; pay quo = num1/num2; ae rem = num] %num: ee print("The Quotient is: %d"%(quo)); aune5 print(The Remainder is: %d"%(rem)); ane v g tine Lisused to input the first number. Line 2 is used to input the second number, Line 3is used for calculating the quotient, g Lined isused for calculating the remainder g Line Sis used to display quotient, 1 Line 6is used to display remainder. ilostration IX: WAP in Python to cony syecksand remaining days, Therelevant Program| Codelin/Python: sdyrinlGnput("Enter total number of days"); ert given number of total days into no. of years, #Line 1 sears = tday/365; #Line 2 rem = tday'6365; weeks = rem/7; #Line 3 det = rem’%7; #Line 4 pint(Years is %od"%6(years)}; #Line 5 inl Wecks is %d"%(weeks); #Line 6 Frint"Days is %d"94(dleft); #Line7 Ra 3 Line lis used to input total no. of days. 5 Line? Line 2 is used to calculate number of years. ® Line is used to calculate number of weeks 8 Line 4is used ® Line Sis used to display number of years. Line 6is used to display number of weeks. * Line is used to display number of days left. to calculate number of days left. According to the creator of Python, Mr Guido Van Rossum, the name of this language ‘was derived from the British comedy series “Monty Python's Flying Circus” which ‘was aired on BBC during the 1970s as the creator got high enjoyment with the series when he was into the development of Python language . i » square of any number. Mlustration X: WAP in Python to find the square of any meen vaste! num =inifinpul(*Enter the number") square= num * num; prini('square of %al is %d"%4(num square) & Line lis © Line 2 is used to © Line 3 is used to dis ised to input the number. Iculate square of the number. lay the square of the number. ‘w WAP to input two numbers and calculate their sum and difference @ WAP to convert hours into minutes and seconds. ve WAP to input diameter of circle to calculate area and perimeter ofa circle. %@ WAP to input base and height of a triangle and calculate its area. 1 WAP to calculate the product of 4 numbers. WAP to calculate the area and the perimeter of the rectangle. w® WAP to find the cube of any number. vw WAP to input Principal, Rate and Time to calculate simple interest. Ling *Lineg #Line 4 QUC0 00 0 1, Fillin the blanks: (2) ____ datatype is used to store numeric values, 0) are the kind of key value pairs. (c)_______operator is used to evaluate relational expressions. (d) The smallest individual unit in a program is called ()___ statements used to accept and assign value to the variable given by the user. 2, Select the most suitable alternative: (2) Which of the following is a Python tuple? @ [12,3] ii) (1,2,3) Gil) {1,2,3} (vy) (3 (b) What is the output of the expression, 30%6? i) 0 Gi) 5 (ii) 1 (iv) 8 (¢) Which of the following is a data type? (i) List (ii) Tuple (iii) dictionary (iv) All of these (@) Which of the following is a logical operator? (i) and (i) or (iii) not (iv) All of these (©) Which mode of Python allows writing code in multiple lines? (i) Programming Mode (ii) Script mode (iii) Execution mode (iv) All of these 1. Fillin the blanks: (a)_____ datatype is used to store numeric values, (b)_ __________ are the kind of key value pairs. ()_____operator is used to evaluate relational expressions, (@) The smallest individual unit ina program is called (e) _______statement is used to accept and assign value to the variable given by the user. . Select the most suitable alternative: (a) Which of the following is a Python tuple? @ [12,3] Gi) (1,2,3) (ii) {1,2,3} (iv) (3 (b) What is the output of the expression, 30%6? ) 0 (i) 5 (iii) 1 (iv) 8 () Which of the following is a data type? @) List (ii) Tuple (iii) dictionary (iv) All of these (@ Which of the following is a logical operator? (i) and (ii) or (iii) not (iv) Allof these ©) Which mode of Python allows writing code in multiple lines? (i) Programming Mode (ii) Script mode (iii) Execution mode (iv) All ofthese [Setieterteee eee 3. Write short notes on: (a) Keyword (b) Datatype (o) List (d) Tuple (c) Literals (f) Character Set 4. Answer the following questions: (a) List few applications of Python. (b) Explain the featur f Python. (c) Explain about Python character set (G) What is the drawback of Interactive mode in Python? (e) Explain the guidelines for creating identifiers in Python. (8 How to insert single line and multi line comments in Python? (g) What is data type? List different data types used in Python. 5. Identify the errors in the following program code: (a) =Program to print sum of three numbers whose values are 10, 12 and 15 num1=10 sum=num1+num2=num3: print “sum” (b) #Program to input and display the name of your school input “Your School Nam Print NAME (c) #Program to find the square of the number 5 num: ° cube =Num print “sum” Lab Project n° Activity 5 Code and develop a Program in Python to accept t i eit a ir 7 ad fem| enhel! convert into Centigrade. ept temperature in Fah D Inter Disciplinary Subject: Physics ,

You might also like