Values and Types
- A value is a basic think that a program work with, like a letter or a number. Eg: 1,2 and ‘hello world’
- The values belongs to different types. In python the standard data types available are:
- Numbers
- Strings
- List
- Tuples
- dictionary
Numbers
Numbers data type stores numerical values. It supports four numerical types.
- int (singed numbers like 10, -2)
- long (long intrgers represented in octal and hexadecimal)
- float (floating point like 3.14)
- Complex (comlex number like x = 4+5j )
String data type
- String are the sequence of characters represented with in quotation marks.
- It allows either pairs of single or double quotes. The substring access is possible through the slicing operator ([] or [:])
The string index 0 represents beginning of the string.
index -1 represent ending of the string.
Example:
str='Hello World'
| Code | Result | Definition |
|---|---|---|
| str | Hello World | #print complete string |
| str[0] | H | #print the first character of the string |
| str[-1] | d | #print the last character of the string |
| str[1:5] | ello | #print character starting from index 1 to 4 |
| str[2:] | llo World | #print string starting at index 2 till tha end of the string |
| str*2 | Hello WorldHello World | #print string repeatedly two times |
| str+" hai" | Hello World hai | #print concatenated string |
List data type
- List are the most significant compound data type contain element of various types
- A list can hold items of different data types it is enclosed by square brackets []. Where the items are separated by commas
- It is similar to string accessed using slice operator ([] or [:])
The sting index 0 represents the beginning of the list.
index 1 represents the ending of the list.
Example
List1 = ['abcd', 345, 3.2, 'python', 3.14]
List2 = [234, 'xyz']
| Code | Result | Definition |
|---|---|---|
| List1 | ['abcd', 345, 3.2, 'python', 3.14] | #print complete list |
| List1[0] | abcd | #print the first character of the list |
| List1[-1] | 3.14 | #print the last character of the List |
| List1[1:2] | [245, 3.2] | #print character starting from index 1 to 2 |
| List1[2:] | [3.2, 'python', 3.14] | #print string starting at index 2 till tha end of the list |
| List2*2 | [234, 'xyz', 234, 'xyz'] | #print list repeatedly two times |
| List1+List2 | ['abcd', 345, 3.2, 'python', 3.14, 234, 'xyz'] | #print concatenated lists |
Tuple data type
- Tuple is another sequence data type similar to list
- A tuple consists of number of values separated by cammas and enclosed with parentheses
- Tuple value cannot be updated.
Example
Tuple1 = ('abcd', 345, 3.2, 'python', 3.14)
Tuple2 = (234, 'xyz')
| Code | Result | Definition |
|---|---|---|
| Tuple1 | ('abcd', 345, 3.2, 'python', 3.14) | #print complete Tuple |
| Tuple1[0] | abcd | #print the first character of the Tuple |
| Tuple1[-1] | 3.14 | #print the last character of the Tuple |
| Tuple1[1:2] | (245, 3.2) | #print character starting from index 1 to 2 |
| Tuple1[2:] | (3.2, 'python', 3.14) | #print string starting at index 2 till tha end of the Tuple |
| Tuple2*2 | (234, 'xyz', 234, 'xyz') | #print Tuple repeatedly two times |
| Tuple1+Tuple2 | ('abcd', 345, 3.2, 'python', 3.14, 234, 'xyz') | #print concatenated Tuples |
Dictionary data type
- Dictionary data type is a kind of hash table.
- It contain key-value pairs almost any python type usually numbers or string.
- Dictionaries are enclosed by curly braces {} and values can be assigned and accessed using square brackets [].
Example
Dict1={'name':'abcd', 'code':9530, 'dept':'Mech'}
| Code | Result | Definition |
|---|---|---|
| Dict1 | {'name':'abcd', 'code':9530, 'dept':'Mech'} | #print complete dictionary |
| Dict1['name'] | abcd | #print the value for the key |
Boolean data type
- Boolean is one more datatype supported in python. It takes values: True or False.
Eg: print True
print False

No comments:
Post a Comment