Hello, Hello everyone , I'm Chen Chen ~
Python Because of the simplicity and readability of its code, it has become a very popular language . It's one of the simplest languages you choose . If you are python Beginners of basic concepts , So this is the best time to learn to write better code .
python There are many techniques that can improve the program better than before . This article will help you understand python Various techniques available in . Keep practicing them , Until it becomes part of your programming habits .
I will share 30 It's a magic Python skill To help you write better code , It will be divided into two times to share ~
today Share first 15 individual , Let's see !
Python Allows us to assign values to multiple variables in a row . You can use commas to separate variables . Multitasking on the front line has many advantages . It can be used to assign multiple values to multiple variables or to assign multiple values to a single variable name . Let's make a problem statement , Where we must set the value 50 and 60 Assign to a variable a and b. The general code is as follows .
a = 50 b = 60 print(a,b) print(type(a)) print(type(b))
Output
50 60 <class 'int'> <class 'int'>
Conditions I- Value equals variable
When a variable is equal to multiple assignments , Each value will be stored in all variables .
a , b = 50 , 60 print(a,b) print(type(a)) print(type(b))
Output
50 60 <class 'int'> <class 'int'>
The two programs give the same results . This is the benefit of using a row value assignment .
Conditions II- Value greater than variable
Let's try increasing the number of values in a program . You can assign multiple values to a single variable . When assigning multiple values to a variable , We must use an asterisk before the variable name .
a , *b = 50 , 60 , 70 print(a) print(b) print(type(a)) print(type(b))
Output
50 [60, 70] <class 'int'> <class 'list'>
The first value is assigned to the first variable . The second variable will collect values from the given values . This will create a list type object .
Conditions III- Multivariable a value
We can assign a value to multiple variables . Each variable will be separated by an equal sign .
a = b = c = 50 print(a,b,c) print(type(a)) print(type(b)) print(type(c))
Output
50 50 50 <class 'int'> <class 'int'> <class 'int'>
Exchange is the process of exchanging the values of two variables with each other . This is useful in many operations of Computer Science . ad locum , I have written two main methods for programmers to exchange knowledge and the best solution .
Method I- Use temporary variables
This method uses temporary variables to store some data . The following code is written using temporary variable names .
a , b = 50 , 60 print(a,b) temp = a+b #a=50 b=60 temp=110 b = a #a=50 b=50 temp=110 a = temp-b #a=60 b=50 temp=110 print("After swapping:",a,b)
Output
50 60 After swapping: 60 50
Method II- Do not use temporary variables
The following code exchanges variables without using temporary variables .
a , b = 50 , 60 print(a,b) a = a+b #a=110 b=60 b = a-b #a=110 b=50 a = a-b #a=60 b=50 print("After swapping:",a,b)
Output
50 60 After swapping: 60 50
Method III-Python Excellent solutions in
This is the use of python Another way to exchange variables . In the last section , We learned about multiple assignments . We can use the concept of exchange .
a , b = 50 , 60 print(a,b) a , b = b , a print("After swapping",a,b)
Output
50 60 After swapping 60 50
There is another cool technique that can be used in python Reverse string in . The concept used to invert a string is called string slicing . You can use the symbol... After the variable name [::-1] Invert any string .
my_string = "MY STRING" rev_string = my_string[::-1] print(rev_string)
Output
GNIRTS YM
No special algorithm is needed to split words into one line . So , We can use keywords split(). ad locum , I wrote two ways to segment words .
Method 1 - Using the iterative method
my_string = "This is a string in Python" start = 0 end = 0 my_list = [] for x in my_string: end=end+1 if(x==' '): my_list.append(my_string[start:end]) start=end+1 my_list.append(my_string[start:end+1]) print(my_list)
Output
['This ', 'is ', 'a ', 'string ', 'in ', 'Python']
Method II- Use the split function
my_string = "This is a string in Python" my_list = my_string.split(' ') print(my_list)
Output
['This ', 'is ', 'a ', 'string ', 'in ', 'Python']
This is the opposite of the previous process . In this part , We will use join Function to convert a list of words to a single line . The use of join Syntax of functions .
grammar :"" .join(string)
my_list = ['This' , 'is' , 'a' , 'string' , 'in' , 'Python'] my_string = " ".join(my_list)
Output
This is a string in Python
We can use the multiplication operator to print strings many times . This is a very effective way to repeat strings .
n = int(input("How many times you need to repeat:")) my_string = "Python\n" print(my_string*n)
Output
How many times you need to repeat:3 PythonPythonPython
Can be used without join Function to connect various strings . We can only use the addition operator (+) To perform this operation .
a = "I Love " b = "Python" print(a+b)
Output
I Love Python
Two can combine two or more conditional operators in a program , We can use logical operators . however , You can get the same result through the link operator . for example , If we need the value of the variable to be greater than 10 And less than 20 When printing something , The code will be similar to the following .
a = 15 if (a>10 and a<20): print("Hi")
In its place , We can combine conditional operators into a single expression .
a = 15 if (10 < a < 20): print("Hi")
Output
Hi
Elements that appear most of the time in the list , Then it will become the most frequent element in the list . The following code snippet will help you get the most frequent elements from the list .
my_list = [1,2,3,1,1,4,2,1] most_frequent = max(set(my_list),key=my_list.count) print(most_frequent)
Output
1
The previous code will provide the most frequent values . If we need to know the appearance of all the unique elements in the list , You can use the collection module . The assembly is python A great module in , It provides powerful functions . Counter Method provides a dictionary with elements and occurrence pairs .
from collections import Counter my_list = [1,2,3,1,4,1,5,5] print(Counter(my_list))
Output
Counter({1: 3, 5: 2, 2: 1, 3: 1, 4: 1})
If a string consists of characters in another string , Then the two strings are word puzzles . We can do it in collections The same... Is used in the module Counter Method .
from collections import Counter my_string_1 = "RACECAR" my_string_2 = "CARRACE" if(Counter(my_string_1) == Counter(my_string_2)): print("Anagram") else: print("Not Anagram")
Output
Anagram
function range() Useful for creating a sequence of numbers . In many code snippets , It can be very useful . The syntax of the scope function is written here .
grammar :range( Start , end , step )
Let's try to create an even list .
my_list = list(range(2,20,2)) print(my_list)
Output
[2, 4, 6, 8, 10, 12, 14, 16, 18]
Similar to string multiplication , We can use the multiplication operator to create a list of filled elements multiple times .
my_list = [3] my_list = my_list*5 print(my_list)
Output
[3, 3, 3, 3, 3]
in the majority of cases , We are Python Nested conditional structures are used in . In addition to using nested structures , You can also replace a line with the help of ternary operators . The syntax is given below .
grammar :if True then Statement1 else Statement2
if age > 20 then age = 25, print("czz")else print(" unqualified ")
Output
qualified
A list derivation expression is a very compact way to create a list from another list . Look at the code below . The first is written using simple iterations , The second is understood using lists .
square_list = [] for x in range(1,10): temp = x**2 square_list.append(temp) print(square_list)
Output
[1, 4, 9, 16, 25, 36, 49, 64, 81]
Use a list to derive an expression
square_list = [x**2 for x in range(1,10)] print(square_list)
Output
[1, 4, 9, 16, 25, 36, 49, 64, 81]
Okay , Today my sharing is over , The other half , I'll share it with you next time