Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

Basics of Python

The document provides an overview of Python programming basics, including numerical constants, strings, variables, data types, and various operators. It explains how to perform operations, format strings, and utilize functions like input() and format(). Additionally, it covers comments, assignment operators, and logical, membership, and identity operators.

Uploaded by

ojhasweta064
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Basics of Python

The document provides an overview of Python programming basics, including numerical constants, strings, variables, data types, and various operators. It explains how to perform operations, format strings, and utilize functions like input() and format(). Additionally, it covers comments, assignment operators, and logical, membership, and identity operators.

Uploaded by

ojhasweta064
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 51

Basics of Python

Yashraj Kumar Gupta


Numerical Constants
• Integers
Simple whole numbers. For eg: 5, -1, 6, etc.

• Long Integers
Bigger whole numbers.
For eg: 5694412365366L
A long integer must have ‘l’ or ‘L’ as suffix.
Numerical Constants
• Floating Point Numbers
eg: 5.28 or 25.9E-4
Here, E represents power of 10.
25.9E-4 = 25.9 x 10-4

• Complex Numbers
Numbers of the form a+bi.
For eg: -5+6i
Numerical Constants
Note: Commas are never used in numeric values.
Values like 3,567 are not allowed in Python.
format( ) Function
• It produces a string version of a floating point value rounded to a
specific number of decimal places.

Output Output
format( ) Function
• The format( ) function can also be used for inserting commas in the
numbers.

Output Output
Simple Operations on Numbers
1. + : Addition.
2. - : Subtraction.
3. / : divides and returns quotient in float type.
4. // : divides and returns quotient in desired data type. It is known as
floor division operator.
5. % : divides and returns the remainder. It is known as modulo operator.
6. ** : exponents. (5**2 is equivalent to 52 and will return 25 as answer)
7. Division by zero : Invalid. It will generate an error.
Strings
• Strings are declared in single quotes or double quotes.
Example:

Output Output
Strings
• We can enter a multi-line string using triple quotes.
Example:

Output
Strings
• Python concatenates two strings placed side by side.
Example:

Output

• Note: There is no char datatype in python.


Escape Sequences
• Some characters (like “ ‘ /) cannot be directly included in a string.
Example:

Output
Escape Sequences
• Such characters must be escaped by placing a backslash before them.
Example:

Output
Escape Sequences
Escape Sequence Purpose Syntax Output
\\ Prints backslash print(“\\”) \
\’ Prints single quote print(“\’”) ‘
\” Prints double quote print(“\””) “
\n Prints newline print(“Hello\nWorld”) Hello
World
\t Inserts a tab space Print(“Hello\tWorld”) Hello World
String Formatting
It will right align the string in a
field width of 10 characters.
Output

It will centre align the string in a


field width of 10 characters.
Output
String Formatting
It will left align the string in a
field width of 10 characters.
Output
Variables
• Variables represent memory location where any information or data is
stored.
• Rules for naming a variable:
1. It can only start with a letter or an underscore (No numerical digits or special
characters).
2. The rest of the name can contain letter, underscore and numerical digits.
3. Variable names are case sensitive. (eg: Name, name and NAME: All these three
are different variables.)
4. Punctuation characters (such as @, $, %) are not allowed within the name of
any variable.
Variables
• Examples of valid variables:
sum _my_var num1 a var20 First

• Examples of invalid variables:


1num my-var %check Date of Birth a&b
Data Types
• The type of value a variable can hold is known as data type.
• For example:
A person’s name will be made up of only characters. His age will be stored in
numbers and his address can probably be a mixture of numbers and characters.

• Five standard data types stored in python are:


numbers strings list tuple dictionary

• We can even create our own data types (like classes). It will be discussed
in upcoming chapter.
Assigning or Initializing Values to Variables

We can assign values to multiple


variables in a single line.

Text inside single/double quote will be printed as it is.


The variable whose value is to be printed will be
separated from the quote using comma.

Output
Reassigning Values to Variables

In Python, we can reassign variables


as many times as we want.

Output
input( ) Function
input function is used to take input from
the user. The text inside the double quote
will be displayed as prompt while taking
the input.

Output
input from the user.

input function takes the input as a string.


So, whether we enter a number, it will be
treated as a string.
Comments
• Comments are non-executable
statements in a program.
• They are just added to describe any Output
statement of the program.
• They begin with a hash(#) sign.
Assignment/Shortcut Operators
Operator Representation Meaning
+= a += b a=a+b
-= a -= b a=a–b
*= a *= b a=a*b
/= a /= b a=a/b
%= a %= b a=a%b
//= a //= b a = a // b
**= a **= b a = a**b
Assignment/Shortcut Operators

Output Output

This statement is equivalent to This statement is equivalent to


a=a+b a=a*b
Relational/Comparison Operators
• Suppose a = 100; b=200

Operator Meaning Example Output


== Equals to print(a==b) False
!= Not equal to print(a!=b) True
> Greater than print(a>b) False
< Lesser than print(a<b) True
>= Greater than or print(a>=b) False
equal to
<= Lesser than or print(a<=b) True
equal to
Relational/Comparison Operators

Output
Bitwise Operators
• They perform operations at the bit level.
• Various Bitwise Operators are:
1. Bitwise AND (&)
2. Bitwise OR ( | )
3. Bitwise XOR ( ^ )
4. Bitwise NOT ( ~ )
Bitwise Operators
1. Bitwise AND (&)
Convert the given numbers into Binary of 4 bits and then follow the truth
table given below.
A B A&B
0 0 0
1 0 0
0 1 0
1 1 1
Bitwise Operators
1. Bitwise AND (&)
Example:
print(5&6) will be evaluated by first converting 5 into binary which
will be 0101 and 6 in binary will be 0110.
Now using the truth table of & operator evaluate corresponding digits.
5&6 = 0101 & 0110 = 0100
Now 0100 in binary = 4 in decimal.
Thus, print(5&6) will give output = 4.
Bitwise Operators
1. Bitwise AND (&)

Output
Bitwise Operators
2. Bitwise OR ( | )
Convert the given numbers into Binary of 4 bits and then follow the truth
table given below.
A B A|B
0 0 0
1 0 1
0 1 1
1 1 1
Bitwise Operators
2. Bitwise OR ( | )
Example:
print(5|6) will be evaluated by first converting 5 into binary which
will be 0101 and 6 in binary will be 0110.
Now using the truth table of | operator evaluate corresponding digits.
5|6 = 0101 | 0110 = 0111
Now 0111 in binary = 7 in decimal.
Thus, print(5|6) will give output = 7.
Bitwise Operators
2. Bitwise OR ( | )

Output
Bitwise Operators
3. Bitwise XOR ( ^ )
Convert the given numbers into Binary of 4 bits and then follow the truth
table given below.
A B A^B
0 0 0
1 0 1
0 1 1
1 1 0
Bitwise Operators
3. Bitwise XOR ( ^ )
Example:
print(5^6) will be evaluated by first converting 5 into binary which
will be 0101 and 6 in binary will be 0110.
Now using the truth table of ^ operator evaluate corresponding digits.
5^6 = 0101 ^ 0110 = 0011
Now 0011 in binary = 3 in decimal.
Thus, print(5^6) will give output = 3.
Bitwise Operators
3. Bitwise XOR ( ^ )

Output
Bitwise Operators
4. Bitwise NOT ( ~ )
Convert the given numbers into Binary of 4 bits and then follow the truth
table given below.
A !A
0 1
1 0
Bitwise Operators
4. Bitwise NOT ( ~ )
The NOT bitwise operation inverts bits. A 0 becomes a 1. A 1 becomes a
0.
When numbers are printed in base-10, the result of a NOT operation can
be surprising. In particular, positive numbers can become negative and
negative numbers can become positive.
Bitwise Operators
4. Bitwise NOT ( ~ )
For example:
~ 5 will give -6
At the bit level:
~ 0000 0101 (5) = 1111 1010 (-6)

This is because numbers are (usually) represented using two's complement,


where the leftmost bit is actually negative. So flipping the leftmost bit
usually flips the sign of the number.
Bitwise Operators
4. Bitwise NOT ( ~ )

Output
Shift Operators
• Shift Operators refer to bit shifting.
• Python supports two bitwise shift operator:
1. Shift Left (<<)
2. Shift Right (>>)
Shift Operators
1. Shift Left (<<)
When shifting left, the most-significant bit is lost, and a 0 bit is inserted on
the other end.

A single left shift multiplies a binary number by 2:


For Example:
5<<1 = 0101 << 1 = 1010 = 10
Therefore, 5<<1 = 5x21 = 10;
5<<2 = 5x22= 20; 5<<3 = 5x23 = 40
Shift Operators
1. Shift Left (<<)

Output Output
Shift Operators
2. Shift Right (>>)
When shifting right, the least-significant bit is lost, and a 0 bit is inserted
on the other end.

A single right shift divides a binary number by 2 and leaves the remainder:
For Example:
5>>1 = 0101 >> 1 = 0010 = 2
Therefore, 5>>1 = 5\21 = 2;
5>>2 = 5\22= 1; 5>>3 = 5\23 = 0
Shift Operators
2. Shift Right (>>)

Output Output
Logical Operators
1. Logical AND (&&)
If we have an expression (a>b) && (b>c), then the whole expression
is true only if both the expressions are individually true.
2. Logical OR (||)
If we have an expression (a>b) || (b>c), then the whole expression
is true if any one of the two expressions is individually true.
3. Logical NOT ( ! )
It converts any non-zero number to 0 and 0 to 1.
Membership Operators
1. in Operator:
It returns True if a variable is found in a specific sequence.
Otherwise, it will return False.
For example:

Output Output
Membership Operators
2. not in Operator:
It returns True if a variable is not found in a specific sequence.
Otherwise, it will return False.
For example:

Output Output
Identity Operator
1. is Operator:
It returns True if values on both sides of the operator point to the
same object. Otherwise, it will return False.

Output Output
Identity Operator
2. is not Operator:
It returns True if values on both sides of the operator does not
point to the same object. Otherwise, it will return False.

Output Output
The End

You might also like