A.I, Data and Software Engineering

Advanced python: Styling, Boolean, String and bytes

A

Let get some more advanced python techniques, tools, libraries.

Python coding style

It is quite common for beginner to skip the coding style and conventions in any programming language. But when you have a chance to revisit your code or others’ code, you will soon recognise how important of following coding conventions.

To learn more, you can visit PEP 8 — Style guide for Python code that includes everything you need to know, such as indentation, commenting, declaration, initialization etc. Examples:

# No extra indentation.
if (this_is_one_thing and
    that_is_another_thing):
    do_something()
# Add a comment, which will provide some distinction in editors
my_list = [
    1, 2, 3,
    4, 5, 6,
]
result = some_function_that_takes_arguments(
    'a', 'b', 'c',
    'd', 'e', 'f',
)

True value testing

In Python, any object can be tested for Boolean truth value. Any object is considered to be equivalent to Boolean true, unless its class defines a Bool method that returns false, or has a len method that returns a zero-length. So, let’s look first at the built-in objects that evaluate to false

  •  False and None evaluate to false
  • Numeric zero values: 0, 0.0, 0j
  • Decimal(0), Fraction(0, x)
  • Empty set and sequence/collections: {}, []. Keep in mind that if you are familiar with JavaScript, JS will return True when you evaluate empty sets.
x = []
print(bool(x))
#False

In custom objects, they are by default considered to be true, unless they override the boolfunction and return a false value, or they override the len function and return a value of zero.

class myClass:
    def __bool__(self):
        return False
    def __len__(self):
        return 0
mc = myClass()
print(bool(mc))
#False

Strings and bytes

In Python 3, there are very important differences between the notions of strings and bytes. A string in Python 3 is a sequence of Unicode characters, while bytes are a sequence of raw eight-bit values.

# define some starting values
b = bytes([0x41, 0x42, 0x43, 0x44])
print(b)
#b'ABCD'
s = "This is a string"
print(s)
#This is a string

 If you want to combine a string with bytes, you have to decode the bytes into a string. And I can do that using the built-in decode function. Likewise, you can use encode to convert a string to bytes.

# Try combining them. This will cause an error:
# print(s+b)
# Bytes and strings need to be properly encoded and decoded
# before you can work on them together
s2 = b.decode('utf-8')
print(s+s2)
#This is a stringABCD
b2 = s.encode('utf-8')
print(b+b2)
#b'ABCDThis is a string'
# encode the string as UTF-32
b3 = s.encode('utf-32')
print(b3)
#b'\xff\xfe\x00\x00T\x00\x00\x00h\x00\x00\x00i\x00\x00\x00s\x00\x00\x00 \x00\x00\x00i\x00\x00\x00s\x00\x00\x00 \x00\x00\x00a\x00\x00\x00 \x00\x00\x00s\x00\x00\x00t\x00\x00\x00r\x00\x00\x00i\x00\x00\x00n\x00\x00\x00g\x00\x00\x00'

Thanks for reading. Read more other advanced python articles.

1 comment

💬

A.I, Data and Software Engineering

PetaMinds focuses on developing the coolest topics in data science, A.I, and programming, and make them so digestible for everyone to learn and create amazing applications in a short time.

Categories