Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- print('Hello, world!') # Hello, world!
- print("hello".upper()) # HELLO
- print("""hello""".upper()) # HELLO
- print("""hello
- """.upper())
- # HELLO
- #
- print(""" Hello
- world ...""".lower())
- # hello
- # world ...
- print(("this is a very"
- " long string too"
- "for sure ..."
- )) # this is a very long string toofor sure ...
- print(str(1/1)) # 1.0
- print(type(str(1/1))) # <class 'str'>
- print(type(1/1)) # <class 'float'>
- print('T'.lower() in 'beTa') # False
- print('T'.lower() in 'bEta') # False
- print(ord('b')) # 98
- print(ord('a')) # 97
- print(chr(97)) # a
- print(ord('b') + 2) # 100
- print(chr(ord('b') + 2)) # d
- print(chr(ord('b') + 2) in 'dogs') # True
- print('F' in 'False') # True
- # print('F' in False) # TypeError: argument of type 'bool' is not iterable
- import math
- print(math) # <module 'math' (built-in)>
- print(type(math)) # <class 'module'>
- print(dir(math))
- """
- ['__doc__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'comb', 'copysign', 'cos', 'cosh', 'degrees', 'dist', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'gcd', 'hypot', 'inf', 'isclose', 'isfinite', 'isinf', 'isnan', 'isqrt', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'log2', 'modf', 'nan', 'perm', 'pi', 'pow', 'prod', 'radians', 'remainder', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'tau', 'trunc']
- """
- import math
- print(math) # <module 'math' (built-in)>
- print(type(math)) # <class 'module'>
- pi = 3.141592653589793
- # print(math.round(pi)) # AttributeError: module 'math' has no attribute 'round'
- print(math.ceil(pi)) # 4
- print(round(pi, 0)) # 3.0
- print(round(pi)) # 3
- print(math.trunc(pi)) # 3
- print(math.floor(pi)) # 3
- print(round(5.76543, -1)) # 10.0
- print(round(5.76543, -10)) # 0.0
- print(round(576543, -1)) # 576540
- print(round(576543, -10)) # 0
- print(round(576543, -5)) # 600000
- from math import sqrt
- # print(math) # NameError: name 'math' is not defined
- print(sqrt) # <built-in function sqrt>
- # print(sqrt()) # TypeError: sqrt() takes exactly one argument (0 given)
- print(sqrt(25)) # 5.0
- # print(dir(math)) # NameError: name 'math' is not defined
- import platform
- # print(system) # NameError: name 'system' is not defined
- print(platform.system) # <function system at 0x7f65bf600280>
- print(platform.system()) # Linux
- print(platform.processor) # <function processor at 0x7f65bf600550>
- print(platform.processor()) #
- print(platform.machine) # <function machine at 0x7f65bf6004c0>
- print(platform.machine()) # x86_64
- print(platform.platform()) # Linux-5.13.0-1023-gcp-x86_64-with-glibc2.27
- print(platform.version()) # #28~20.04.1-Ubuntu SMP Wed Mar 30 03:51:07 UTC 2022
- from platform import system
- print(system()) # Linux
- """
- $ pip show pandas
- Name: pandas
- Version: 1.2.3
- Summary: Powerful data structures for data analysis, time series, and statistics
- Home-page: https://pandas.pydata.org
- Author: None
- Author-email: None
- License: BSD
- Location: /path/to/lib/python3.6/site-packages
- Requires: pytz, python-dateutil, numpy
- Required-by: shap, seaborn
- python -m pip list
- Package Version
- ------- -------
- docopt 0.6.2
- idlex 1.13
- jedi 0.9.0
- python3 -m pip search peppercorn
- (Search for PyPI packages whose name or summary contains <query>.)
- pepperedform - Helpers for using peppercorn with formprocess.
- peppercorn - A library for converting a token stream into [...]
- """
- try:
- print(int('0'))
- except NameError:
- print('0')
- else:
- print(int('')) # ValueError: invalid literal for int() with base 10: ''
- try:
- print(int('0')) # 0
- except NameError:
- print('30')
- try:
- print(0/0)
- except :
- print(0/1) # 0.0
- else:
- print(2/0)
- import math
- try:
- print(math.sqrt(-1))
- except :
- print(math.sqrt(1)) # 1.0
- else:
- print(math.sqrt(25))
- try:
- print(float("1e1"))
- except (NameError, SystemError):
- print(float("1.1"))
- else:
- print(float('1c1')) # ValueError: could not convert string to float: '1c1'
- print(float("1e1")) # 10.0
- print(float("1.1")) # 1.1
- try:
- print(1//0)
- except ArithmeticError:
- print("Zero Handled by arithmetic branch")
- except ZeroDivisionError:
- print("Error handled by zero division branch")
- except:
- print("Error handled by except branch")
- print(1//0) # ZeroDivisionError: integer division or modulo by zero
- print(issubclass(KeyError, Exception)) # True
- print(issubclass(KeyError, Exception)) # true
- print(issubclass(LookupError, Exception)) # true
- print(issubclass(SystemExit, Exception)) # False
- print(issubclass(KeyboardInterrupt, Exception)) # False
- print(issubclass(SystemExit, BaseException)) # True
- print(issubclass(KeyboardInterrupt, BaseException)) # True
- ----
- def foo(x):
- assert x
- return 1/x
- try:
- print(foo(0))
- except ZeroDivisionError:
- print("zero")
- except AssertionError as e:
- print(e) #
- except:
- print(some)
- ----
- def ooh(x):
- assert x, 'message goes here'
- return 1/x
- try:
- print(ooh([]))
- except ZeroDivisionError:
- print("zero")
- except AssertionError as e:
- print(e) # message goes here
- except:
- print(some)
- ----
- x = 1E2500 ** 1
- print(x) # inf
- x = 1E250 * 2
- print(x) # 2e+250
- x = 1E308 # 1e+308
- print(x) # inf
- x = 1E309
- print(x) # inf
- x = 1E250 ** 2
- print(x) # OverflowError: (34, 'Numerical result out of range')
- # Unicode Transformation Format (UTF)
- string = """\\
- \\"""
- print(len(string)) # 3
- print(string)
- """
- \
- \
- """
- string2 = '''\\
- \\'''
- print(len(string2)) # 12
- print(string2)
- """
- \
- \
- """
- alphabet = "abcdefghijklmnopqrstuvwxyz"
- alphabet.insert(0, "A") # AttributeError: 'str' object has no attribute 'insert'
- del alphabet[0] # TypeError: 'str' object doesn't support item deletion
- print(alphabet[-30:30:1]) # abcdefghijklmnopqrstuvwxyz
- print(alphabet[-30:30:-1]) #
- print(alphabet.lstrip()) # abcdefghijklmnopqrstuvwxyz
- x = "True"
- y = 3
- print(x == y) # False
- print(x != y) # True
- print(x < y) # TypeError: '<' not supported between instances of 'str' and 'int'
- print(x > y) # TypeError: '>' not supported between instances of 'str' and 'int'
- for ch in 'aBc123':
- if ch.isupper():
- print(ch.lower(), end='\n' )
- elif ch.islower():
- print(ch.upper(), end='')
- else:
- print(ch, end='')
- """
- Ab
- C123
- """
- s1 = "dog cat rat"
- s2 = s1.split()
- print(s2[-4:1]) # ['dog']
- print(s2) # ['dog', 'cat', 'rat']
- print(s1.split('a')) # ['dog c', 't r', 't']
- print(s2[-3:1]) # ['dog']
- print(s2[-3:0]) # []
- # Slicing Start
- a[start:stop] # items start through stop-1
- a[start:] # items start through the rest of the array
- a[:stop] # items from the beginning through stop-1
- a[:] # a copy of the whole array
- a[start:stop:step] # start through not past stop, by step
- a[-1] # last item in the array
- a[-2:] # last two items in the array
- a[:-2] # everything except the last two items
- a[::-1] # all items in the array, reversed
- a[1::-1] # the first two items, reversed
- a[:-3:-1] # the last two items, reversed
- a[-3::-1] # everything except the last two items, reversed
- a = [1,2,3,4,5,6,7,8,9,0]
- print(a[0:3]) # [1, 2, 3]
- print(a[4:]) # [5, 6, 7, 8, 9, 0]
- print(a[:5]) # [1, 2, 3, 4, 5]
- print(a[:]) # [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
- print(a[2:6:2]) # [3, 5]
- print(a[-1]) # 0
- print(a[-2:]) # [9, 0]
- print(a[:-2]) # [1, 2, 3, 4, 5, 6, 7, 8]
- print(a[::-1]) # [0, 9, 8, 7, 6, 5, 4, 3, 2, 1]
- print(a[1::-1]) # [2, 1]
- print(a[:-3:-1]) # [0, 9]
- print(a[-3::-1]) # [8, 7, 6, 5, 4, 3, 2, 1]
- # Slicing End
- my_list = ['Hello', 'World', '!']
- s = '\\'.join(my_list)
- print(s)
- my_list = ['Hello', 'World', '!']
- s = '\\'.join(my_list)
- print(s) # Hello\World\!
- my_Tuple = ("John", "Peter", "Vicky")
- x = "#".join(my_Tuple)
- print(x) # John#Peter#Vicky
- myDict = {"name": "John", "country": "Norway"}
- mySeparator = "TEST"
- y = mySeparator.join(myDict)
- print(y) # nameTESTcountry
Add Comment
Please, Sign In to add comment