Contact Us

Data and Operations

Day-2 of A Month in the Python's Cave

getting started with meteor
Python is Dynamic and Interpreted
In many statically typed languages like Java, you've to declare variables and set a data-type for your variables otherwise the compiler will throw an error. In Python which is a dynamic language, you don't have to set the data-type of a variable. This will be taken care of by Python interpreter during runtime. In order to use a variable you can simply assign a value to the variable and reference it in your program.
You can execute the following in your Python interpreter and see results for yourself.
Variables in Python
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 e.g. 0xdeadbeaf
Binary numbers are preceded by a 0b e.g. 0b10
Octal numbers are preceded by a 0 e.g. 010
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 as Calculator
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.

				>>> a = 100
				>>> b = 50
				>>> print a + b
				>>> print a - b
				>>> print a * b
				>>> print a / b
				>>> print a % b 
				# if you want to raise x to the power of y
				>>> x = 2
				>>> y = 3
				>>> print x ** y
				# '+' example of plus operator with strings
				>>> alice = 'I want to'
				>>> bob = ' enjoy python cave'
				>>> print alice + bob
				I want to enjoy python cave

Finally! You've complete yet another day at the Python's Cave and you learned about Data and Operations in Python.
PythonCave on Github
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.

developed & nourished with by rishabh.io