<jupyter_start><jupyter_text>Python Cheat Sheet & PracticeThis notebook is a quick primer to the amazing world of Python. It's not meantto be exhaustive, but is meant to cover many of the basic learnings that practitioners of the language find useful.The recommended audience for this notebook are beginners.  Data types and operatorsTry typing out the instructions below, line by linein your own notebook<jupyter_code># Python can be used as a calculator
# The following line adds two numbers

5 + 7


# We can use the "type()" method to gauge the type of the parameters passed

type(5+7)


# This is also a very intuitive way to understand functions and arguments
# You "Pass" arguments to functions as we have done above
# Basic Subtraction in Python

4.5 - 5.5

type(7.1 - 2.1)
# What happens when you add two strings ?

"Hello" + " world"
# And what type is this addition of?

type("Hello" + " world")
# Storing values in a variable

my_string = "Hello" + " world"
my_int = 10
my_float = 7.42

print("The value of my_string is: " , my_string) #Notice that you can combine sentences and variables
print("The value of my_int is: ", my_int)
print("The value of my_float is: ", my_float)<jupyter_output>The value of my_string is:  Hello world
The value of my_int is:  10
The value of my_float is:  7.42<jupyter_text>Exercises - 11. Make a shopping list of 5 items. You could choose to type them out as a string.2. Use python as a calculator to find their total. The prices of the five items are: 9.42, 5.67, 8.21, 3.78 and 6.243. Use the len() function to determine the length of your shopping list string.4. Pick your favorite snack. Print it out using the print() function.5. Try multiplying strings and numbers. Multiply the name of your favorite snack by 10 and store it in a different variable.<jupyter_code>#Solutions to Exercise 1:

shopping_list = "Bread, Eggs, Jam, Milk, Honey"

list_total = 9.42 + 5.67 + 8.21 + 3.78 + 6.24

len(shopping_list)

print("Doritos ")

mult_str = "Doritos " * 10


print("My Shopping list is: ", shopping_list, "\nWith the total amount: ", 
      list_total, "\nThe length of the shopping list is: ", len(shopping_list),
      "\nThe favorite snack 10 times is: ", mult_str)<jupyter_output>Doritos 
My Shopping list is:  Bread, Eggs, Jam, Milk, Honey 
 With the total amount:  33.32 
 The length of the shopping list is:  29 
 The favorite snack 10 times is:  Doritos Doritos Doritos Doritos Doritos Doritos Doritos Doritos Doritos Doritos<jupyter_text>Variables, Types and FunctionsWe've already seen a few functions thus far including the "print()" function.Lets look at a few more:<jupyter_code># First store a value in a variable as we've seen above:

x = 5


# Now, check if x is an integer

isinstance(x, int)

# This function takes TWO arguments. The first one is the value we want to check
# and the second argument is the type we want to check against.
# The result is a "True" or a "False" -> Also known as a "Boolean"

# Check if x is of a type we pass:

print(isinstance(x, type(8)))

print(isinstance(x, type('a')))
# Now, lets change x to another type. Can we do this?

x =  "Hello"

# And then check if its type is an int:

print(isinstance(x,int))

# We see that this is not just possible but deceivingly simple.
# This is why python is known as a Language with "Dynamic typing", ie, what the
# value of the variable is does not need to always be a single type and can be 
# changed.
# Converting from one type to another:

a = "3.1234" # This is a string. Use the isinstance() function to confirm for yourself.

b = float(a) # Most types can be "Cast to"

# Now check the type of variable b

type(b)
# Now try converting a type we know cannot be expressed as a float:

float(x) #Why does this fail ?
# More conversions:

c = str(b)

print(c)

i = int(b)

print(i)
# Sometimes all these variables might get confusing and you would like to know 
# all the variables you have defined in your environment.
# The dir() function is useful here. Try it:

dir()
# In addition to the variables we have defined, there are what look like some "Inbuilt"
# values available in our environment
# We can also assign variables with some fancy shortcuts

a = b = c = 0

x,y = 1,2

print(a,b,c)
print(x,y)
# You may also swap variable values easily:

x,y = y,x

print(x,y)
# Lists! 

# These are a cool data type and are the topic of more advanced detail for later
# but in short, they are "Containers" that can contain a collection of values
# such as what we attribute to a shopping list in our minds :

shoppingList = ["Bread", "Eggs", "Jam", "Milk", "Honey"]

print(shoppingList)
# Lists are "Indexed" and can be addressed by these "Indices" which begin at 0:

print(shoppingList[0]) # Print the first element

print (shoppingList[3]) # Print out the 4th element
# But careful now, trying to access an index of a list that doesn't exist will error out:

print(shoppingList[100])
# You can check if an item exists in a list using the "item in list" pattern:

"Eggs" in shoppingList
# But, careful now, these are case sensitive:

"eggs" in shoppingList
# . . .annnd must be an exact match. No spaces, or other foreign characters:

"Eggs " in shoppingList
# The len() function returns a length for lists as well. Notice the difference 
# between this and the len() function applied on a string?

len(shoppingList)<jupyter_output><empty_output><jupyter_text>Exercises - Part 21. Re create your shopping list now, as a list.2. Think of another snack, check if it is in your shopping list using the "item in list" pattern.3. Print each element of your shopping list on a different line using what we've learned thus far.4. Quick assign two new variables "first" and "second" with the values of the first two elements of the shopping list.5. Quick Swap the values of these two new variables.<jupyter_code># Solutions to Part 2:

shoppingList = ["Bread", "Eggs", "Jam", "Milk", "Honey"]

print(shoppingList)

print("Doritos" in shoppingList)

print(shoppingList[0], "\n", shoppingList[1], "\n", shoppingList[2], "\n", shoppingList[3], "\n", shoppingList[4], "\n")

first,second = shoppingList[0], shoppingList[1]

print(first, second)

first, second = second, first

print(first, second)<jupyter_output>['Bread', 'Eggs', 'Jam', 'Milk', 'Honey']
False
Bread 
 Eggs 
 Jam 
 Milk 
 Honey 

Bread Eggs
Eggs Bread<jupyter_text>FunctionsPython hasa lot of built in functions and more can be imported from loadable libraries.Lets look at a few of them:<jupyter_code>min(2,3,4,5,6) #Return the minimum value of the arguments

max(2349, 208320, 238923, 239823) # Similarly, returns the maximum value of the arguments

#Print them out separately. Try playing arounf with them.
# Return the absolute value of the argument:

abs(-1)


# Round out decimal places

round(1.7)
# We've seen length already:

len("12345")
# These are mainly "Inbuilt" functions with Python. ie, the functions that some
# smart people somewhere wrote and decided to ship out with the basic Python installations.

# Similarly, you can import other modules. Lets take a look at one such example:

import os
# This loads the os module. A module is simply a file with statements and definitions
# of functions written into it.

# The os module contains functions dealing with the Operating System on the computer that Python is running on.

# To see everything defined in the os module, use the following statement:

dir(os)
# Now, we can use the functions defined in the os module like so:
#Remember, we are running this on a Google Server in this notebook.

os.name 
# List all files in the directory
os.listdir()
# Instead of importing and ENTIRE module, you may choose to only import specific functions

from os import name, listdir<jupyter_output><empty_output><jupyter_text>Making your own functionsFunctions (in Python) are really just special variables (or data types) that can have input and output. Obce defined, you can treat them like any other variables.Functions are defined with a specific syntax:*   Start with the keyword "def"*   followed by the function name, and*   a list of arguments enclosed in "()", then*   the line ends with a ":", and*   the body of the function is indented on the following linesPython uses white space to determine blocks.To have output from the funciton, the "return" keyword is used, followd by the thing to be returned. For no output, use "return" by itself or just leave it out.<jupyter_code># Your first function:

def first_func(x):
  return x*2

# Running this cell returns no output as intended.
# All we're doing here is declaring to the python interpreter that there is now a 
# new function with the name "first_func" and with the functionality declared.
# Now, this function can be used like all the others we have used thus far:

first_func(10)
# Try passing a string. How would we handle the case where we only want this function
# to work with numbers?

first_func("hello")
# Lets try multiple arguments:

def add(a,b):
  return a+b
# Now call the function:

add(2,3)
# Similarly, we can use our imagination to declare any kind of function:

def multiply(a,b):
  return a*b
# And once again, call the function by passing in arguments:

multiply(4,5)
# You can give arguments default values, this makes them optional:

def f(a,b, c=1):
  return (a+b) + c
# Calling a function and explicitly assigning a value to something that already
# has one, "overrides" that value:

f(1,2,3)
# You can also call the arguments by name:
f(c=5, a=10, b=5)<jupyter_output><empty_output><jupyter_text>Exercises Part 31. Write a function called "multiply_snacks()" that takes a snack name as the input and prints it 100 times2. Try modifying your function to print the snack 100 times with a space in between the words if you haven't done this already.<jupyter_code># Solutions to Part 3 

def multiply_snacks(snack_name):
  return snack_name*100

def multiply_snacks_with_space(snack_name):
  return (snack_name + ' ')*100
multiply_snacks_with_space("Doritos")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.07807358659886329, 'fraction_numerical': 0.017050553395154055, 'mean_word_length': 4.360769641902726, 'pattern_counts': {'":': 1, '<': 26, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '357509', 'license': 'Apache-2.0', 'n_tokens_mistral': 3134, 'n_tokens_neox': 2963, 'n_words': 1484, 'path': 'Python_Beginners_Cheat_Sheet_and_practice.ipynb', 'repo_name': 'ak-sparkline/Tutorials-and-cheatsheets', 'size': '10029'}