Can Anyone Explain Me What This Python 3 Command Do

Can anyone explain me what this Python 3 command do?

input() looks like it gets the next line of input.
From the example this is the string "1 2 3 4 5\n" (it has a newline on the end).

rstrip() then removes whitespace at the right end of the input, including the newline.

split() with no arguments splits on whitespace, transforming the input to an iterable of strings. e.g. ['1', '2', '3', '4', '5']

map(int, sequence) applies int to each string. e.g. int('1') -> 1, int('2') -> 2 etc.. So your sequence of strings is now a sequence of integers.

Finally list(seq) transforms the sequence to a list type. So now you have [1,2,3,4,5].

can anyone explain me the background process of this code

1. input_string = input("Enter family members separated by space ")

This line request for a user to enter names, separate them by commas. The names are stored as a single string in "input_string"

2. family_list = input_string.split(" ")

This line will convert the names in the string into arrays by separating them by the use a comma.

3. print("\n")

This line prints an empty line.

4. print("Here is the list in alphabetical order:")
This line will just print the text in quotes inside the print function in a new line

5. for name in sorted (family_list): print(name)

This line will take the array names created at line 2, sort them alphabetically then print then line by line following that order.

Cannot understand specific Python 3 code

Basically, cmd is constructed by appending the command (say "insert"), to the operands. This cmd forms a correct python expression (for example l.insert(0,5), to insert 5 at index 0, in list l).

Here, l. is hardcoded(start of line 9), cmd is initialized in cmd = s[0], and operands are added in line 8.

eval(str) evaluates the command str, in string format, as if it were in a command line.

What is the difference between the python and python3 commands in unix-like systems?

The Python3 command was introduced because the python command pointed to python2. Since then, Python3 has become the default and thus python points to python3 on most but not all systems. So, most developers explicitly use python2 and python3 as to not run into issues on other systems.

can someone please explain me how to pass variables between 2 functions using python 3?

ok i just solved by myself:

from tkinter import *
def main(which):
def go(which):
b1=Button(w1,text='button 1',command=lambda:main('button 1'))
b2=Button(w1,text='button 2',command=lambda:main('button 2'))

What is the purpose of the -m switch?

The first line of the Rationale section of PEP 338 says:

Python 2.4 adds the command line switch -m to allow modules to be located using the Python module namespace for execution as scripts. The motivating examples were standard library modules such as pdb and profile, and the Python 2.4 implementation is fine for this limited purpose.

So you can specify any module in Python's search path this way, not just files in the current directory. You're correct that python args has exactly the same effect. The first line of the Scope of this proposal section states:

In Python 2.4, a module located using -m is executed just as if its filename had been provided on the command line.

With -m more is possible, like working with modules which are part of a package, etc. That's what the rest of PEP 338 is about. Read it for more info.

Related Topics

Leave a reply