<jupyter_start><jupyter_text>StringsA string is a series of characters, they are mostly used to display text.To define a string simply type text between quotes. Python accepts single, double and triple quotes. String output/inputTo output text (string) to the screen:<jupyter_code>s = "hello world"
print(s)<jupyter_output>hello world<jupyter_text>To get text from keyboard:<jupyter_code>name = input("Enter name: ")
print(name)<jupyter_output>Enter name: Vivek
Vivek<jupyter_text>If you use an old Python version (2.x), you need to use:<jupyter_code>name = raw_input("Enter name: ")
print(name)<jupyter_output><empty_output><jupyter_text>String Comparison- To test if two strings are equal use the equality operator (==)- To test if two strings are not equal use the inequality operator (!=)<jupyter_code>computer_name='Mac'
name = input("Guess computers name: ")
print(computer_name==name)<jupyter_output>Guess computers name: Lenovo
False<jupyter_text>Characters in stringThe 0th index is used for the first character of a string<jupyter_code>s = "Hello Python"
print(s)      # prints whole string
print(s[0])   # prints "H"
print(s[1]) # prints "e"<jupyter_output>Hello Python
H
e<jupyter_text>String Slicing`s[ startIndex : pastIndex ]`- startIndex is the start index of the string (0 or first character of string, by default)- pastIndex is one past the end of the slice (-1 or last character of string, by default)<jupyter_code>s = "Hello Python"
print(s[0:2]) # prints "He"
print(s[2:4]) # prints "ll"
print(s[6:])  # prints "Python"<jupyter_output>He
ll
Python<jupyter_text>Lists- Lists is a sequence and a basic data structure   - A list may contain strings (text) and numbers- A list is similar to an array in other programming languages, but has additional functionality [ ] bracketsWe define lists with brackets []. To access the data, these same brackets are used.<jupyter_code>l = [ "vivek", "claudio", "adrian", "richard" ]
print(l)     # prints all elements
print(l[0])  # print first element
print(l[1])  # prints second element
print(l.index('vivek')) # Return the index in the list of the first item whose value is x
print(l.count("vivek")) # Return the number of times x appears in the list<jupyter_output>['vivek', 'claudio', 'adrian', 'richard']
vivek
claudio
0
1<jupyter_text>Add/remove elements from a list<jupyter_code>l.append("alejandro")   # add element. Equivalent to a[len(a):] = [x]
print(l)                

l.insert(2, "alvaro")   # Insert an item at a given position
print(l)

l.remove("adrian")      # remove element
l.remove("vivek")       # remove element
print(l)                

l.pop(1)                # Remove the item at the given position in the list, and return it. 
print(l)                # If no index is specified, a.pop() removes and returns the last item in the list<jupyter_output>['vivek', 'claudio', 'adrian', 'richard', 'alejandro']
['vivek', 'claudio', 'alvaro', 'adrian', 'richard', 'alejandro']
['claudio', 'alvaro', 'richard', 'alejandro']
['claudio', 'richard', 'alejandro']<jupyter_text>Sort/reverse a list<jupyter_code>l.sort()    # sorts the list in alphabetical order
print(l)     # prints all elements
l.reverse() # reverse order.
print(l)     # prints all elements<jupyter_output>['alejandro', 'claudio', 'richard']
['richard', 'claudio', 'alejandro']<jupyter_text>List comprehension- A list comprehension consists of brackets containing an expression followed by a for clause, then zero or more for or if clauses- The result will be a new list resulting from evaluating the expression in the context of the for and if clauses which follow it- For example, assume we want to create a list of squares<jupyter_code># Traditional way:
s = []
for x in range(10):
    s.append(x**2)
print(s)

# List comprehension way:
s2 = [x**2 for x in range(10)]
print(s2)<jupyter_output>[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]<jupyter_text>Nested List Comprehensions- Lets see if we can use list comprehensions to transpose a matrix<jupyter_code>matrix = [
    [1, 2, 3, 4],
    [5, 6, 7, 8],
    [9, 10, 11, 12],
]

# Traditional way:
transposed = []
for i in range(4):
    transposed_row = []
    for row in matrix:
        transposed_row.append(row[i])
    transposed.append(transposed_row)

transposed
[[row[i] for row in matrix] for i in range(4)]<jupyter_output><empty_output><jupyter_text>DictionariesA dictionary can be thought of as an unordered set of key: value pairs { } brackets- A pair of braces creates an empty dictionary: {}- Each element can maps to a certain value- An integer or string can be used for the index- Dictonaries do not have an order<jupyter_code>d = {'hello': 'hola', 'bye': 'adios'}
d['no'] = 'no'
d
d['bye']
del d['no']
d['yes'] = 'si'
d
list(d.keys())
sorted(d.keys())
'hello' in d<jupyter_output><empty_output><jupyter_text>dict() constructor and Merging 2 dictionaries- builds dictionaries directly from sequences of key-value pairs<jupyter_code>d2 = dict([('and', 'y'), ('day', 'dia'), ('night', 'noche')])
d2
d3 = {**d , **d2}
d3<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12357339630066903, 'fraction_numerical': 0.015741833923652106, 'mean_word_length': 4.924242424242424, 'pattern_counts': {'":': 0, '<': 44, '<?xml version=': 0, '>': 44, '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': '13', 'dir': 'jupyter-structured-clean-dedup', 'id': '40466', 'license': 'MIT', 'n_tokens_mistral': 1780, 'n_tokens_neox': 1692, 'n_words': 604, 'path': 'Python-Programming/Teaching-Material/strings-lists-and-dict.ipynb', 'repo_name': 'vivekparasharr/Learn-Programming', 'size': '5082'}