Module 1 Python Basics Flashcards
- Types - Expressions and Variables - String Operations
What scientific computing libraries does Python have?
Pandas, NumPy, SciPy, and Matplotlib
What scientific AI libraries does Python have?
Keras, Scikit-learn, Tensorflow, Pytorch
Python can be used for Natural Language Processing (NLP), using the Natural Language Toolkit (NLTK) Yes or No?
Yes
What is an Int?
A whole number (no decimals)
What is a float?
A real number (Anything between integers ex: 0.8 is between 0 and 1)
Changing Expression Types (Type Casting)
- You can convert (cast) an int to a float
- If the string contains an integer value you can convert it to an integer
- If we convert a string that contains a non-integer to an integer value, we get an error
- You can convert an int to a string
- You can convert a float to a string
Data Types: Boolean
- Booleans can be True and False (remember to use uppercase T and F)
- Using the type command on a Boolean value we obtain the term bool (example: type(True): bool
- If we cast a Boolean True to an interger or float we obtain the number 1
- If we cast a Boolean False to an integer or float we obtain the number 0
- If we cast a 1 to a boolean bool(1)we get a true
- If we cast a 0 to a boolean bool(0) we get a false
What is the type of the result of 6/2?
Float
What is the type of the result of 6//2?
Int
- The double slashes stand for integer division
Expressions and Variables
.
Expressions
Expressions describe a type of operation that computers perform.
Expressions are also…..
Operations that python performs
Operands
Numbers in a mathematical equation
What are the math symbols called?
operators
Variables
- Can be used to store values
- Ex: x = 1
- The colon denotes the value of the variable
So now x:1 will give you 1
- We can assign a new value to the colon using the same assignment operator. The old value will be overwritten with the new value
Variables (Part 2)
We can also store the results of expressions
- Ex: x = 43 + 60 + 16 + 41
- x will now store the sum of that expression
- x: (which will equal 160)
- To see the value of x in a Notebook, we can simply place it on the last line of a cell (simply type your variable (x) in any empty cell to see its value
Strings Operations
.
String
- Can be spaces or digits
- Can also be special characters
- We can bind or assign a string to another variable
- String are an ordered sequence (Ex: Michael, M =0, i=1, c=2, h=3, a=4, e=5, l=6)
- The index can be accessed for each letter with this command (Ex: variable_name[#]:
You can also used negative indexing with strings. The last index will be given -1
Strings:
Name = Michael Jackson
- Think of strings as a list or index
- You can bind a string to another variable
- Ex: Name[0:4] = Mich (Every value up to index 3)
- Ex: Name[8-12] = Jack (Every value starting at index 8 up to index 11)
Strings,Slicing, and Stride
Name[::2]: “McalJcsn”
- the 2 indicates we select every second variable
- Incorporating slicing will look like this
- Ex: Name[0:5:2]: “Mca”
- you are returning to every second value up to index four
- Ex: Michael…….. Name[0:5:2]: Mca
Strings,Slicing, and Stride (Continued)
- You can use the len command to obtain the length of the string
- Ex: len(“Michael Jackson”) Answer: 15
- You can combine strings with the addition symbols
- Name = (“Michael Jackson”)
- Statement = Name + (“is the best”)
- Statement = (“Michael Jackson is the best”)
You can replicate values of a string by multiplying it by the number of times you would like to replicate it.
Ex: 4*(“Michael Jackson”) = (“Michael Jackson Michael Jackson Michael Jackson Michael Jackson”)
- You cannot change the value of the string but you can create a new string
Strings are Immutable
- \ represents the beginning of escape sequences
- Escape sequences represent strings that may be difficult to input.
- For example \n represents a new line
- Ex: print(“Michael Jackson”\n is the best”)
This will be the output Michael Jackson (and then “is the best”) will be put on the new line - \t represents a tab
- If you actually want to put a backslash in your string do \ to put in a singular backslash in your string
- Ex: print(“Michael Jackson \ is the best”)
- Ex: Michael Jackson \ is the best
OR - You could place an “r” in front of the string
- Ex: print(r”Michael Jackson is the best”)
- Michael Jackson \ is the best
String Methods
One is converting lowercase characters to uppercase characters
A = “Thriller is the six studio album”
B =A.upper()
B:”THRILLER IS THE SIX STUDIO ALBUM”
A=’Michael Jackson is the best’
B=A.replace(‘Michael,’ ‘Janet’)
B: ‘Janet Jackson is the best’
Name = Michael Jackson
Name.find(‘el’): 5
Name.find(‘Jack’):8
————————————————————————-
- If the substring is not in the string, the output is -1.
String interpolation (f-strings)
Introduced in Python 3.6, f-strings are a new way to format strings in Python. They are prefixed with ‘f’ and use curly braces {} to enclose the variables that will be formatted. For example:
1 name = “John”
2 age = 30
3 print(f”My name is {name} and I am {age} years old.”)
- This will output as —-> My name is John and I am 30 years old.
F-strings are also able to evaluate expressions inside the curly braces, which can be very handy. For example:
1 x = 10
2 y = 20
3 print(f”The sum of x and y is {x+y}.”)
- This will be the output —-> The sum of x and y is 30.
Raw String (r’’)
- In Python, raw strings are a powerful tool for handling textual data, especially when dealing with escape characters. By prefixing a string literal with the letter ‘r’, Python treats the string as raw, meaning it interprets backslashes as literal characters rather than escape sequences.
Raw String (r’’) (Continued)
Regular string:
1 regular_string = “C:\new_folder\file.txt”
2 print(“Regular String:”, regular_string)
This will ouput
1 Regular String: C:
2 ew_folderile.txt
In the regular string regular_string variable, the backslashes (\n) are interpreted as escape sequences. Therefore, \n represents a newline character, which would lead to an incorrect file path representation.
Raw string:
1 raw_string = r”C:\new_folder\file.txt”
2 print(“Raw String:”, raw_string)
This will output
Raw String: C:\new_folder\file.txt
However, in the raw string raw_string, the backslashes are treated as literal characters. This means that \n is not interpreted as a newline character, but rather as two separate characters, ‘’ and ‘n’. Consequently, the file path is represented exactly as it appears.
Seperator
- This is the delimiter at which the string will be split. If not provided, the default separator is any whitespace.
Maxsplit
- This specifies the maximum number of splits to perform. If not provided, there is no limit on the number of splits.
Split
Syntax
string.split(separator, maxsplit)
RegEx
- RegEx (short for Regular Expression) is a tool for matching and handling strings.