Posts Tagged with 'Python'
Python join all items of a list to convert into a single string
Python | Join all items of a list to convert into a single string >>> x = ["Python", "Online", "Training"] >>> print(" ".join(x)) Python Online Training
Reversing a String in Python
Python | Reversing a String >>> x = 'PythonWorld' >>> print(x[: : -1]) dlroWnohtyP
Python In-place swapping of two numbers
Python | In-place swapping of two numbers >>> a, b = 10, 20 >>> print(a, b) 10 20 >>> a, b = b, a >>> print(a, b) 20 10
List the common security issues that can be avoided by using Django?
A few common security issues that can be avoided by using Django are: Clickjacking Cross-site scripting and SQL injection
What is the difference between Python array and Python list?
Difference between Python array and Python list Array Lists it is used to store only homogeneous data. The list is used to store heterogeneous data it occupies less amount of memory as the array stores …
Program to count the number of capital letters in a file?
Python program to count the number of capital letters in a file. with open(FILE_NAME) as my_file: count = 0 text = my_file.read() for character in text: if character.isupper(): count += 1 print(count)
What does [::-1] do?
[::-1] is used to inverse the order of a sequence. >>> my_list = [1,2,3,5,6,7,8,9] >>> my_list[::-1] [9, 8, 7, 6, 5, 3, 2, 1]
What is the purpose of ‘is’, ‘not’ and ‘in’ operators?
Python | ‘is’, ‘not’, and ‘in’ operators is: Returns True if both variables are the same object >>> x = 5 >>> y = 5 >>> z = 1 >>> x is y True >>> x …
What is the difference between .py and .pyc files?
The .py files are the python source code of a program. While the .pyc files contain the bytecode of the python files. We get bytecode after compilation of .py file (source code). The .pyc files are not …
Write a code to match a string that has the letter ‘a’ followed by 4 to 6 'b’s.
We can use the python re module to match the string import re def check(txt): # regex pattern pattern = 'ab{4,8}' if re.search(pattern, txt): print('Match found') else: print('Match not found') check("abcbbbb") # Match not found check("aabbbbbc") # …
Write a code to add the values of same keys in two different dictionaries and return a new dictionary.
We can use the Counter method from the collections module from collections import Counter dict1 = {'a': 5, 'b': 3, 'c': 2} dict2 = {'a': 2, 'b': 4, 'c': 3} new_dict = Counter(dict1) + Counter(dict2) …
Program to check all numbers are unique in a sequence.
You can do this by converting the list to a set using the set() method and comparing the length of this set with the length of the original list. If found equal, return True else …
What are decorators?
Python | What are decorators A decorator in Python is a function that modifies the behavior of an existing function or class without any modification to the original source code. A decorator takes another function as its argument and returns yet …
What are unit tests in Python?
Python | What are unit tests Unit test is a testing technique to check a single component of code and ensures that it performs as expected. Unit tests are an important part of regression testing to ensure that …
“in Python, Functions Are First-class Objects.” What Do You Infer from this?
It means that a function can be treated just like an object. You can assign them to variables and pass them as arguments to other functions. You can even return them from another function. You …