Python basic types8/11/2023 ![]() ![]() If you're unfamiliar with the difference between the = and is operators, check out our guide to Object Comparison in Python - = vs is! Complex Numbers - complex This of course happens because NaN was meant to behave this way, but it's still interesting. In fact, it's behavior can be seen as strange by comparing the values and references: > x = float( 'nan') Namely, running y = float('nan') would return False, even though y is, well, not a number. One interesting side-note here is how NaN behaves. This data type can be used to represent some special "numbers" like the NaN ("Not a Number"), +/-infinity, and exponents: > y = float( '-infinity') If, for some reason, we wanted a float variable that has the value 5, we'd need to explicitly let Python know: > y = 5.0 > type(y) However, as previously mentioned, if we only say 5 Python will consider it an int data type. For example, Python has no difficulty correctly understanding the following as a float: > y = 2.3 > type(y) Numbers that have more than 15 numbers after the dot will be truncated. This means that it can cover numbers such as 0.3, -2.8, 5.542315467, etc. The float data type can represent floating point numbers, up to 15 decimal places. ![]() ![]() So, let's try these out: # Decimal value of 5 > x = 5 > xĥ # Binary value of 1 > x = 0b001 > xĥ # Hexadecimal value of 10 > x = 0x10 > x 0x or 0X - Will turn your integer into Hexadecimal.0o or 0O - Will turn your integer into Octal.0b or 0B - Will turn your integer into Binary.What happens when you'd like to pack a numeric value in a different form? You can prefix a sequence of numbers and tell Python to treat them in a different system. The only real limit to how big an integer can be is the memory of the machine you're running Python on. In fact, we can even go as far as: > y = sys.maxsize + sys.maxsize This appears to be a 32-bit signed binary integer value, though, let's see what happens if we assign a higher number to x: > x = sys.maxsize The sys.maxsize may sound counterintuitive then, since it implies that that's the maximum value of an integer, though, it isn't. That is to say, unlike in some other languages like Java, the value of the int doesn't have a maximum value - it's unbounded. This sequence, in fact, isn't constrained. It's worth noting that Python treats any sequence of numbers (without a prefix) as a decimal number. We could have been more specific and said something along these lines, to make sure Python understood our 5 as an integer, though, it'll automatically do this exact same thing under the hood: > x = int( 5) If you give Python the following code, it will conclude that a is an integer and will assign the int data type to it: > x = 5 > type(x) This means values like 0, 1, -2 and -15, and not numbers like 0.5, 1.01, -10.8, etc. The int data type deals with integers values. These can be decimal values, floating point values or even complex numbers. These data types are fairly straight-forward and represent numeric values. We will also be using the Python shell so we don't have cumbersome code to print everything we want to show. In our examples we will occasionally use a function called type(variable) which returns, well, the type of the variable we passed to it. So something that was an int can end up being a str easily, if you assign it a string value. It's important to point out that Python usually doesn't require you to specify what data type you are using and will assign a data type to your variable based on what it thinks you meant.Īn equally important thing to point out is that Python is a "loosely/weakly typed" programming language, meaning that a variable can change its type over the course of the program's execution, which isn't the case with "strongly typed" programming languages (such as Java or C++). Dictionary: dict(dictionary) data type, consisting of (key, value) pairs. ![]()
0 Comments
Leave a Reply.AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |