## Python – Basic Data Types

In last blog post we learn about basic of python to familiar with the environment of python. Now we will learn python basic data types. One step ahead in process of learning python.

## Numerical Data Types:

Numbers in Python are of four types:
int: integer numbers (infinite)
float: corresponds to double in C
complex: complex numbers ( j is the imaginary unit)

Here are examples of each type. (I am using a python function type() to show the type of data type)

```>>> type(2)
<class 'int'>
>>> type(2.6)
<class 'float'>
>>> type(2+6j)
<class 'complex'>
>>> ```

### Integer:

In python, Int type refers to the integer, is a whole number, positive or negative, without decimals, of unlimited length.

```>>> print(1)
1
>>> print(1234235524565432+234231321)
1234235758796753
>>> ```

By default, python uses base 10 for numbers. You can choose another type of bases like octal, hexadecimal or binary

• 0b or 0B for Binary
• 0o or 0O for Octal
• 0x or 0X for Hexadecimal
```>>> print(0B0011)
3
>>> print(0b0011)
3
>>> print(0o0011)
9
>>> print(0O0011)
9
>>> print(0x0011)
17
>>> print(0X0011)
17
>>> ```

### Floating Point:

In python float indicate decimal number, is a number, positive or negative, containing one or more decimals. Optionally, the character `e` or `E` followed by a positive or negative integer may be appended to specify scientific notation. The E notation indicates powers of 10. In this case, 52.3E-4 means 52.3 * 10-4

```>>> print(1.)
1.0
>>> print(1.890)
1.89
>>> print(-2.3090)
-2.309
>>> print(1.3e235)
1.3e+235
>>> print(1.202392040240240240885)
1.20239204024
>>> print(.4)
0.4
>>> print(.4e4)
4000.0
>>>```

### Complex Number:

A complex number has two-part real and imaginary. It is written with a “j” as the imaginary part.
<real part>+<imaginary part>j

```>>> print(3+4j)
(3+4j)
>>> print(-3+5j)
(-3+5j)
>>> print(3.9+4j)
(3.9+4j)
>>> print(3+4.9j)
(3+4.9j)
>>> ```

### Operators on Numbers Basic:

Arithmetics: `+ , - , * , /` hint: Python 2 ⇒ `1/2 = 0` Python 3 ⇒ `1/2 = 0.5` Div and modulo operator: `// , % , divmod(x, y)` Absolute value: `abs(x)` Rounding: `round(x)` Conversion: `int(x) , float(x) , complex(re [, im=0])` Conjugate of a complex number: `x.conjugate()` Power: `x ** y , pow(x, y)`

`We will learn about operator other blog post`

## Strings:

A string is a sequence of characters. Strings are basically just a bunch of words. In python string type is know as str.

### Using Single Quotes (‘):

You can create a string in python using single quotes(‘) such as ‘This is a python string’.

```>>> type('This is python string')
<class 'str'>
>>> print('This is python string')
This is python string
>>> ```

There is no separate char data type in Python like C/C++.

String in python

### Using Double Quotes (“):

You can also create a string in python using single quotes(“) both are same, such as “This is a python string”.

```>>> type("This is python string")
<class 'str'>
>>> print("This is python string")
This is python string
>>>```

### Using Triple Quotes (”’ or “””):

If you want to write multi-line string then you can use triple quotes, such as ”’Hi, I want to learn python
because it is easy.”’
OR
“”” Hi, I want to learn python,
because it is easy. “””

```>>> type('''Hi, I want to learn python
... because it is easy.''')
<class 'str'>
>>> print('''Hi, I want to learn python
... because it is easy.''')
Hi, I want to learn python
because it is easy.
>>>```

You can use single quotes and double quotes freely within the triple quotes.

```>>> print("""Hi my name is 'singhak'
... What is your "good" name?""")
Hi my name is 'singhak'
>>> print("""Hi my name is 'singhak'
... What is your "good" name?""")
Hi my name is 'singhak'
>>>```

All the characters between the opening delimiter and matching closing delimiter are part of the string.

string in python

### Escape Sequences:

If you want to use a character that is not legal to a string then use escape character for this. An escape character () follow by that character that you want to insert.

Example 1: insertion of a single quote (‘)

```>>> print("What is your\'s name?")
What is your's name?
>>> print("What is your's name?")
What is your's name?
>>> print('What is your's name?')
File "<stdin>", line 1
print('What is your's name?')
^
SyntaxError: invalid syntax
>>> print('What is your\'s name?')
What is your's name?
>>>```

Sometimes you want some different characters like the new line character, tab or any special symbol (which is not legal to a string). In that case escape character () comes for the rescue.

Example 2: new line and tab in string

```>>> print("What is your status? and your profile")
>>> print("A tab is four space character like\t!.")
A tab is four space character like	!.```

A single backslash at the end of the line indicates that the string is continued in the next line, but no newline is added. For example.

```>>>> print("This is the first line. \
... This is second line. \
... But both will appear in single line.")
This is the first line. This is second line. But both will appear in single line.
>>>```

In many case we do not want to process any special character or any escape sequence then you need to use raw string by prefixing r or R to the string. An example :
r”No escape sequence will not work in raw string”

```>>> print(r"Here \n will nor work")
Here \n will nor work
>>> print(R"Tab \t will also not work")
Tab \t will also not work
>>>```

If you place two string literals side by side, they are automatically concatenated by Python. For example, ‘What\’s ‘ ‘your name?’ is automatically converted in to “What’s your name?”.

String in python
```>>> 'What\'s ' 'your name?'
>>>```

### String Methods

Count appearance of substrings: `s.count(sub [, start[, end]])` Begins/ends with a substring?: `s.startswith(sub[, start[, end]]) , s.endswith(sub[, start[, end]])` All capital/lowercase letters: `s.upper() , s.lower()` Remove whitespace: `s.strip([chars])` Split at substring: `s.split([sub [,maxsplit]])` Find position of substring: `s.index(sub[, start[, end]])` Replace a substring: `s.replace(old, new[, count])` More methods: `help(str) , dir(str)`

## Boolean Type:

Python also provide Boolean type that is False and True.

```>>> type(False)
<class 'bool'>
>>> type(True)
<class 'bool'>
>>> 1==1
True
>>> 1==2
False
>>>```

## Summary:

In this post you learnt different type of basic data types of python and how to use them. In next post we will learn variable declaration.