Rewind:
In the previous part we learnt about the introduction to python, installing it and finally writing a program to print “Hello World”. That was a great start but now we all go on the real part.
Data Types:
The representation of a value is said to be a data type. Well to be clear what will you call this “1” as, it’s obviously a number so we are representing 1 as number. This is what data type is. There is several data type that we use every time during our programming, some familiar ones are
Integer – It can be any (negative or positive) number without any Decimal Point
E.g.: -12
Float – It can be any (negative or positive) number with a Decimal Point
E.g.: 23.22
String – A group of characters including numbers
E.g.: My Name is Raghunath.J
There are still other different data types and you will discover them very shortly.
Variable:
A variable is just a memory to store your data values. To identify these memories we just give them a name. The value in the variable is nothing but called as data types.
Identifiers:
Name itself says, identifying something. Variable is the best example for an identifier.
There are some rules you need to follow when you are using identifiers.
- Must Start with a Alphabet (Upper or lower case) or with an underscore (‘_’)
Example: a, Raf, _rt, a434
- The names are case sensitive. That is hello and Hello are totally different and they are never related to each other.
Give a try:
Which are valid and which are not?
A, __34, _4e, _!ss,#rt, raghu,raghu332, r_565, ^ss
(Mail me your answers to talkonsomething@gmail.com)
Objects
As I have already said python is an object oriented programming. So everything in the program is referred as object.
To be very clear here is an example
“I brought a Car”
Here “Car” is an object. So please remember that something is always named as object.
Now we learnt something about data types, variables, identifiers and object. Let us write a simple example programming.
# To add two numbers a = 12 b = 20 c = a + b print(a) print(b) print(“Total”,c)
The output for the above code will be
12
20
Total 32
Explanation of the above code:
Line 1: Added a comment stating that the code will add two numbers (i.e. data type).
Line 2: Assigning constant value 12 to a variable “a”
Line 3: Assigning constant value 20 to a variable “b”
Line 4: Printing the value stored in a. In this line you can observe that we are not using Quotes. If in case we use quotes the result of this line will not be 12 instead it will be a.
Line 5: Printing the value stored in b.
Line 6: Adding the values present in variable a and b and storing it in c. We are using a operator + between a and b. We will learn more about the operator a bit later.
Line 7: Printing the value present in c. We will learn more about displaying in the future episodes.
Hope you understood what is happening in the above example. In .net and other high level programming language we need to assign a data type to the variable before we use them.
Example in VB.net
<
>Dim a, b, c as Integer
But in Python we didn’t assign any data type to a, b or c but it understood that these variable belongs to Integer data type by seeing the values.
How can it understand?
In Line 2: a =12, we know that 12 is an integer and = means assigning the value 12 to a (more about = will be said later) so python interpreter understood that the variable should be taken as Integer
But instead if the line 2 was replaced as a = “12” then it would be considered that as string because we are using quotes between the numbers. This is one of the added advantages in Python at the same time there might be even disadvantages.
Now we have learnt the usage and proper meaning of variables and data types. Let us write one more code but this time let us use another command called input for users to input a number.
# Accept user input values and add them print(“Enter a number: “) a = int(input()) print(“Enter another number: “) b = int(input()) c = a + b print(“Total is “,c)
Output for the above code will be
Enter a number:
10
Enter another number:
12
Total is 22
Explanation for the above code
Line 1: Adding a comment
Line 2: Printing Enter a number on the monitor
Line 3: Requesting user to enter a number and assigning that value to a. Here we are using input() command and interpreter will understand that it must wait for user to enter something before proceeding to execute the next line.
In the same line we are using int. This is done because the input will take user input as raw unassigned value. So we need to first convert it to Integer so we use as int(input()).Hope that understands.
After converting the value to integer we assign it to a
Line 4: Again Printing Enter another number on the monitor
Line 5: Doing the same step which line 3 does but the new value will be stored in b
Line 6: adding a and b and storing the result in c
Line 7: Displaying the Result.
We will be learning more about input in later episodes.
The above code can be written even like this:
# Accept user input values and add them print(“Enter a number: “) a = input() print(“Enter another number: “) b = input() c = int(a) + int(b) print(“Total is “,c)
Give a try
Try to add two numbers in experimenting in other ways.
Operators
Mathematics is very important in programming we keep on using these operators to perform some job. We already used two operators i.e. + and =
Here are some of the operators, meanings and how they are used.
| Operator | Algebraic Expression | Python Expression |
| + | a + b | a + b |
| - | A – B | A – B |
| * | A * B | A * B |
| / | A/B | A/B |
| ** (Exponent) | A^b | A**B |
| % | A mod B | A % b |
Give a try
Try to use the above operators for the above code and see the output.
This is the end of part 2. Soon will be posting part 3 of the “Python Days” episodes.
Subscribe to our feeds or email newsletter for notification of new Python Days episodes.
Will Bookmark it for now, then learn frm this..thanks !!