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.
Python variables do not need explicit declaration 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. For example : name="AyaN"; roll=21; print(name); print(age);
Note : Here name is a string and age is a integer.
Python allows you to assign a single value to several variables simultaneously (Multiple Assignment). For example :a=b=c=1; print(a); print(b); print(c);
Or,a,b,c=1,2,"ayan"; print(a); print(b); print(c);
Note : Here you can see Two errors cause - 1st error (NameError) : when we add a string vale must add " " (Example: c="your string"). 2nd Error (SyntaxError) : must add ( , ) when adding multiple values (Example: a,b,c=1,2,3 or a,b,c=1,2,"your string")
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 data types that are used to define the operations possible on them and the storage method for each of them. Python standard data types : Numbers, String, List, Tuple, Sets, Dictionary.
Number data types store numeric values. Number objects are created when you assign a value to them. For example : a=2; b=3.8; c=10j;
Python supports four different numerical types :
int | float | complex |
---|---|---|
10 | 0.0 | 3.14j |
100 | 15.20 | 45.j |
-786 | -21.9 | 9.322e-36j |
080 | 32.3+e18 | .876j |
-0490 | -90. | -.6545+0J |
-0x260 | -32.54e100 | 3e+26J |
0x69 | 70.2-E12 | 4.53e-7j |
|
Strings in Python are identified as a contiguous set of characters represented in the 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.
|
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.
|
A tuple is another sequence data type that is similar to the list. A tuple consists of a number of values separated by commas. Unlike lists, however, tuples are enclosed within parentheses.
The main differences between lists and tuples are: Lists are enclosed in brackets ( [ ] ) and their elements and size can be changed, while tuples are enclosed in parentheses ( ( ) ) and cannot be updated. Tuples can be thought of as read-only lists.
|
A set is a collection which is unordered and unindexed. In Python sets are written with curly brackets. Sets are enclosed by curly braces ( { } ) .
|
Python's dictionaries are kind of hash table type. They work like associative arrays or hashes found in Perl and consist of key-value pairs. A dictionary key can be almost any Python type, but are usually numbers or strings. Values, on the other hand, can be any arbitrary Python object.
Dictionaries are enclosed by curly braces ( { } ) and values can be assigned and accessed using square braces ( [ ] ).
|
It's a special area where you can find special questions and answers for CSE students or IT professionals. Also, In this section, we try to explain a topic in a very deep way.