Python is an interpreted, high-level and general-purpose programming language. Comparing to other programming languages python is so easy to learn. Python is a highly productive language. Python first appeared in 1991. So it has a mature developer community. Python can be used on almost all platforms. It can be used for web development, mobile apps development, game development, big data, machine learning, cloud computing, and so many other cases.
Here are some python coding tips and tricks for beginners.
1. Assigning multiple variables at once
Generally, in python, we assign variable like this
a = 25
b = 36
But we can assign multiple variables in one line like this
a, b, c = 21, 34, 54
Print statement can take unlimited arguments separated by comma like this
print(a, b, c)
2. Large number
You can easily split the large number with the underscore and it doesn't affect the value, and easy to read. Like
large_number = 99_99_99_999
3. Assign multi-line string in a variable
In python, if you declare a variable with a multi-line value it will take the first line. But if you want to declare a multi-line value in a variable you can do it with a backslash like this
str_value = "This is multi" \
"line value"
Alternatively, you can do it with triple quotes. it will print the output multiple lines as you declared Like
str_value = '''This is multi
line value'''
If you want to print it out in one line you have to use a backslash like this
str_value = '''This is multi \
line value'''
4. Separate list item by the character in the same line
You can loop through your list item and show it in the same line separate by any given separator like this
mylist = ["c++", "python", "java", "kotlin"]
for l in mylist:
print(l, end="\t\t")
But if you print something after the loop it will show the next to the loop items. like
for l in mylist:
print(l, end="\t\t")
print('new test')
to clear it you have to put an empty print statement after the loop like this
for l in mylist:
print(l, end="\t\t")
print("")
print('new test')
5. Ternary Operators
It is easy to write a simple if statement in one line by ternary operators. like this, if statement
fish_price = 50
money = 100
if(money > fish_price):
print("You can buy")
else:
print("You can not buy")
to this
print( "You can buy" if money > fish_price else "you can not buy")
6. Removing duplicate item and keep one from the list
Duplicates item can be removed easily by using the set constructor like this
fruits = ["Apple", "Banana", "Orange", "Apple", "Coconut", "Apple"]
unique_fruits = list(set(fruits))
print(unique_fruits)
7. Simplify the if statement with multiple OR condition
It can easily simplify and clean the multiple or condition in an if statement like
fruits = "Apple"
if fruits == "Apple" or fruits == "Orange" or fruits == "Watermelon":
print("I eat this")
to this
if fruits in ["Apple", "Orange", "Watermelon"]:
print("I eat this")
8. Another way to clean if statement
if an if statement multiple comparisons/conditions exist like this
fruit = "Apple"
salad = "Cucumber"
drink = "7UP"
if fruit == "Apple" and salad == "Cucumber" and drink == "7UP":
print("WoW Good Meal")
if you need to pass all condition you can write this like
conditions = [
fruit == "Apple",
salad == "Cucumber",
drink == "7UP"
]
if all(conditions):
print("WoW Good Meal")
if you need to implement or condition you can use any like this
if any(conditions):
print("WoW Good Meal One Of This")
9. Flatten a 2D list
Generally, a 2D list can be flattened or convert as a single list like this
fruits = [ ["Apple", "Banana"], ["Coconut", "Orange"], ["Watermelon", "Mango", "Guava"]]
flat = []
for fruit in fruits:
for f in fruit:
flat.append(f)
print(flat)
The easiest way to do this
flat = [f for fruit in fruits for f in fruit]
print(flat)
here the f is the value we keep/append to the list.
If you like these tips and tricks please share this content and get more programming tips and tricks.