Skip to main content

Featured

Learning RSI - (Relative Strength Index) the unconventional way.

What the heck is RSI  The relative strength index (RSI) is a momentum indicator   used in technical analysis that measures the magnitude of recent price changes. RSI is an  oscillatory indicator means it oscillates between 0-100  How to apply. GO TO INDICATORS SEARCH RSI APPLY

Python List Comprehension & any,all

PYTHON


Python is a vast languages there is always somthing that you left behind if you are a beginner Let's Explore Some of that






1. List Compression

List compression is used as one liners in python they are sometimes very useful While we doesn't want to write a loop to fill list

For example : if we want to create a list of square of odd numbers from pre-existing list of numbers 1 to 10 

We consider doing this in two ways :

  • For loop

l1=[1,2,3,4,5,6,7,8,9,10] result=[] for item in l1: if item%2 != 0: result.append(item**2) print(result)

  • List Compression


l=[x**2 for x in range(10) if x%2!=0] print(l)


Result For Both :


[1,9,25,49,81] 


Let's see how it works

l=[result  loop  condition]


• The first one is result that you want 
• The second one is loop it's on you how you use it
• The third one is if else condition that you want to follow (optional)

Note : This also works with dictionaries ,tuples



Let's move on to second one

2. any , all keywords

any ,all keyword return a boolean they are generally used when we need to check multiple condition

For example : if we have a list of string and we need to check whether all the words are present in given string

We generally use two ways :

  • Check condition one by one


con=["hey","are","you"] phrase="hey what are you doing!" if con[0] in phrase and con[1] in phrase and con[2] in phrase: print(("All Words Match"))


  • Using "all"

con=["hey","are","you"] phrase="hey what are you doing!" if all(x in phrase for x in con): print("All Words Match")


Result for Both:


All Words Match 


We can also use "any" in similar way if we want to check if any of words of list con present in phrase


Comments

Popular Posts