<jupyter_start><jupyter_text>Data TypesPython has a number of built-in data types such as numbers (integers, floats, complex numbers), strings, lists, tuples, and dictionaries.Each of these can be manipulated using:1. Operators1. Functions1. Data-type methods  NumbersNumbers can be integers, floating points, Booleans, or complex numbers. The former three are the most important:+ Integers are whole numbers - 1, 2, 22, 476, -99999+ Floats have decimal points - 1.0, 2.22, 22.098, 476.1, -99999.9+ Booleans represent either True or False (or 1 or 0). They represent data that can only be one thing or another.<jupyter_code>num = 1
type(num)
num = 1.5
type(num)
b = True
type(b)<jupyter_output><empty_output><jupyter_text>Python can also handle complex numbers and its associated functions using the file “cmath”. Complex numbers have their uses in many applications related to mathematics and python provides useful tools to handle and manipulate them.An complex number is represented by **"x + yi"**. Python converts the real numbers x and y into complex using the function complex(x,y). The real part can be accessed using the function real() and imaginary part can be represented by imag().<jupyter_code>import cmath 
x = 5
y = 3
z = complex(x,y); 
print(z)
print ("The real part of complex number is : ",end="") 
print (z.real) 
print ("The imaginary part of complex number is : ",end="") 
print (z.imag) 
print ("The phase of complex number is : ",end="") 
print (cmath.phase(z))<jupyter_output>The phase of complex number is : 0.5404195002705842<jupyter_text>Float / int float() - given an integer, this function returns a float.int() - given a float, this function returns an integer. There is no rounding of the number, the function simply throws out anything after the decimal point (flooring) and returns the number.<jupyter_code>float(9)
float(-9)
int(10.6)
int(3.25)<jupyter_output><empty_output><jupyter_text>Is integer float.is_integer() - tests if a float is finite.<jupyter_code>(10.0).is_integer()
(10.1).is_integer()<jupyter_output><empty_output><jupyter_text>StringStrings are lines of text that are declared with single or double quotes<jupyter_code>name = 'John'
type(name)
word = 'abcdefgh'
len(word) # this function returns the length of string
word[0]
word[7]
word[9]<jupyter_output><empty_output><jupyter_text>Slicing Given a start and stop value, you can access a set of, or single, character(s) from the string.<jupyter_code>word[0:4] # print till 4th character
word[:4]
word[:-4]
word[-3:]<jupyter_output><empty_output><jupyter_text>FormatEasily format values into strings<jupyter_code>name = "<NAME>"
greeting = "My name is {}".format(name)
print(greeting)<jupyter_output>My name is <NAME><jupyter_text>StripReturns the string with the leading and trailing whitespace removed<jupyter_code>b  = "     Yes      "
print(b)
print(b.strip())<jupyter_output>Yes      
Yes<jupyter_text>Replace<jupyter_code>word.replace('f','g') # new string created
print(word) # old word stays as it is<jupyter_output>abcdefgh<jupyter_text>TupleTuples are similar to lists, but they are immutable - which means they cannot be changed after creation.<jupyter_code>weeks =('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday')
print(weeks) 
type(weeks)
weeks[0]
weeks[:3]
weeks[6] = 'Testing'
salaries = tuple()
print(salaries)
type(salaries)
first_tuple = (1, 2)
second_tuple = (3, 4)
print(first_tuple + second_tuple)
print(list(first_tuple))<jupyter_output>[1, 2]<jupyter_text>Because tuples are immutable, many of the list methods don’t work on tuples<jupyter_code>first_tuple.append(3)
first_tuple.pop(1)
first_tuple.sort()
first_tuple.reverse()<jupyter_output><empty_output><jupyter_text>ListLists, which are called Arrays in nearly every other programming language, group various types of data together.1. create_a_list = []1. numbers_list = [1, 2, 3]1. strings_list = ["spam", "eggs", "cheese"]1. mixed_list = ["Hello", [1, 2, 3], False]As you can see above, lists may contain any data type (you can mix and match, too), including other lists or nothing at all. You can access parts of lists just like strings with indexes.<jupyter_code>salaries = [] # create empty list
print(salaries) 
salaries = list() # create empty list
print(salaries) 
salaries = [8,10,15,20,16]
print(salaries) 
type(salaries) 
salaries[0] = 18
print(salaries)<jupyter_output>[18, 10, 15, 20, 16]<jupyter_text>Appendlist.append() - used to add an item to the end of a list<jupyter_code>salaries.append(28)
print(salaries)<jupyter_output>[18, 10, 15, 20, 16, 28]<jupyter_text>Sortlist.sort() - sort this list<jupyter_code>salaries.sort()
print(salaries)<jupyter_output>[10, 15, 16, 18, 20, 28]<jupyter_text>Pop list.pop() - given an index value, remove an element from the list<jupyter_code>salaries.pop(2)
print(salaries)<jupyter_output>[10, 15, 18, 20, 28]<jupyter_text>Mixed list<jupyter_code>mixed_list = [1,'a',1.5, [155,'b'], (12,13)]
print(mixed_list) 
mixed_list[4]
type(mixed_list[4])
mixed_list[3]
type(mixed_list[3])<jupyter_output><empty_output><jupyter_text>Slicing slice() - given a start and stop value, you can access a set of elements.<jupyter_code>numbers_list = [1, 2, 3, 200]
numbers_list[0]
numbers_list[1:2]
numbers_list[2:]
numbers_list[-1] # Negative indexes count from the last item backwards<jupyter_output><empty_output><jupyter_text>Concatenation<jupyter_code>fruits = ["apples", "grapes", "oranges"]
veggies = ["corn", "kale", "mushrooms"]
print(fruits + veggies)
print( 2 * fruits)<jupyter_output>['apples', 'grapes', 'oranges', 'apples', 'grapes', 'oranges']<jupyter_text>Length len() - given a list, this function returns the length of it<jupyter_code>len(fruits)<jupyter_output><empty_output><jupyter_text>Reverse<jupyter_code>fruits.reverse()
print(fruits)
### List Operation
List1 = ['Python', 'Py', 'Pyth', 'Python3'] 
List2 = List1*2 
List3 = List1[:] 
List2[0] = 'Hello' 
List3[1] = 'World' 
sum = 0 
print(List1,end=" ")
print(List2,end=" ")
print(List3,end=" ")
for ls in (List1, List2, List3):
    print(ls,end=" ")
for ls in (List1, List2, List3):
    print(ls[0]+" "+ls[1])
for ls in (List1, List2, List3): 
    if ls[0] == 'Hello': 
        sum += 1 
    if ls[1] == 'World': 
        sum += 2 
    print(sum, List2)
print(sum, List2)<jupyter_output>3 ['Hello', 'Py', 'Pyth', 'Python3', 'Python', 'Py', 'Pyth', 'Python3']<jupyter_text>DictionaryDictionaries are a type of associative array (an object) implemented using hash tables containing key/value pairs. They are unordered.<jupyter_code>salaries = {'John':15, 'Johnny':17, 'Jane':20}
print(salaries) 
type(salaries) 
print(salaries['John']) 
print(salaries['JohnTest']) 
print(salaries.get('JohnTest',16)) # Get JohnTest's salary. If its not there then get 16
testDict = dict()
print(testDict) 
type(testDict) 
testDict['John'] = 25
print(testDict)<jupyter_output>{'John': 25}<jupyter_text>SetSets are used to store multiple items in a single variable. A set is a collection which is both unordered and unindexed.<jupyter_code>s = set()
type(s)
thisset = {"apple", "banana", "cherry"}
print(thisset)
s.add(1)
s.add(2)
s.add(2) # Duplicate values will be ignored
print(s)
1 in s
print(len(s))<jupyter_output>2
{'fraction_non_alphanumeric': 0.13127252564457997, 'fraction_numerical': 0.033545883005267535, 'mean_word_length': 6.200598802395209, 'pattern_counts': {'":': 0, '<': 78, '<?xml version=': 0, '>': 78, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '22', 'dir': 'jupyter-structured-clean-dedup', 'id': '168434', 'license': 'MIT', 'n_tokens_mistral': 2707, 'n_tokens_neox': 2505, 'n_words': 753, 'path': 'code/3. Data Types.ipynb', 'repo_name': 'Ansu-John/Python', 'size': '7220'}