Anda di halaman 1dari 5

AMITY INTERNATIONAL SCHOOL

VASUNDHARA, SEC-1
GETTING STARTED WITH PYTHON
UNIT 2 – GETTING STARTED WITH PYTHON

Getting Started (Important Aspects)

 Introduction to Python- an interpreted high level language


 Interactive mode and script mode.
 Creating and assigning values to Variables
 Data Types
 Expressions and Statements
 Operators and Operands in Python (Arithmetic, relational and logical operators), operator precedence,
Expressions and statements (Assignment statement)
 Taking input (using raw_input() and input ()) and displaying output (print statement); Putting comments.

Points to Remember :

 Python was developed by Guido van Rossum at the National Research Institute for Mathematics and
Computer Science in the Netherlands.
 Python is a widely used general-purpose, high-level programming language.
 Python is free to use, even for commercial products, because of its OSI-approved open source license.
 Python is strongly object-oriented in the sense that everything is an object including numbers, strings and
functions.
 Python Supports interactive mode in which you can enter results from a terminal right to the language,
allowing interactive testing and debugging of snippets of code.
 In script mode, we type the programs in a file and then use the interpreter to execute the contents of the file.
 PyScripter is a full-featured stand- alone Python IDE with Integrated Interpreter, debugger , Syntax Error
Highlighting and many more features.
 An identifier is a name used to identify a variable, function, class, module, or other object.
 A variable in Python is defined through assignment.
 The basic data types used in Python are-
1.Number
a)Integer Number
b)Long Integer Number
c)Floating Point Number
d)Complex Number
2. None
3. Sequence
a) Strings
b) Tuples
c) List
4. Sets
5. Mappings
a) Dictionary
 A mutable variable is one whose value may change in place, whereas in an immutable variable change of
value will not happen in place.

 A hash sign (#) that is not inside a string literal begins a single line comment.
 Multiline comments start from ###.
 Python provides the following operators – Arithmetic, Relational, Logical, Assignment and other special
operators
 Arithmetic operators are used for various mathematical calculations. They include the following :
Operator Description Example
+ Addition - Adds values on either side of the operator a=10, b=20, a + b will give 30
- Subtraction - Subtracts right hand operand from left hand operand a=10, b=20, a - b will give -10
* Multiplication - Multiplies values on either side of the operator a=10, b=20, a * b will give 200
/ Division - Divides left hand operand by right hand operand a=10, b=20, b / a will give 2
Modulus - Divides left hand operand by right hand operand and returns
% a=10, b=20, b % a will give 0
the remainder
a=10, b=20, a**b will give 10 to the
** Exponent - Performs exponential (power) calculation on operators
power 20
Floor Division - The division of operands where the result is the 9//2 is equal to 4 and 9.0//2.0 is equal to
//
quotient in which the digits after the decimal point are removed. 4.0

 Relational Operator compares the value of any two operands and produce a result TRUE or FALSE
Operator Description Example
Checks if the value of two operands is equal or not, if yes it returns
== a=10, b=20, a == b) is not true.
true.
Checks if the value of two operands is equal or not, if values are not
!= a=10, b=20, (a != b) is true.
equal then condition becomes true.
Checks if the value of two operands is equal or not, if values are not a=10, b=20, (a <> b) is true. This is
<>
equal then condition becomes true. similar to != operator.
Checks if the value of left operand is greater than the value of right
> a=10, b=20, (a > b) is not true.
operand, if yes then condition becomes true.
Checks if the value of left operand is less than the value of right
< a=10, b=20, (a < b) is true.
operand.
Checks if the value of left operand is greater than or equal to the value
>= (a >= b) is not true.
of right operand,
Checks if the value of left operand is less than or equal to the value of
<= (a <= b) is true.
right operand, if yes then condition becomes true.

 Logical operators connect two relational expressions and produce a truth value.
Operator Description Example
And If both the operands are true then the condition becomes true. a=10, b=20, (a and b) is true.
If any of the two operands are non zero then the condition
Or a=10, b=20, (a or b) is true.
becomes true.
Use to reverses the logical state of its operand. If a condition is
Not a=10, b=20, not(a and b) is false.
true then Logical NOT operator will make false.

 Assignment operator and arithmetic assignment operators –


Operator Description Example
Simple assignment operator, Assigns values from right side
= c = a + b will assign value of a + b into c
operands to left side operand
+= Add AND assignment operator c += a is equivalent to c = c + a
-= Subtract AND assignment operator c -= a is equivalent to c = c – a
*= Multiply AND assignment operator c *= a is equivalent to c = c * a
/= Divide AND assignment operator c /= a is equivalent to c = c / a
%= Modulus AND assignment operator c %= a is equivalent to c = c % a
**= Exponent AND assignment operator c **= a is equivalent to c = c ** a
//= Floor Division and assigns a value operand c //= a is equivalent to c = c // a
 Python has two key functions to deal with end-user input, one called raw_input() and one called input().
 Raw_input() works both with number and string data types but it returns the output in string type.
 The input function allows the user to provide a prompt string. When the function is evaluated, the prompt is
shown. The user of the program can enter the name and press return. Input() value returns a string value.
 Python’s print statement, paired with the string format operator ( % ), supports string substitution. For ex:
>>> print "%s is number %d!" % ("Python", 1)
Python is number 1!
%s substitute a string, %d indicates an integer substitution and %f for floating point numbers.
 The print function can print any number of values separated by commas.
1 Mark Questions (Very Short Questions):
1) Write a statement to assign the value of (a*a) + (b*b) to a variable.
2) I want to divide 1 by 3 and get a result like: 0.333333333333. What is the exact command to do this?
3) What will be the output of 15.0/3?
4) What does the modulus operator do?
5) Mention the use of following escape sequence
a. \n
b. \t
6) If a=70 and b=10 then determine the result of the following:
a. a/b
b. a% b
7) What is the purpose of using comments in a program?
8) What is an assignment statement?
9) What will be the output of the following:
a. i=30*15/3
b. i=30/15*3
10) What is the result of the following expression :
p>=q && (p+q) > p
a. P=3,q=0
b. P=5,q=5
11) Predict the output of :
a. print(type("17"))
b. print(type("3.2"))
12) How can you determine the type of a variable in python?
13) Is the following a legal variable name in Python : A_good_grade_is_A+
14) What is printed when the following statements execute?
n = input("Enter your age: ")
# user enters a value of 16
print ( type(n)
15) What will be the output of :
a. print(2 ** 3 ** 2)
b. print((2 ** 3) ** 2)
16) What will be the value of variables a and b in the following code:
a=5
b=a
print(a,b)
a=3
print(a,b)
17) What does the >>> prompt indicates?
18 ) What is the order of the arithmetic operations in the following expression. Evaluate the
expression.
2 + (3 - 1) * 10 / 5 * (2 + 3)
19) Add parenthesis to the expression 6 * 1 - 2 to change its value from 4 to -6.
20) Take the sentence: All work and no play make Jack a dull boy. Store each word in a separate variable, then
print out the sentence on one line using print.
2 Mark Questions (Short Questions):
1) What is a python IDLE?
2) What is a variable? What are the rules to name a variable.
3) What is an expression? Explain with examples.
4) What will be the type of the following result :
i. 3.14 * 7 * 7
ii. Hello * 26
5) If a car travels at a speed of 10 Km/hr, how much distance will it cover in 50 mins. If the driver increases the
speed to 15 Km/hr then how much distance can be covered.
6) What is the difference between = and == operators.
7) For any two given int values, return their sum. If the two values are the
same, then return double their sum.
8) What is an operator? Explain with the help of examples.
9) Write a program in script mode to add two numbers and print their sum and product.
10) Write Statements to calculate the following:
i. Area of circle with radius 12.
ii. Volume of a sphere of radius 7.
iii. Area of room whose length is 40.53 and breadth is 34.56
iv. A book has a price of 425 Rs and shopkeeper is offering a discount of 15%. Calculate its price after
discount.
11) What is a keyword in Python? In what colour is it displayed in code editor window?
12) Write a program to calculate volume and surface of a cylinder whose radius is given by the user.
13) What will be the output of the following statements if the value of variable str=”HelloWorld”
i. Print str iv. Print str[2:]
ii. Print str[0] v. Print str *2
iii. Print str[2:5]
14) What will be the output of the following statements if the value of variable list=[‘abcd’,786,’xyzw’,’892’]
i. print list[] iii. print list[1:3]
ii. print list[0] iv. print list[2:]
15) Can we convert a string data type to integer. If yes, how?
16) What is a statement? Explain by giving examples.
17) What is concatenation, and on what type of variables (integers/floats/strings) does it operate on?
18) Differentiate between the following with suitable examples :
 Variable and constant
 Number and string data type
19) What is printed when the following statements execute?
day = "Thursday"
day = 32.5
day = 19
print(day)
20) What value is printed when the following statement executes?
a. print (18 / 4)
b. print (18 % 4)
c. print (18 // 4)
3 Mark Questions:
1) Differentiate between the following:
i. Mutable and immutable variables
ii. A statement and an expression.
2) Explain the importance of data types.
3) Mention the rules for naming an identifier. Give appropriate examples.
4) Which of the following are invalid variable names and why?
a) MarksSecured e) TOTAL
b) mark in math f) Mr_obt
c) perc% g) 123subj
d) $name h) subj-sc
5) Why are comments placed in a C++ program? What are the various ways in which comments can be added to a
program?
6) Write codes to do the following :
a) Assign values 5,10,15 to variables x,y,z in a single statement.
b) Accept input from the user in three different variables in a single statement.
7) Write a program to convert Celsius temperature into Fahrenheit temperature.
8) Write a program to convert Fahrenheit temperature into Celsius temperature.
9) Write a program to find the larger of two numbers.
10) Evaluate the following expressions:
a) 3*(3*(3*len(“ab”)))
b) (60<110/0) and (15<150)
c) Len(“helloworld”)==100/10
d) Len(“helloworld”)==100/10 and 200/20
11) What will be the output of the following if my_age=29 and my_height=104
a. print “I am %s inches tall” % my_heigth
b. print “ My age is %d” % my_age
c. print “There are %d days in a week” %7
12) Write a program to find roots of a quadratic equation.
13) Write a program to find whether a number entered by the user is positive or negative.
14) Rohit gets a basic pay of Rs.8000. His dearness Allowance is 40% of basic salary and house rent allowance is
20% of the basic salary. Calculate his gross salary.
15) Write a program to calculate simple interest.
16) Many people keep time using a 24 hour clock (11 is 11am and 23 is 11pm, 0 is midnight). If it is currently 13
and you set your alarm to go off in 50 hours, it will be 15 (3pm). Write a Python program to the above problem.
Ask the user for the time now (in hours), and then ask for the number of hours to wait for the alarm. Your
program should output what the time will be on the clock when the alarm goes off.
17) Write a program to calculate area of a triangle using hero’s formula.
18) Write a program to calculate volume and surface area of a cuboid whose length, breadth and height are given.
19) Name the days of the week from 0-6 where day 0 is Sunday and day 6 is Saturday. If you go on a holiday
leaving on day number 3 (a Wednesday) and you return home after 10 nights. Write a program which asks for
the starting day number, and the length of your stay, and it will tell you the number of day of the week you will
return on.
20) Use IDLE to do the following:
i. Calculate total and average of marks scored in five subjects
ii. Find whether a number is even or odd.
iii. Find whether a number is divisible by 3 .
HOTS Questions:
1) The following statement produces no output when not run in the shell. 3.14 * 6 * 6
Modify it to produce output.
2) What's the difference between input() and raw_input() ?
3) The following statement produced an output of 13 and not 20. Explain the reason.
x=7+3*2
4) Why is Python called a strongly object oriented language.
5) Enter the following code on the prompt:
>>> 1 == 1
>>> "1" == "1"
>>> 1 == "1"
Why does the third line returns false, while the first 2 lines are true?

Anda mungkin juga menyukai