Some Method of Python

 Floor Division Operator

To find integral part of quotient we use Floor Division Operator

//

  • a // b

    Code

print(3 // 2)
PYTHON

Output

1

Compound Assignment Operators

Different compound assignment operators are

+=
,
-=
,
*=
,
/=
,
%=

a += 1
is similar to
a = a + 1

Code

a = 10
a -= 2
print(a)
PYTHON

Output

8

Examples of Compound Assignment Operators

Code

a = 10
a /= 2
print(a)
PYTHON

Output

5.0

Code

a = 10
a %= 2
print(a)
PYTHON

Output

0

Escape Characters

Single And Double Quotes

String is a sequence of characters enclosed within quotes.

Code

sport = 'Cricket'
print(type(sport))
sport = "Cricket"
print(type(sport))
PYTHON

Output

<class 'str'>
<class 'str'>

Code

is_same = ('Cricket' == "Cricket")
print(is_same)
PYTHON

Output

True

Escape Characters

Escape Characters are a sequence of characters in a string that are interpreted differently by the computer. We use escape characters to insert characters that are illegal in a string.

Code

print("Hello\nWorld")
PYTHON

Output

Hello
World

We got a new line by adding

\n
escape character.

Examples - Escape Characters

Escape Characters start with a backslash in Python

  • \n
     -> New Line
  • \t
     -> Tab Space
  • \\
     -> Backslash
  • \'
     -> Single Quote
  • \"
     -> Double Quote

Passing Strings With Quotes

The backslash

\
character here tells Python not to consider the next character as the ending of the string.

Code

print('It\'s Python')
PYTHON

Output

It's Python

Code

print("It's Python")
PYTHON

Output

It's Python

Post a Comment

Please Select Embedded Mode To Show The Comment System.*

Previous Post Next Post

Contact Form