Python Flashcards

1
Q

Why Python?

A
  1. Python is a mature language, created by Guido Van Rossum in 1991
  2. Lots of libraries and very useful in data science community
  3. Documentation is constantly updated
  4. Huge community
  5. Easy to learn with readable syntax
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Why version 3.6 and not 2.7?

A

Changes are very minimal. Support for 2.7 ends in 202, Most libraries are now written in 3.6. Improvements in print function, range function and division.

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

What is a web server?

A

A computer system that processes HTTP requests.

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

What is the HTTP response?

A

HTML, CSS, and Javascript

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

Where is python running?

A

On the server,. Python Django framework for example, will handle logic of what to do with each HTTP request and what to return in the HTTP response.

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

What is the HTTP request?

A

An HTTP client sends an HTTP request to a server in the form of a request message which includes the following format:

  1. A request-line
  2. zero or more header fields followed by CRLF
  3. An empty line indicating the end of the header fields
  4. Optional message body

Request-Line
The Request-Line begins with a method token, followed by the Request-URI and the protocol version, and ending with CRLF. The elements are separated by space SP characters.

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

How do we run Python code?

A

Within a python shell directly or save the code to a file that ends in .py file extension and tell python to run that file from our terminal.

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

What type of language is python?

A

An interpreted language, although it does compile our code into bytecode .pyc to be run in the python shell from time to time.

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

What is OOP?

A

stands for Object Oriented Programing, it is an important paradigm in which data and certain methods can be contained within objects.

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

What are the advantages of OOP?

A

Helps us DRY out our code.
Forces you to plan ahead which leads to higher quality code.
Don’t need to know how an object works exactly to use it.
If you need to change the code you can change the object itself and not hunt down every use of the object in your project.
Widely used in web design and game design.
Most frameworks use OOP.

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

How do we make (instantiate) an object?

A

create the variable and assign it to the class.

student1 = Student(“Janet”, “janet@gmail.com”)

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

How do we write a class?

A
Class Student:
    def \_\_init\_\_(self, name, email):
          self.name = name
          self. email = email
          self.numberofBelts = 0
     def addBelt(self):
           self.numberofBelts +=1
           return self
     def say(self, thing):
            print(self.name, "says", thing)
            return self
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is self?

A

self is whatever that object happens to be. Think of it as a placeholder for the names of the objects that we will be making.

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

What are the attributes in the class Student?

A

The attributes are the variables: self.name, self.email

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

What are the methods in the class Student?

A

addBelt() and say()

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

How do you apply and chain methods to an object?

A

student1.addBelt().addBelt()

17
Q

How can we illustrate inheritance?

A
Class Person:
    def \_\_init\_\_(self, name, email):
          self.name = name
          self. email = email
Class Student(Person):
    def \_\_init\_\_(self, name, email):
          super(Student, self).\_\_init\_\_(name, email)
          self.numberofBelts = 0
18
Q

What does super do?

A

It runs the __init__ method in the class Person which creates the self.name and self.email attributes.

19
Q

What are multi-arguments?

A
def testingArgs(*args):
     for arg in args:
           print(arg)
def testingKwargs(**kwargs):
     for kw in kwargs:
           print(kw)

Using the “splat operator” * we can turn all our arguments into a list of arguments. This way our function can receive any number of arguments. We can also use ** to create a dictionary of key/value pairs from our arguments. The arg returns a tuple.

20
Q

What is a virtual environment?

A

A clean python environment that is separate from the global python environment that can have just the version of python and libraries and scripts installed that are needed for the project.

21
Q

How do you install a virtual environment on Windows and Linux?

A

pip install virtualenv

virtualenv DjangoEnv

22
Q

What is a framework?

A

A standard structure for how to implement a software project. Contains both the tools needed to do repetitive tasks and they may generate required project files as well.

23
Q

What is a micro-framework?

A

Similar to a framework, but for a minimalistic web application. It’s typically less complex than a full stack framework. An example is Flask

24
Q

What are routes?

A

Routes are the part of the url that comes after our main url, anything after the slash.
eg. url = “http://www.myawesomesite.com/This_is_the_route”

25
Q

What is a static file?

A

A fiule that doesn’t change, in the context of our webapp the css, js, and images.

26
Q

When a user submits a form, where does it go?

A

It goes to the server.

27
Q

What is a GET request?

A

If a form is submitted with a method=”get” it will pass the form data through the url like “localhost:5000/process? name=example&email=example@email.com”. This is fine for a simple form like a search, but might be insecure if the form has a password or credit card number.

28
Q

What is a POST request?

A

If a form is submitted with a method=”POST” it does not pass the form data in the url and instead sends it as part of the HTTP request. We can access this data in django using request.POST.

29
Q

What is session?

A

Session is a variable we can store on the user’s web browser and we can access in our server or templates. We can set session and access session as if it were a dictionary.

30
Q

What’s the correlation between sessions and the stateless protocol?

A

All correspondence between web browsers (clients) and servers is achieved through the HTTP protocol. As we very briefly touched upon in Chapter 7, HTTP is a stateless protocol. This therefore means that a client computer running a web browser must establish a new network connection (a TCP connection) to the server each time a resource is requested (HTTP GET) or sent (HTTP POST) 1.

Without a persistent connection between client and server, the software on both ends cannot simply rely on connections alone to hold session state.

The most commonly used way of holding state is through the use of a session ID stored as a cookie on a client’s computer. A session ID can be considered as a token (a sequence of characters) to identify a unique session within a particular web application. Instead of storing all kinds of information as cookies on the client (such as usernames, names, passwords…), only the session ID is stored, which can then be mapped to a data structure on the web server. Within that data structure, you can store all of the information you require. This approach is a much more secure way to store information about users. This way, the information cannot be compromised by a insecure client or a connection which is being snooped.

31
Q

What is a hidden input?

A

An input tag that isn’t displayed in a form.

32
Q

Why would we need to validate form data?

A

To insure data that we might want to save into a database is correct or in the right format.

33
Q

How do we validate data?

A
Use lots of conditionals!
valid = True
if len(request.form["name"]) < 1:
     valid=false
34
Q

What is Regex?

A

A short name for Regular Expressions. It is a way we can find patterns in text or even search text.