Unit 1.2

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

2SAEKNT : Introduction to Python P R M I T & R ,B A D N E R A

Subject
Introduction to Python
Unit 1.2

Prof. M. V. Tiwari

(B. E. in Electronics & Telecommunication Engineering, H.V.P.M., C.O.E.T., Amravati, 2009.)


(M. Tech. in Electronic System & Communication, An Autonomous Institute of GCOE, Amravati,
2013.)
(Pursuing Ph.D. from P.G. Department of Applied Electronics, S.G.B.A.U. under the guidance of
Dr. S. V. Dudul, Head PGDAE.)

DEPARTMENT OF ELECTRONICS & TELECOMMUNICATION ENGINEERING PROF. M. V. Tiwari


2SAEKNT : Introduction to Python P R M I T & R ,B A D N E R A

Module 2 :- Python Fundamental’s

•Python Comments
Comments are any text to the right of # symbol and is mainly useful as notes for the reader of the
program.
Comments explain:
1. Assumptions
2. Important Decisions
3. Important Details
4. Problems you are typing to solve

DEPARTMENT OF ELECTRONICS & TELECOMMUNICATION ENGINEERING PROF. M. V. Tiwari


2SAEKNT : Introduction to Python P R M I T & R ,B A D N E R A

Python Keywords ( 35 in number)


Python uses keywords to recognize the structure of the program and hence they cannot
be used for any other purpose.

DEPARTMENT OF ELECTRONICS & TELECOMMUNICATION ENGINEERING PROF. M. V. Tiwari


2SAEKNT : Introduction to Python P R M I T & R ,B A D N E R A

•Identifier
Identifier is a name given to programming elements like variables, functions, strings, etc.
Identifier Rules:
1. Name can contain any combination of letters, Underscore ( _ ) and digits.
2. Name cannot begin with digit.
3. Names are case sensitive.
4. Keywords cannot be used as Indentifies.

DEPARTMENT OF ELECTRONICS & TELECOMMUNICATION ENGINEERING PROF. M. V. Tiwari


2SAEKNT : Introduction to Python P R M I T & R ,B A D N E R A

•Data Types

Note 1 : Python does not have char data type.


Note 2 : Python does not support any constant declaration (every thing is variable)

DEPARTMENT OF ELECTRONICS & TELECOMMUNICATION ENGINEERING PROF. M. V. Tiwari


2SAEKNT : Introduction to Python P R M I T & R ,B A D N E R A

Q 1) You develop a python application for your company. You want to add notes to your
code so other members will understand it. What should you do ?

A) Place the notes after the # sign on any line.


B) Place the notes after the last line of code separated by blank line.
C) Place the notes before the first line of code separated by a blank line.
D) Place the notes inside of parentheses on any line.

DEPARTMENT OF ELECTRONICS & TELECOMMUNICATION ENGINEERING PROF. M. V. Tiwari


2SAEKNT : Introduction to Python P R M I T & R ,B A D N E R A

Q 2) You create a function to calculate the power of a number by using Python.


You need to ensure that the function is documented with comments. You create the
following code. Line numbers are included for reference.

1. # the calc_power function calculates exponents


2. # x is the base
3. # y is the component
4. # the value of x raised to the power y is returned
5. defcalc_power(x,y):
6. comment = “# return the value”
7. return x ** y # raise x to the y power

For each of the following statements, select Yes if the statement is true else no.

Option 1 : Lines 1 to 4 will be ignored for syntax checking : Yes / No


Option 2 : The pound sign (#) is optional for lines 2 and 3: Yes / No
Option 3 : The string in line 6 will be interpreted as a comment: Yes / No

DEPARTMENT OF ELECTRONICS & TELECOMMUNICATION ENGINEERING PROF. M. V. Tiwari


2SAEKNT : Introduction to Python P R M I T & R ,B A D N E R A

Q 3) You create a function to calculate the power of a number by using Python Practice Test. You
need to ensure that the function is documented with comments. You create the following code. Line
numbers are included for reference only.

1 # The calc_power function calculates exponents


2 # x is the base
3 # y is the exponent
4 # The value of x raised to the y power is returned
5 defcalc_power(x,y):
6 comment = “# Return the value”
7 return x ** y # raise x to the y power

For each of the following statements, select Yes if the statement is true, otherwise select false.

Options :
Lines 01 through 04 will be ignored for syntax checking Yes / No
The pound sign (#) is optional for line 2 and 3 Yes / No
The string in line 6 will be interpreted as a comment Yes / No
Line 7 contains on inline comment. Yes / No
DEPARTMENT OF ELECTRONICS & TELECOMMUNICATION ENGINEERING PROF. M. V. Tiwari
2SAEKNT : Introduction to Python P R M I T & R ,B A D N E R A

Q 4) Woodgrove Bank is migrating their legacy bank transaction code to Python


Practice Test. You have been hired to document the migrated code. Which
documentation syntax is correct ?

Options :

A) Returns the current balance of the bank account


defge_balance():
return balance
B) defget_balance():
/* Returns the current balance of the bank account */
return balance
C) // Returns the current balance of the bank account
defget_balance():
return balance
D) defget_balance():
# Returns the current balance of the bank account
return balance
DEPARTMENT OF ELECTRONICS & TELECOMMUNICATION ENGINEERING PROF. M. V. Tiwari
2SAEKNT : Introduction to Python P R M I T & R ,B A D N E R A

Q 5) All keywords in Python are in


A)Lower case B) Upper Case
C) Capitalized D) None of these

Q6) Which of the following only contains keywords ?


A)try & catch B) and, or & in
C) while & do D) if, elif, elseif & else

Q7) Which of the following does not contain keywords ?


A)True & False B) try, except & finally
C) is, not & or D) break, continue & exit

Q8) Which of the following is invalid identifier rules ? Select all that apply.
A)Name can contain $ B) Name can begin with digit
C) Keyword cannot be used as identifier D) Names are case sensitive
DEPARTMENT OF ELECTRONICS & TELECOMMUNICATION ENGINEERING PROF. M. V. Tiwari
2SAEKNT : Introduction to Python P R M I T & R ,B A D N E R A

Q 9 ) Which of the following cannot be a variable ?


A) this B) in
C) it D) on

Q 10) Which of the following is an invalid variable ?


A) my_string_1 B) 1st_string
C) repeat D) _

Q 11) What is the result of the following :


A) type(+1E10)
1) int 2) float 3) str 4) bool
B) type(5.0)
1) int 2) float 3) str 4) bool
C) type(“True”)
1) int 2) float 3) str 4) bool
D) type(False)
1) int 2) float 3) str 4) bool
DEPARTMENT OF ELECTRONICS & TELECOMMUNICATION ENGINEERING PROF. M. V. Tiwari
2SAEKNT : Introduction to Python P R M I T & R ,B A D N E R A

Q12)You are writing a Python Program. The program collects customer


data and stores it in a database. You need to ensure that the program
handles the data correctly so it can be stored in the database correctly.

A = 2;
Minor = False;
Name = “Contosa”;
Weight = 123.5;
Zip = “81000”
What are the data types of the above declaration ?

A)int_bool, char[], float, char[]


B) int, bool, str, double and char
C) Int, boolean, string, float and int
D)Int, bool, str, float and str
DEPARTMENT OF ELECTRONICS & TELECOMMUNICATION ENGINEERING PROF. M. V. Tiwari

You might also like