Following lines of code shows how we can declare and initialize variables in Python.
Note that the lines starting with ">>>"
in the following snippet shows the code you type in your Python interpreter while the lines without ">>>" shows the output of the Python interpreter
#a variable marks with value 20
>>> marks = 20
>>> print marks
#a variable name with a string
>>> name = "Emily"
>>> print name
#a variable with a decimal number
>>> pi = 3.14
>>> print pi
#a variable grade char storing a single char
>>> grade = 'E'
>>> print grade
As we can see in the above code snippet, we don't need to tell Python whether marks is of type int
and similarly we don't tell whether name is a String
. Python interpreter
takes care of it for us.
Binary, Octal and Hexadecimal Numbers in Python
Decimal number system is natural to us because we've been using it for centuries but when it comes to digital systems like computers and smart-phones, there are other number systems which are more relevant. In this section, we'll see how we can convert
data from decimal to more computer friendly number systems like binary, octal and hexadecimal. We'll also do the reverse of that.
Representation of Numbers >>
Decimal numbers are represented simply by writing the original number: e.g. 32, 12 etc
Hexadecimal numbers are preceded by a 0x
Binary numbers are preceded by a 0b
Octal numbers are preceded by a 0
Let's go ahead and see how to perform number-system conversions in Python.
1. Decimal - Binary Conversions in Python
# convert decimal 20 to binary
>>> b20 = bin(20)
>>> print b20
# convert decimal 2 to binary
>>> b2 = bin(2)
>>> print b2
# convert binary 0b10100 to decimal
>>> d1 = int(0b10100)
>>> print d1
# convert binary 0b101010101010 to decimal
>>> d2 = int(0b101010101010)
>>> print d2
Note that the hex(), oct() and bin() function actually return a representation of the corresponding number as a String. So if you check the type(bin(10)) the result would be a String.
2. Decimal - Hexadecimal Conversions in Python
# convert decimal 3735928559L to hexadecimal **[L stands for long integers in Python]
>>> hexNum = hex(3735928559L)
>>> print hexNum
# convert decimal 16 to hexadecimal
>>> hex16 = hex(16)
>>> print hex16
# convert hexadecimal 0xdeadbeef to decimal
>>> dbf = int(0xdeadbeef)
>>> print dbf
# convert hexadecimal 10 to decimal
>>> d10 = int(0x10)
>>> print d10
2. Decimal - Octal Conversions in Python
# convert decimal 8 to octal
>>> dec8 = oct(8)
>>> print dec8
# convert octal 0100 to decimal
>>> oct100 = int(0100)
>>> print oct100
Python Shell can be easily used to perform simple mathematical operations like addition, subtraction, division, multiplication, remainder etc. Note that if we use String values instead of int values, the plus '+' operator will act as a concatenation operator
and it will join the Strings as shown in below code snippet.
For best results, it's always recommended to try the code in your Python Shell.
We've created a repository on github to keep pushing new code as we add new days. In case you're enthusiastic about it, following is link to that repository.