Message From the Blogger:
Hello Friends ! before moving forward to the next chapter i would like to share some feedback about our last tutorial on the basics of Python. Below are some points which i consider for Improvment
1: i did not post about the environment setup of Python. Actually it
is very easy step thats why i did not include it. but still if any one
getting any problem in environment setup please refer to How to
2: Second Point is i am going to add another Page in the blog Latest
in Python. In this section you will get the latest information
regarding Python.
and again Thank you for your feedback, and looking for such suggestions in future.
---------------------------------------------------------------------------
Now lets Move to our next Chapter Python variables and Operaters.
Now lets Move to our next Chapter Python variables and Operaters.
What Is Variables?
Firstly i would like to ask what memory is? And i hope every body have the answers. Memory is just the space which can occupy something. Like right now you are sitting in a chair that means you are occupying some space. The same concept applies in Computer Science theory. Here in place of Space we call it Memory.
Now the Question is what is Variable and what is it use?
Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory.
Based on the data type of a variable, the interpreter allocates memory and decides what can be stored in the reserved memory. Therefore, by assigning different data types to variables, you can store integers, decimals or characters in these variables.
![]() |
| Memory allocation to a variable |
From the above figure you will understand better.
Assigning Values to Variables
Python variables do not have to be explicitly declared to reserve memory space. The declaration happens automatically when you assign a value to a variable. The equal sign (=) is used to assign values to variables.
The operand to the left of the = operator is the name of the variable and the operand to the right of the = operator is the value stored in the variable. For example:
here 100, 200.0 and String 'Neeraj' are assigned to the variables x, y, name respectively.
Multiple Assignment:
Python allows you to assign a single value to several variables simultaneously. For example:
a = b = c = 1
Here, an integer object is created with the value 1, and all three variables are assigned to the same memory location. You can also assign multiple objects to multiple variables. For example: a, b, c = 1, 2, "john"
Here, two integer objects with values 1 and 2 are assigned to variables a and b, and one string object with the value "john" is assigned to the variable c.
Standard Data Types:
The data stored in memory can be of many types. For example, a person's age is stored as a numeric value and his or her address is stored as alphanumeric characters. Python has various standard types that are used to define the operations possible on them and the storage method for each of them.
Python has five standard data types:
- Numbers
- String
- List
- Tuple
- Dictionary
Python Numbers:
Number data types store numeric values. They are immutable data types which means that changing the value of a number data type results in a newly allocated object.
Number objects are created when you assign a value to them. For example:
var1 = 1 var2 = 10
You can also delete the reference to a number object by using the del statement. The syntax of the del statement is:
del var1[,var2[,var3[....,varN]]]]
You can delete a single object or multiple objects by using the del statement. For example:
del var
del var_a, var_b
Python supports four different numerical types:
- int (signed integers)
- long (long integers (can also be represented in octal and hexadecimal))
- float (floating point real values)
- complex (complex numbers)
Examples:
Here are some examples of numbers:
| int | long | float | complex |
|---|---|---|---|
| 10 | 51924361L | 0.0 | 3.14j |
| 100 | -0x19323L | 15.20 | 45.j |
| -786 | 0122L | -21.9 | 9.322e-36j |
| 080 | 0xDEFABCECBDAECBFBAEl | 32.3+e18 | .876j |
| -0490 | 535633629843L | -90. | -.6545+0J |
| -0x260 | -052318172735L | -32.54e100 | 3e+26J |
| 0x69 | -4721885298529L | 70.2-E12 | 4.53e-7j |
- Python allows you to use a lowercase L with long, but it is recommended that you use only an uppercase L to avoid confusion with the number 1. Python displays long integers with an uppercase L.
- A complex number consists of an ordered pair of real floating-point numbers denoted by a + bj, where a is the real part and b is the imaginary part of the complex number.
Python Strings:
Strings in Python are identified as a contiguous set of characters in between quotation marks. Python allows for either pairs of single or double quotes. Subsets of strings can be taken using the slice operator ( [ ] and [ : ] ) with indexes starting at 0 in the beginning of the string and working their way from -1 at the end.
The plus ( + ) sign is the string concatenation operator and the asterisk ( * ) is the repetition operator. For example:
Python Lists:
Lists are the most versatile of Python's compound data types. A list contains items separated by commas and enclosed within square brackets ([]). To some extent, lists are similar to arrays in C. One difference between them is that all the items belonging to a list can be of different data type.
The values stored in a list can be accessed using the slice operator ( [ ] and [ : ] ) with indexes starting at 0 in the beginning of the list and working their way to end -1. The plus ( + ) sign is the list concatenation operator, and the asterisk ( * ) is the repetition operator. For example:
Data Type Conversion:
Sometimes, you may need to perform conversions between the built-in types. To convert between types, you simply use the type name as a function.
There are several built-in functions to perform conversion from one data type to another. These functions return a new object representing the converted value.
| Function | Description |
|---|---|
int(x [,base])
|
Converts x to an integer. base specifies the base if x is a string.
|
long(x [,base] )
|
Converts x to a long integer. base specifies the base if x is a string.
|
float(x)
|
Converts x to a floating-point number.
|
complex(real [,imag])
|
Creates a complex number.
|
str(x)
|
Converts object x to a string representation.
|
repr(x)
|
Converts object x to an expression string.
|
eval(str)
|
Evaluates a string and returns an object.
|
tuple(s)
|
Converts s to a tuple.
|
list(s)
|
Converts s to a list.
|
set(s)
|
Converts s to a set.
|
dict(d)
|
Creates a dictionary. d must be a sequence of (key,value) tuples.
|
frozenset(s)
|
Converts s to a frozen set.
|
chr(x)
|
Converts an integer to a character.
|
unichr(x)
|
Converts an integer to a Unicode character.
|
ord(x)
|
Converts a single character to its integer value.
|
hex(x)
|
Converts an integer to a hexadecimal string.
|
oct(x)
|
Converts an integer to an octal string.
|




Wooo! the way of presentation is amazing... keep it up (y)
ReplyDelete