Python How to's Flashcards

1
Q

How to take input from user ?

A

We have to use input function.

variable = input(“Enter Value >”)

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

How to take arguments from terminal ?

A
  1. Import optparse module
  2. Create optparse object.
  3. Teach optparse object for arguments.
  4. Capture arguments in variable using optparse.
    import optparse
    parser = optparse.OptionParser()
    parser.add_option(“-i”, “–interface”, dest = “interface”, help = “Network Interface “)
    (interface, arguments) = parser.parse_args()
    subprocess.call(“ifconfig”, interface.interface)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How to Execute Linux System Command ?

A
  1. import subprocess module
  2. execute system command with subrocess functions
    import subprocess
    subprocess.call(“ifconfig”,”eth0”)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How to handel regular expressions ?

A
  1. import re module
  2. execute re functions to search
    import re
    re.search(r’regular expression’,stringVariable,flag)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How many ways to present list with index value?

A

By Three Ways we can achieve this.

                 1. Using enumerate function
                  2. Using Zi[p Function
                    3. Usinge range function

For Code See Juypter Lab - Python Exercise for Practice

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

How to use subprocess Module.

A

import subprocess

subprocess.call(“ifconfig”,interface,”hw”,”ether”)

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

How to capture output of subprocess.

A

ifconfig_output = subprocess.check_output(“ifconfig”, interface,)

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

How many types to use subprocess module.

A

subprocess. call(“ifconfig eth0 down “,shell=True)
subprocess. call(“ifconfig “+ interface + “ down”,shell=True)
subprocess. call(“ifconfig”,”eth0”)
subprocess. call(“ifconfig”, interface)

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

Which Function is used with subprocess to execute system command.

A

subprocess.call()

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

Which Function is used with subprocess to read output of system command.

A

subprocess.check_output

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

Which class is used to create optparse object?

A

optparse.OptionParser()

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

Which function is used with optparse object to teach object ?

A

parser.add_options(“-i”, “–interface”, dest = “interface”,help=”Ethernet Interface”)

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

Which function is used with optparse object to process arguments ?

A

parser.parse_args()

interface,arguments)=parse.parse_args(

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

Which function is used with optparse object to handle error ?

A

parser.error()

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

Which function is used with optparse object to get arguments ?

A

parser.parse_args()

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

How to handel error in optparse ?

A

if not interface:

parser.error(“[-] Plz Specify interface or –help for more info”)

17
Q

How to generate number by random module

A

import random

number = random.randint(startRange,endRange)

18
Q

How to get a square root of a number

A

import math

sqrt = math.sqrt(number)

19
Q

How to get present working directory

A

os module

print(os.getcwd())

20
Q

How to import how to import PDF file

A

PyPDF2