Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What exercises does Python have?

2025-03-13 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the relevant knowledge of what exercises Python has, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value. I believe you will gain something after reading this Python exercise article. Let's take a look at it.

Example 051: bitwise vs.

Topic: learn to use bitwise and &.

Program analysis: 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0

A=0o77print (a) b=a&3print (b) b=b&7print (b) instance 052: bitwise or

Topic: learn to use bitwise or |.

Program analysis: 0 | 0: 0; 0 | 1: 1; 1 | 0: 1; 1 | 1: 1

A=0o77print (a | 3) print (a | 3 | 7) instance 053: XOR by bit

Topic: learn to use bitwise XOR ^.

Program analysis: 0 ^ 0 = 0; 0 ^ 1 = 1; 1 ^ 0 = 1; 1 ^ 1 = 0

A=0o77print (a ^ 3) print (a ^ 3 ^ 7) instance 054: bit inversion, bit shift

Topic: take an integer a starting from the right end of the 4-7 digits.

Program analysis: can be considered as follows: (1) move a to the right 4 bits first. (2) set a number where the lower 4 bits are all 1s and the rest are all zeros. Available ~ (0 self.get_len (): print ("unreasonable given position") return if index = = 1: self.head = self.head.next return temp = self.head cur_pos = 0 while temp is not None: cur_pos + = 1 if cur_pos = = index-1: Temp.next = temp.next.next temp = temp.next def insert (self Pos, node): if pos

< 1 or pos >

Self.get_len (): print ("unreasonable insertion node") return temp = self.head cur_pos = 0 while temp is not Node: cur_pos + = 1 if cur_pos = = pos-1: node.next = temp.next temp.next = node break Temp = temp.next def reverse (self Head): if head is None and head.next is None: return head pre = head cur = head.next while cur is not None: temp = cur.next cur.next = pre pre = cur cur = temp head.next = None return pre def print_list (self Head): init_data = [] while head is not None: init_data.append (head.get_data ()) head= head.next return init_dataif _ _ name__=='__main__': head=Node ('head') link=List (head) for i in range (10): node=Node (I) link.append (node) print (link.print_list (head)) example 073: reverse output linked list

Topic: output a linked list in reverse.

Class Node: def _ _ init__ (self, data): self.data = data self.next = None def get_data (self): return self.dataclass List: def _ _ init__ (self Head): self.head = head def is_empty (self): return self.get_len () = = 0 def get_len (self): length = 0 temp = self.head while temp is not None: length + = 1 temp = temp.next return length def append (self Node): temp = self.head while temp.next is not None: temp = temp.next temp.next = node def delete (self, index): if index

< 1 or index >

Self.get_len (): print ("unreasonable given position") return if index = = 1: self.head = self.head.next return temp = self.head cur_pos = 0 while temp is not None: cur_pos + = 1 if cur_pos = = index-1: Temp.next = temp.next.next temp = temp.next def insert (self Pos, node): if pos

< 1 or pos >

Self.get_len (): print ("unreasonable insertion node") return temp = self.head cur_pos = 0 while temp is not Node: cur_pos + = 1 if cur_pos = = pos-1: node.next = temp.next temp.next = node break Temp = temp.next def reverse (self Head): if head is None and head.next is None: return head pre = head cur = head.next while cur is not None: temp = cur.next cur.next = pre pre = cur cur = temp head.next = None return pre def print_list (self Head): init_data = [] while head is not None: init_data.append (head.get_data ()) head= head.next return init_dataif _ _ name__=='__main__': head=Node ('head') link=List (head) for i in range (10): node=Node (I) link.append (node) print (link.print_list (head)) print (link.print_list (link.reverse (head) instance 074: list sort, Connect

Topic: list sorting and connection.

Program analysis: sorting can use the sort () method, connections can use the + sign or extend () method.

A.extend (b) a.sort () print (a) example 075: do not know what to say

Topic: relax, it's a simple question.

If _ _ name__ = ='_ main__': for i in range (5): n = 0 if I! = 1: n + = 1 if I = = 3: n + = 1 if I = = 4: n + = 1 if I! = 4: n + = 1 if n = 3: print (64 + I) this is the end of the article on "what exercises are there in Python". Thank you for reading! I believe you all have a certain understanding of the knowledge of "what exercises do you have in Python". If you want to learn more, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report