在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:wilfredinni/python-cheatsheet开源软件地址:https://github.com/wilfredinni/python-cheatsheet开源编程语言:Jupyter Notebook 100.0%开源软件介绍:AboutBasic cheatsheet for Python mostly based on the book written by Al Sweigart, Automate the Boring Stuff with Python under the Creative Commons license and many other sources. ContributeAll contributions are welcome:
Read ItPython Cheatsheet
The Zen of PythonFrom the PEP 20 -- The Zen of Python:
>>> import this
The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those! Python BasicsMath OperatorsFrom Highest to Lowest precedence:
Examples of expressions in the interactive shell: >>> 2 + 3 * 6
20 >>> (2 + 3) * 6
30 >>> 2 ** 8
256 >>> 23 // 7
3 >>> 23 % 7
2 >>> (5 - 1) * ((7 + 1) / (3 - 1))
16.0 Data Types
String Concatenation and ReplicationString concatenation: >>> 'Alice' 'Bob'
'AliceBob' Note: Avoid String Replication: >>> 'Alice' * 5
'AliceAliceAliceAliceAlice' VariablesYou can name a variable anything as long as it obeys the following rules:
Example: >>> spam = 'Hello'
>>> spam
'Hello' >>> _spam = 'Hello'
CommentsInline comment: # This is a comment Multiline comment: # This is a
# multiline comment Code with a comment: a = 1 # initialization Please note the two spaces in front of the comment. Function docstring: def foo():
"""
This is a function docstring
You can also use:
''' Function Docstring '''
""" The print() Function>>> print('Hello world!')
Hello world! >>> a = 1
>>> print('Hello world!', a)
Hello world! 1 The input() FunctionExample Code: >>> print('What is your name?') # ask for their name
>>> myName = input()
>>> print('It is good to meet you, {}'.format(myName))
What is your name?
Al
It is good to meet you, Al The len() FunctionEvaluates to the integer value of the number of characters in a string: >>> len('hello')
5 Note: test of emptiness of strings, lists, dictionary, etc, should not use len, but prefer direct boolean evaluation. >>> a = [1, 2, 3]
>>> if a:
>>> print("the list is not empty!") The str(), int(), and float() FunctionsInteger to String or Float: >>> str(29)
'29' >>> print('I am {} years old.'.format(str(29)))
I am 29 years old. >>> str(-3.14)
'-3.14' Float to Integer: >>> int(7.7)
7 >>> int(7.7) + 1
8 Flow ControlComparison Operators
These operators evaluate to True or False depending on the values you give them. Examples: >>> 42 == 42
True >>> 40 == 42
False >>> 'hello' == 'hello'
True >>> 'hello' == 'Hello'
False >>> 'dog' != 'cat'
True >>> 42 == 42.0
True >>> 42 == '42'
False Boolean evaluationNever use NO (even if they are valid Python): >>> True == True
True >>> True != False
True YES (even if they are valid Python): >>> True is True
True >>> True is not False
True These statements are equivalent: >>> if a is True:
>>> pass
>>> if a is not False:
>>> pass
>>> if a:
>>> pass And these as well: >>> if a is False:
>>> pass
>>> if a is not True:
>>> pass
>>> if not a:
>>> pass Boolean OperatorsThere are three Boolean operators: and, or, and not. The and Operator’s Truth Table:
The or Operator’s Truth Table:
The not Operator’s Truth Table:
Mixing Boolean and Comparison Operators>>> (4 < 5) and (5 < 6)
True >>> (4 < 5) and (9 < 6)
False >>> (1 == 2) or (2 == 2)
True You can also use multiple Boolean operators in an expression, along with the comparison operators: >>> 2 + 2 == 4 and not 2 + 2 == 5 and 2 * 2 == 2 + 2
True if Statementsif name == 'Alice':
print('Hi, Alice.') else Statementsname = 'Bob'
if name == 'Alice':
print('Hi, Alice.')
else:
print('Hello, stranger.') elif Statementsname
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论