Short notes on Python

Float format for print
  • {value:width.precesion f}
  • result =0.28715647
eg. print('result {r:10:3f}').format(r = result)
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}

  • 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, bool
    Mutable 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
  • 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:
OperatorDescriptionExample
+Concatenate two or more stringsa+b gives "HelloWorld"
*Repetition - Creates new strings, concatenating multiple copies of the same stringa*2 gives "HelloHello"
[ : ]Range Slice - Gives the characters from the given rangea[1:4]will give "ell"
inMembership - Returns true if a character exists in the given stringH in a will give 1
not inMembership - Returns true if a character does not exist in the given stringM not in awill 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].
OperationDescriptionExample
len(a)length3
a + bconcatenation[1,2,3,4,5,6]
[a]*2Repitition[1,2,3,1,2,3]
3 in aMembershipTrue
max(a)Returns item from the list with max value3
min(a)Returns item from the list with min value1
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 appears0
a.reverse()Reverses objects of list in place[3,2,1]
a.pop()Removes and returns last object from list3
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.
ExpressionDescriptionExample
len(a)Length of tuple2
a + bConcatenation(1,2,3,4)
a*4Repitition(1,2,1,2,1,2)
2 in aMembershipTrue
max(a)Maximum value in tuple2
min(a)Minimum value in tuple1
a[0]Indexing1
a[:2]Slicing(1,2)

You cannot delete an element from tuple(as they are immutable) but you can delete the entire tuple.


Operators

OperatorDescriptionExample
+(Addition)Adds values on either side of the operator2 + 4
- (Subtraction)Subtracts right hand operand from left hand operand4 - 2
* (Multiplication)Multiplies values on either side of the operator2 * 4
/ (Division)Divides left hand operand by right hand operand4 / 2 = 2.0
% (Modulus)Divides left hand operand by right hand operand and returns remainder4 % 2 = 0
** (Exponent)Exponential (power) calculation on operators4^2
// (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 = 49.0//2.0 = 4.0-11//3 = -4,-11.0//3 = -4.0

OperatorDescriptionExample
==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.

OperatorDescriptionExample
inEvaluates to true if it finds a variable in the specified sequence and false otherwisex in y, here in results in a 1 if x is a member of sequence y
not inEvaluates to true if it does not finds a variable in the specified sequence and false otherwisex 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)

bitwise
and,or,not are logical operators which gives results in either true or false

Common Numeric Operations

SymbolFunctionExample
+Addition2 + 3
-Subtraction2 - 3
*Multiplication2*3
//Integer division (returns only quotient)3//2
/Division3/2
**Exponentiation2^3
abs(x)Absolute value|x|
exp(x)e raised to the power xe^x
log(x)Logarithm with base elog_e x
log10(x)Logarithm with base 10log_{10} x
pow(x,y)x raised to the power y





x^y

Comments

Popular posts from this blog

Basic Electronics

DATA STRUCTURES IN C.

RTOS(Real Time Operating System) notes