Module 1 Python Basics Flashcards

- Types - Expressions and Variables - String Operations

You may prefer our related Brainscape-certified flashcards:
1
Q

What scientific computing libraries does Python have?

A

Pandas, NumPy, SciPy, and Matplotlib

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What scientific AI libraries does Python have?

A

Keras, Scikit-learn, Tensorflow, Pytorch

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Python can be used for Natural Language Processing (NLP), using the Natural Language Toolkit (NLTK) Yes or No?

A

Yes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is an Int?

A

A whole number (no decimals)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is a float?

A

A real number (Anything between integers ex: 0.8 is between 0 and 1)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Changing Expression Types (Type Casting)

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Data Types: Boolean

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the type of the result of 6/2?

A

Float

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the type of the result of 6//2?

A

Int

  • The double slashes stand for integer division
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Expressions and Variables

A

.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Expressions

A

Expressions describe a type of operation that computers perform.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Expressions are also…..

A

Operations that python performs

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Operands

A

Numbers in a mathematical equation

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What are the math symbols called?

A

operators

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Variables

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Variables (Part 2)

A

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
17
Q

Strings Operations

A

.

18
Q

String

A
  • 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
19
Q

Strings:

A

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)
20
Q

Strings,Slicing, and Stride

A

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

21
Q

Strings,Slicing, and Stride (Continued)

A
  • 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

22
Q

Strings are Immutable

A
  • \ 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
23
Q

String Methods

A

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.
24
Q

String interpolation (f-strings)

A

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.
25
Q

F-strings are also able to evaluate expressions inside the curly braces, which can be very handy. For example:

A

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.
26
Q

Raw String (r’’)

A
  • 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.
27
Q

Raw String (r’’) (Continued)

A

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.

28
Q

Seperator

A
  • This is the delimiter at which the string will be split. If not provided, the default separator is any whitespace.
29
Q

Maxsplit

A
  • This specifies the maximum number of splits to perform. If not provided, there is no limit on the number of splits.
30
Q

Split

A

Syntax

string.split(separator, maxsplit)

31
Q

RegEx

A
  • RegEx (short for Regular Expression) is a tool for matching and handling strings.