Thread: A Guide for Python View Single Post
 2019-11-04, 00:37 #6 Dylan14     "Dylan" Mar 2017 2·293 Posts Basic Constructs: strings, integers, floats, complexes, bools Okay, we've covered how to get python, we've covered how to be wary of our syntax, and we've learned how to comment and (hopefully) do it properly. We should be ready to go, right? Not so fast. In codes, unless we're doing something very simple like printing "Hello World!" to the screen (*), we will generally have functions that take some input, do stuff (**) with that input, and then return something back to the user, either directly or into another function. Now in something like C or C++, functions themselves have type, like int, void, etc. etc. This is not the case with Python. Here, the variables themselves are typed, and the functions are not. This is important! Okay, but what types are there to use? In standard Python 3, we have many different types that we can use. These include: * bool (a boolean, which can take the value 0 or 1, or alternatively "True" or "False" * int (an integer) * float (a floating point number) * complex (a complex number of the form a + bj) (***) * containers and iterators (iterate over objects within the container) * lists (mutable sequence, used to store collections of similar objects) * tuples (immutable sequences containing heterogeneous data, or for use in sets/dictionaries) * ranges (used for "for" loops) * strings (for text data) * and more (see https://docs.python.org/3.8/library/stdtypes.html for all built in types) Some modules will introduce other types. A notable example is the array class in numpy (which can be thought of as a matrix). Here, we will focus on 5 classes which appear very often in python: strings, integers, floats, complexes and bools. Bools: A bool is short for a boolean. This is a type which has two values: either 0 (denoting "off" or "false") or 1 (denoting "on" or "true"). The values themselves are not really helpful in themselves, but they are useful when setting conditions for while loops and for if/else statements, which we will cover later. The Boolean operators are "and", "or", and "not". To understand these, one should know the truth tables of these expressions (****). Let a and b be logical statements. Then the operators will yield *and - evaluates false if one of a or b are false. *or - evaluates false if both a and b are false. *not - evaluates true if the statement is false, else it's false. Booleans also come about when using comparisons, of which there are 8 of these defined in Python - < (less than), <= (less than or equal), > (greater than), >= (greater than), == (equal), != (not equal), is (object identity) and is not (negated object identity). Some of these operators (such as <, <=, > and >=) are only defined in certain cases. For example, if one of the arguments is a complex number, then a TypeError will be raised. Int: Int is short for integer. Integers are defined as you would expect in mathematics. You can have integers as large as you would like in Python 3, as long as you have enough memory. For example, here is M1693: Code: (2**1693)-1  yields Code: 440334444803013926723870430995863483703366909461783710471893932957885075695846526218526403061701134167211016236435527577005050015233222965976326940896095207044707758848508612460210362291110289652838518022120032380292451462329609095153231156707074655775395637629132002575140637348625684412427398984395674716936018562692034858963987306844991080918202832393268803551844553169715869473510245310131838410602687073367986808845044441458533294015993729099271507428272873558378015567915849627392090773401429909849505791 The usual operators can be used for integers, such as +, -, *, /, % (modulus), abs(x) (absolute value), and ^ (exponentiation, which can be implemented by using ** or pow(x, y)). There is also //, which is the floored quotient function (or integer division), which gives the whole integer value of the division, rounded down. Also, there is the divmod function, which gives the pair x // y, x % y. Float: Floats are floating point numbers. This type is needed because often times, it is not necessary to have infinite precision (or even possible) to have infinite precision, due to memory constraints. Depending on what scale we are working with (nanoscale or planetary scale), we need to have accuracy at different scales. To deal with this, floating point numbers are needed. Floating point numbers are defined by the IEEE 754 standard (*****). These have the same operations available as integers. Complexes: Complexes cover complex numbers. A complex number can be written in Python via the following: Code: complex(real, (imag)) For example, the number 3 + 4i can be written by typing: Code: complex(3, 4) or Code: 3+4j You can't use the % operator or the divmod operator for complexes. However, you also get a new operator in the c.conjugate() function which returns the complex conjugate. Strings: The last class we will cover are strings. Strings are used to convey text information. These can be parsed and stripped and otherwise manipulated (we will go over this later when we deal with list comprehension). A string is denoted by either single quotes ' ', double quotes " ", or triple quotes in either version (""" """ or ''' '''). The triple quotes can be used to do multiline strings. Strings can contain any character you'd like, but some characters need to be escaped. To escape a character, you use a \ (backslash). This is used, for example, if you need to add a tab or a new line character. You can also use \ to prefix special characters like the quotes, or for characters defined by their ASCII or Unicode values. For example, typing Code: print("\u03b3") will yield Code: γ the symbol for lowercase gamma. All of the escape characters can be found on this site: https://www.quackit.com/python/refer..._sequences.cfm. (*) The "Hello World" program is usually the first program that people learning to program will write up. Granted, it's not really that useful on its own (you could just type echo "hello world" in a command prompt and do the same thing without invoking python). However, printing stuff to screen can be useful when you go debug a program. I will cover this in the next section. (**) I'm pretty sure "stuff" is the technical term here. (***) This is the notation that Python uses for complexes, which is similar to what an electrical engineer would use. Mathematicians should interpret this a + bi. Still, they mean the same thing: $i = j = \sqrt {-1}$ (****) A truth table is a construct in logic used to determine the validity of a logical statement. More information can be found here: https://en.wikipedia.org/wiki/Truth_table. (*****) See https://www.geeksforgeeks.org/ieee-s...point-numbers/ for an overview. Last fiddled with by Dylan14 on 2020-08-11 at 14:04 Reason: actually spell out the symbol name, delete some extra return lines Firefox likes to add