Short notes on Python
Float format for print
- {value:width.precesion f}
- result =0.28715647
see link its too good
https://pyformat.info/
use tab button to auto complete the string
Strings are immutable means we can't modify the any index of the string also dictionaries are immutable
while in list are mutable we can change index at any where
mylist=[1,2,3,4,5]
mylist.append(6)
result should be [1,2,3,4,5,6]
don't assign the variable name to already defined function such as in case of list it is mylist.append(), mylist.pop(), mylist.sort()
dictionary formatting
dictionary is same as list but the difference is that in dic. at every index there is key value present as like below
{'key1': 'string/list/value', 'key2': 'string/list/value'}
point is that dictionaries available in curly parenthesis
dictionaries doesn't have any retain order and sequences
tuples are also immutable eg. (1,2,3[1,2])
set provide unique value it doesn't provide the repeated value for list eg. mylist = [1,1,1,1,2,2,2,3,3,3,4,4,4] set(mylist)
output {1,2,3,4}
Operators
Common Numeric Operations
use tab button to auto complete the string
Strings are immutable means we can't modify the any index of the string also dictionaries are immutable
while in list are mutable we can change index at any where
mylist=[1,2,3,4,5]
mylist.append(6)
result should be [1,2,3,4,5,6]
don't assign the variable name to already defined function such as in case of list it is mylist.append(), mylist.pop(), mylist.sort()
dictionary formatting
dictionary is same as list but the difference is that in dic. at every index there is key value present as like below
{'key1': 'string/list/value', 'key2': 'string/list/value'}
point is that dictionaries available in curly parenthesis
dictionaries doesn't have any retain order and sequences
tuples are also immutable eg. (1,2,3[1,2])
set provide unique value it doesn't provide the repeated value for list eg. mylist = [1,1,1,1,2,2,2,3,3,3,4,4,4] set(mylist)
output {1,2,3,4}
- Format conversion method
- print('my os name is %s',%'windows')
- o/p will be my os name is windows
- print('my os name is %r.,%'windows')
- o/p will be my os name is 'windows'
- in above two o/ps the difference is that %r will take the o/p with ' '
- %5.2f it defines 5 is total number of characters and .2f is up to two point decimal value
- Strings are represented as '______'
- lists are represented as [_,_,_]
- dictionaries are represented as mapping{'key1' : 'value','key2' : 'value2'} ......values may be any format character,list,string etc.
- tuples are represented as (_,__,_,__) as like list but in round bracket and other thing is that it is immutable
- the main advantage of tuple is that we can find the index position from value by using .index('value')
- Immutable Object: int, float, long, complex, string, tuple, boolMutable Object: list, dict, set, byte array, user-defined classes
- if memory location of object is changed after modification in value then it is immutable
- if memory location of object is not changed after modification in value then it is mutable
List expression x=[2,3,5]
y= [ i*2 for i in x ].......#list_comphrention
y= (i*2 for i in x).........#generator_expression
y= [ i*2 for i in x ].......#list_comphrention
y= (i*2 for i in x).........#generator_expression
- After every conditional statement must write ':'(colon) at the end
- eg. if number :
- eleif :
- else :
- for (any variable) in mylist :
- while bool_cond:
- enumerated operator in python will give the o/p in form of tuples i.e. index count and value
- for loop is used to iterate the string,list,etc.
- from randint(0,100) it will give any number from range also both numbers are included
- for defined function result at last is not returning so that return keyword is used for returning the value
- my func(*argc) it means it passes argument to funtcion as in tuples format
- myfunc(**kwrgc) it uses dictionary format
- sum([a,b,c]) <---- must be return in this format
- map(func,*iteration)
- output of maps function is written in listmap((func,*iteration))
- filter syntax is same as the map but the difference is that map is applies on every individual and filter is applies on the whole iteration at a time
- lambda is a anonymous function format is input : output
- For scope LEGB rule is applied
The actual slicing syntax is
[Start index (included): Stop index (excluded): Stride]
.Other Common Operations
Let us take the two strings as
a = "Hello"
and b = "World"
. The following table will be helpful in your journey towards mastering strings in Python:Operator | Description | Example |
---|---|---|
+ | Concatenate two or more strings | gives "HelloWorld" |
* | Repetition - Creates new strings, concatenating multiple copies of the same string | gives "HelloHello" |
[ : ] | Range Slice - Gives the characters from the given range | will give "ell" |
in | Membership - Returns true if a character exists in the given string | H in a will give 1 |
not in | Membership - Returns true if a character does not exist in the given string | M not in a will give 1 |
text = 'geeks for geeks'
# Splits at space
print(text.split())
word = 'geeks, for, geeks'
# Splits at ','
print(word.split(', '))
word = 'geeks:for:geeks'
# Splitting at ':'
print(word.split(':'))
word = 'CatBatSatFatOr'
# Splitting at 3
print([word[i:i+3] for i in range(0, len(word), 3)])
Other Common List Operations
There are several operations that can be performed on lists in python. In this section we will learn about a few which you would use frequently going forward in this program. Let us take two lists
a = [1,2,3]
, b = [4,5,6]
.Operation | Description | Example |
---|---|---|
len(a) | length | 3 |
a + b | concatenation | [1,2,3,4,5,6] |
[a]*2 | Repitition | [1,2,3,1,2,3] |
3 in a | Membership | True |
max(a) | Returns item from the list with max value | 3 |
min(a) | Returns item from the list with min value | 1 |
a.append(10) | Adds 10 to the end of the list | [1,2,3,10] |
a.extend(b) | Appends the contents of b to a | [1,2,3,4,5,6] |
a.index(1) | Returns the lowest index in list that 1 appears | 0 |
a.reverse() | Reverses objects of list in place | [3,2,1] |
a.pop() | Removes and returns last object from list | 3 |
a.remove(2) | Removes object 2 from list | [1,3] |
Common Tuple Operations
Let us take two tuples
a = (1,2)
and b = (3,4)
for this purpose.Expression | Description | Example |
---|---|---|
len(a) | Length of tuple | 2 |
a + b | Concatenation | (1,2,3,4) |
a*4 | Repitition | (1,2,1,2,1,2) |
2 in a | Membership | True |
max(a) | Maximum value in tuple | 2 |
min(a) | Minimum value in tuple | 1 |
a[0] | Indexing | 1 |
a[:2] | Slicing | (1,2) |
You cannot delete an element from tuple(as they are immutable) but you can delete the entire tuple.
Operators
Operator | Description | Example |
---|---|---|
+(Addition) | Adds values on either side of the operator | 2 + 4 |
- (Subtraction) | Subtracts right hand operand from left hand operand | 4 - 2 |
* (Multiplication) | Multiplies values on either side of the operator | 2 * 4 |
/ (Division) | Divides left hand operand by right hand operand | 4 / 2 = 2.0 |
% (Modulus) | Divides left hand operand by right hand operand and returns remainder | 4 % 2 = 0 |
** (Exponent) | Exponential (power) calculation on operators | |
// (Floor Division) | Division of operands where result is the quotient and digits after the decimal point are removed. But if one of the operands is negative, the result is floored, i.e., rounded away from zero (towards negative infinity): | 9//2 = 4 , 9.0//2.0 = 4.0 , -11//3 = -4 ,-11.0//3 = -4.0 |
Operator | Description | Example |
---|---|---|
== | If the values of two operands are equal, then the condition becomes true | (2 == 4) is not true |
!= | If values of two operands are not equal, then condition becomes true | (2!= 4) is true |
> | If the value of left operand is greater than the value of right operand, then condition becomes true | (2 > 4) is not true |
< | If the value of left operand is less than the value of right operand, then condition becomes true | (2 < 4) is true |
>= | If the value of left operand is greater than or equal to the value of right operand, then condition becomes true | (2 >= 4) is not true |
<= | If the value of left operand is less than or equal to the value of right operand, then condition becomes true | (2 <= 4) is true |
Membership operators test for membership in a sequence (string, list, tuple, set and dictionary). You will learn about strings, sets, tuple etc in the upcoming chapters.
Operator | Description | Example |
---|---|---|
in | Evaluates to true if it finds a variable in the specified sequence and false otherwise | x in y , here in results in a 1 if x is a member of sequence y |
not in | Evaluates to true if it does not finds a variable in the specified sequence and false otherwise | x not in y , here not in results in a 1 if x is not a member of sequence y |
Bitwise Operators
Bitwise operators act on operands as if they were string of binary digits. It operates bit by bit, hence the name.
For example,
2
is 10
in binary and 7
is 111
.
In the table below: Let
x = 10
(0000 1010
in binary) and y = 4
(0000 0100
in binary)Common Numeric Operations
Symbol | Function | Example |
---|---|---|
+ | Addition | |
- | Subtraction | |
* | Multiplication | |
// | Integer division (returns only quotient) | |
/ | Division | |
** | Exponentiation | |
abs(x) | Absolute value | |
exp(x) | e raised to the power x | |
log(x) | Logarithm with base e | |
log10(x) | Logarithm with base 10 | |
pow(x,y) | x raised to the power y |
Comments
Post a Comment