- Disclaimer: This page is currently a mess... I have python 3. These notes may not be correct for other versions. OTOH some of these notes are indeed only relevant to python 2. It's a work in progress...
- Why do you care about data types? Because many operations will only work on specific datatypes,
and most functions require arguments of specific datatypes.
- Most Helpful Command: So you have a variable... what data type is it?
print( type( [varName] ) )
----------------------------------------------------------------
- Everything in Python is an object. All objects have 3 parameters: "identifiers" (names), values, and data types.
Since everything is an object, data types are actually classes, and the variables are instances of the class.
In fact, when you enter: print( type(4) ), it returns: "class int"
- The data type of an object/variable is not declared/defined explicitly in python (unlike some other programming languages - Java & Arduino eg.).
Instead, it is done automatically by python, depending on the value of the variable.
- So for example, if you define a variable:
var01 = 1
it will be in integer (int) type.
- if instead you define the variable:
var01 = 1.0
then it will be type float (due to the decimal point)
----------------------------------------------------------------
- Data types can be categorized in several ways:
- Whether they are composed of single vs. multiple entities/objects:
- integers and bytes just have single objects
- strings, lists, byte-arrays and others have multiple objects. They are 'sequences' of objects.
- There is a format/protocol for referring to individual objects within the sequence:
eg. suppose var01 = "asdaf"
print( var01[2] ) returns: 'd' (note the index in the brackets is 'zero indexed',
meaning '0' refers to the first element of the sequence, '1' refers to the second, etc.)
- Whether they are mutable or not:
- Some data types are immutable, meaning they cannot be 'changed in place'.
- For example, strings are immutable. So if you define a string:
var01 = "asdf"
then you cannot change elements of it:
var01[1] = 'x' will give an error
- You CAN, however, redefine the whole string with:
var01 = "axdf"
- Whether they are built-in (native) or user-created:
----------------------------------------------------------------
- ...
https://diveintopython3.net/native-datatypes.html
Python has many native datatypes. Here are the important ones:
Booleans are either True or False.
Numbers can be integers (1 and 2), floats (1.1 and 1.2), fractions (1/2 and 2/3), or even complex numbers.
Strings are sequences of Unicode characters, e.g. an HTML document.
Bytes and byte arrays, e.g. a JPEG image file.
Lists are ordered sequences of values.
Tuples are ordered, immutable sequences of values.
Sets are unordered bags of values.
Dictionaries are unordered bags of key-value pairs.
-------------------------------------------------
- Conversion between Data Types:
- random notes:
- s = "6a4f" - a 'hex string'
i = int(s, 16) - converts to integer
str(i) - converts it to a string
- ...
- Float to a String:
- String to Bytes:
- b = bytes(mystring, 'utf-8')
- -or- b = mystring.encode('utf-8')
- Integer to Bytes:
- int.to_bytes(length, byteorder)
- byteorder can be 'little' or 'big'
- eg:
bytes2 = int2.to_bytes(3, 'big')
- Integer to Binary:
- bin3 = bin(int2)
- The binary returned will be a string prefixed with “0b”.
- converting to Hex:
- print(hex(255)) # decimal
output: 0xff
- print(hex(0b111)) # binary
output: 0x7
- print(hex(0o77)) # octal
output: 0x3f
- print(hex(0XFF)) # hexadecimal
output: 0xff
- convert string to hex:
hex_string = "0xFF"
an_integer = int(hex_string, 16)
hex_value = hex(an_integer)
print(hex_value)
- not sure what this is...
- You have a sequence of raw unicode that was saved into a str variable:
s_str: str = "\x00\x01\x00\xc0\x01\x00\x00\x00\x04"
You need to be able to get the byte literal of that unicode (for struct.unpack(), etc.)
s_bytes: bytes = b'\x00\x01\x00\xc0\x01\x00\x00\x00\x04'
Solution:
s_new: bytes = bytes(s, encoding="raw_unicode_escape")
-------------------------------------------------------------
https://stackoverflow.com/questions/21017698/converting-int-to-bytes-in-...
.
.
.