Hello there, programmer! I’m sure you’re aware of what Competitive Programming is. However, there are a few things to keep in mind when coding in Python. These minor changes can make a significant difference in your code.
Python Competitive Programming:
1. Using Generators in Python
Python includes a generator that allows you to write your own iterator function. A generator is a type of function that returns an iterator object with a sequence of values rather than a single value. A yield statement, rather than a return statement, is used in a generator function.
For Example:
def Python_generators(): yield 15 yield 20 yield 30 for itr in Python_generators(): print(itr)
It is also useful for returning multiple values sequentially at the same time.
2. Using built-in Functions in Python
Python includes a number of functions that are readily available for use. Using built-in functions and libraries is preferable to the traditional method.
Some of the built-in Function Examples are:
1.len(): The length of an object is returned by this method.
gvn_lst = [10, 20, 30, 40, 50] lst_len = len(gvn_lst) print("The length of the given list = ", lst_len)
Output:
The length of the given list = 5
2. min(): Returns the minimum element in the given list.
gvn_lst = [90, 20, 30, 40, 50] minmum_ele = min(gvn_lst) print("The minimum element in the given list = ", minmum_ele)
Output:
The minimum element in the given list = 20
3. max(): Returns the maximum element in the given list.
gvn_lst = [90, 20, 30, 40, 50] maxmum_ele = max(gvn_lst) print("The maximum element in the given list = ", maxmum_ele)
Output:
The maximum element in the given list = 90
4. lower(): Converts the given string into lowercase.
gvn_str = "HELLO BTECHgeeks" lwr_str = gvn_str.lower() print("The given string after converting into lowercase = ", lwr_str)
Output:
The given string after converting into lowercase = hello btechgeeks
5. upper(): Converts the given string into uppercase.
gvn_str = "welcome all" upr_str = gvn_str.upper() print("The given string after converting into uppercase = ", upr_str)
Output:
The given string after converting into uppercase = WELCOME ALL
Similarly, we have many built-in functions which make code easier.
3.itertools in Python:
The itertools module can be extremely useful in solving some complex problems.
itertools.permutations() method: This method returns all the possible permutations of a given list.
for example:
import itertools gvn_lst = ['a', 'b', 'c'] rslt = itertools.permutations(gvn_lst) rslt_lst = list(rslt) print("The all permutations of the given list are :") print(rslt_lst)
Output:
The all permutations of the given list are : [('a', 'b', 'c'), ('a', 'c', 'b'), ('b', 'a', 'c'), ('b', 'c', 'a'), ('c', 'a', 'b'), ('c', 'b', 'a')]
The same thing can be done by writing your own logic and functions, but it will be far more complex and time-consuming.
4.map() Function in Python:
When we need to take input from all the elements of an integer array in a single line separated by white spaces, the map function is our best.
Using the map function reduces the complexity of dealing with multiple values entered on a single line.
For example:
# Give the list as user input using list(),map(),input(),and split() functions. # Store it in a variable. givn_lst = list(map(int, input( 'Enter some random List Elements separated by spaces = ').split())) print("The given list = ",givn_lst)
Output:
Enter some random List Elements separated by spaces = 20 46 72 35 The given list = [20, 46, 72, 35]
5. Python String Concatenation:
In Python, To concatenate multiple strings, we have two options:
- adding strings to strings using the ‘+’ operator.
- using the join function.
adding strings to strings using the ‘+’ operator:
gvn_str1 = "hello" gvn_str2 = "btechgeeks" rslt_str = gvn_str1+gvn_str2 print("The concatenated string is :", rslt_str)
Output:
The concatenated string is : hellobtechgeeks
using the join function:
gvn_lst = ["hello", "this", "is", "Python-Programs"] rslt_str = ''.join(gvn_lst) print("The Concatenated string is : ", rslt_str)
Output:
The Concatenated string is : hellothisisPython-Programs