Home > Backend Development > Python Tutorial > What are the hidden tricks in Python?

What are the hidden tricks in Python?

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2023-04-19 20:34:05
forward
1399 people have browsed it

1. Functional attributes

This hack is similar to the concepts of classes and objects. The called function attribute can now be declared and used later in the program.

I will show you a sample code below

# Function Attributes.
def func():
    func.name = "Haider Imtiaz"
    func.age = 22
    func.Profession = "Python developer"
func()
print("Name: ", func.name)
print("Age: ", func.age)
print("Profession: ", func.Profession)
# Output
Name:  Haider Imtiaz
Age:  22
Profession:  Python developer
Copy after login

2. Placeholder for incomplete code

We can use keyword password as a placeholder for incomplete code. Below I show some examples of using the Pass keyword in functions, classes, etc.

# Place Holders
def func():
    pass
class data:
    pass
for x in range(5):
    pass
if 2 == 4:
    pass
else:
    pass
Copy after login

3. eval() function

Eval() function accepts a string parameter. If the string parameter is an expression, then eval() will evaluate the expression.

Below I show a sample code.

# Eval
#example 1
x = 5
y = eval('x + 2')
print(y) # 7
#example 2
x = 2
y = eval('x ** 3')
print(y)# 8
Copy after login

4. Use the following command in the Python parser to start the file hosting server

# Starting a Web server
python -m http.server 5000
Copy after login

When you run the above command, you will see the following screen, which will display the steps to start the server hosting ip:port.

Serving HTTP on 0.0.0.0 port 5000 (http://0.0.0.0:5000/) ...

5. Unlimited parameters

This hack will show you how to pass unlimited arguments in a function call. Below I show sample code.

# Unlimited Arguments
# Python资料源码自取君羊:708525271 
def func(*arg):
    print("Argument Passed: ", len(arg))

func(1, 2, 4, 5, 6, 7)
# Output
# Argument Passed: 6
Copy after login

6. Zip() method

The zip method takes two iterable contents and merges them into a tuple. You can use this zip method to iterate lists and dictionaries. Below I show a sample code for better understanding.

# Zip method
list1 = ["Python", "JavaScript", "C#", "Dart"]
list2 = ["Machine Learning", "Web Developer", "Software", "App Dev"]
for x, y in zip(list1, list2):
    print(x, y)

# Output
Python Machine Learning
JavaScript Web Developer
C# Software
Dart App Dev
Copy after login

7. Rotate Lists

This simple trick will show you how to easily rotate lists. Take a look at the sample code below.

# Rotating list
lst = [10, 20, 30, 40, 50]
#rotate left
lst = lst[1:] + [lst[0]]
print(lst) # [20, 30, 40, 50, 10]
# rotate right
lst = [lst[-1]] + lst[:-1] 
print(lst) # [50, 10, 20, 30, 40]
Copy after login

The above is the detailed content of What are the hidden tricks in Python?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template