How to start a Python programmer III

How to assign the values to variables on python?
Python variables are assign explicit declaration to reserve memory space. The declaration is 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 side of the = operator is the name of the variable and the operand to the right side of the = operator is the value stored in the variable.
eg counter = 100 for integer value assign
In multiple value assigns single value to several variables simultaneously for example a=b=c=1
Which are Standard data types?
In the data stored in the memory for programming data types is needed for example in case of age use Number, in the case of name use string. detailed data types are
- Numbers
- String
- List
- Tuple
- Dictionary
Number
Number data types stores numeric values. Number objects are created when you assign a value to them.For example
Num = 1
Different types of data types are in python
- int (signed integers)
- long (long integers, they can also be represented in octal and hexadecimal)
- float (floating point real values)
- complex (complex 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 |
source is www.tutorialspoint.com
String
String will represent the characters quotation marks
str = ‘Hello World!’
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
Lists are similar to arrays in C.Lists is the versatile of Python’s compound data types. A list contains items separated by commas and enclosed within square brackets ([]). To some extent, 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. Example
list = [ 'xpdf', 123 , 1.12 'james', 80.2 ]
Tuples
A tuple is another data type it 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. Example
tuple = ( 'xpdf', 123 , 1.12 'james', 80.2 )
Dictionary
Python’s dictionary is the 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 ([]).Example
dict = {} dict[‘x’] = “This is x” dict[5] = “This is five”
What are Data Type Conversion of python
To convert between types, you simply use the type name as a function.There are several built functions to perform conversion from one data type to another. The functions return a new object representing the converted value.
Sr.No. | Function & Description |
---|---|
1 | int(a[,base])
It converts a to an integer. base specifies the base if x is a string. |
2 | long(a [,base] )
It converts a to a long integer. base specifies the base if a is a string. |
3 | float(a)
It converts x to a floating-point number. |
4 | complex(real [,imag])
It creates a complex number. |
5 | str(a)
It converts object x to a string representation. |
6 | repr(a)
It converts object x to an expression string. |
7 | eval(str)
To evaluates a string and returns an object. |
8 | tuple(s)
It converts s to a tuple. |
9 | list(s)
It converts s to a list. |
10 | set(s)
It converts s to a set. |
11 | dict(d)
It creates a dictionary. d must be a sequence of (key,value) tuples. |
12 | frozenset(s)
It converts s to a frozen set. |
13 | chr(a)
It converts an integer to a character. |
14 | unichr(a)
It converts an integer to a Unicode character. |
15 | ord(a)
It converts a single character to its integer value. |
16 | hex(a)
It converts an integer to a hexadecimal string. |
17 | oct(a)
It converts an integer to an octal string. |