Showing posts with label Programming'. Show all posts
Showing posts with label Programming'. Show all posts

Sunday, 19 March 2017

Python programming part-5 Strings

Strings : part-1

If you want to use text in Python , you have to use a string .
a string is created by entering text between two single or double quotation mark.

When the python console displays a string, it generally uses single quotes . The delimiter used for a string doesnot affect how behaves in any way .

>>> "Python is fun!"
'Python is fun!'
>>> 'Always look on the bright side of life'
'Always look on the bright side of life'


Question:
Complete the code to create a string  containing "Hello World" .
>>>"Hello _____"
'Hello World'

____________________________________________________

Strings : part-2

Some characters can't be directly included in a string . For instance, double quotes can't be directly included in a  double quote string ; this would cause it to end prematurely.

Characters like these must be escaped by placing a backslash before them.
Other common characters that must be escaped are newlines and backslashes.
Double quotes only need to be escaped in double quote string , and the same is true for single quote strings.

>>>'Brain\'s mother : He\'s not the Messiah.
He\'s a very naughty boy!'
'Brains's mother : He's not the Messiah . He's a very naughty boy!'

\n represents a new line.

_______________________________________

Newlines 

Python provided an easy way to avoid manually writing "\n" to escape newlines in a string . Create a string with three sets of quotes , and newlines that are created by pressing Enter are automatically escaped for you .

>>>"""Customer: Good morning.
Owner: Good morning , Sir. Welcome to the National Cheese Emporium."""

'Customer: Good morning .\n Owner : Good morning , Sir . Welcome to the National Cheese Emporium.'


Question:
Fill the missing part of the output.

>>>"""First line.
second line"""
'First line ___second line'

____________________________________________________

                                                                                                          

Python Programming part-4 (Exponentiation ,Quotient & Remainder :)

Exponentiation 

Besides addition , subtraction , multiplication , and division , Python also supports exponentation , which is the raising of one number to the power of another . This operation is performed using two asterisks.

>>> 2**5
32
>>> 9**(1/2)
3.0

Question :
Fill in the blank to make this code correct :
>>> (1+__) **2
16

____________________________________________________

Quotient & Remainder :

To determine the quotient and remainder of a division , use thr floor division and modulo operators , respectively .
Floor division is done using two forward slashes .
The modulo operator  is carried out with a percent symbol (%).
These operators can be used with both floats and integers.

This code shows that 6 goes into 20 three times , and the remainder  when 1.25 is divided by 0.5 is 0.25 .

>>> 20 // 6
3
>>> 1.25 // 0.5
0.25

Question :
What is the result of this code ?
>>> 7 % (5//2)
a. 0
b. 1
c. 7

ans . b

____________________________________________________

Python programming part-3 Floats

Floats part-1 :

Floats are used in Python to represent numbers that aren't integers.
Some Example of numbers that are represented as floats are 0.5 and -7.8237591 .
They can be created directly by entering a number with a decimal point , or by using operations such as division on integers . Extra zeros at the number's end are ignored .

>>> 3/4
0.75
>>> 9.8765000
9.8765

Question :
Which of these will not be stored as a float ?
a. 7.0
b. 7
c. 2/4

_______________________________________________

Floats part-2 :

As you saw previously , dividing any two integers produces a float.
A float is also produced by running an operation on two floats , or on a float and an integer.

>>> 8 / 2
4.0
>>> 4+1.65
5.65

Question :
Fill in the blank with output of this code .
>>> 1+3+2+4.0+5

_________________________________________________
                                                                                                  

Python programming part-2 Simple Operations

Simple Operations : Part-1


Python has the capability of carrying  out calculations . Enter a calculation directly into Python console , and it will output the answer .

>>> 2+2
4
>>> 5+4-3
6

Question:
How dose this code output ?
>>> 1+2+3

________________________________________________

Simple Operations : Part-2

Python also carries out multiplication and division , using an asterisk to indicate multiplication and a forward slash to indicate division.

Use parentheses to determine which operations and performed first.

>>> 2 * (3+4)
14
>>> 10 / 2
5.0

Question :
Which option is output by this code ?
>>>(4 + 8) / 2

________________________________________________

Simple Operations : Part-3

The minus sign indicates a negative number . Operations are performed on negative numbers , just as they are on positive ones .

>>> -7
-7
>>> (-7 + 2) * (-4)
20

Question :
Fill in the blank to make this code correct .
>>> (___5 - 1) * 3
-18

__________________________________________________

Simple Operations : Part-4

Dividing by zero in Python produces an error , as no answer can be calculated .

>>> 11 / 0
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ZeroDivisionError: division by zero

__________________________________________________
                                                                                                   

Python programming part-1 The Python Console

What is Python ?



What is Python?

Python is a computer programming language that lets you work more quickly than other programming languages. Experienced programmers in any other language can pick up Python very quickly, and beginners find the clean syntax and indentation structure easy to learn. This tutorial will help you to become a python developer.