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 *
w1=Tk()
w2=Tk()
w2.withdraw()
def main(which):
w2.deiconify()
def go(which):
print(which)
b=Button(w2,text='print',command=lambda:go(which))
b.pack()
b1=Button(w1,text='button 1',command=lambda:main('button 1'))
b1.pack()
b2=Button(w1,text='button 2',command=lambda:main('button 2'))
b2.pack()
w1.mainloop()
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 mymod1.py mymod2.py 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
How to Add Parenthesis Around a Substring in a String
Removing Rows and Columns in Python CSV Module
How to Select Last Row and Also How to Access Pyspark Dataframe by Index
How to Name Dataframes Dynamically in Python
Pytest Cannot Import Module While Python Can
How to Run Python Script from Another Machine Without Installing Imported Modules
Getting All Possible Combinations from a List With Duplicate Elements
Simple Digit Recognition Ocr in Opencv-Python
Delete Rows Containing Numeric Values in Strings from Pandas Dataframe
Possible to Loop Through Excel Files With Differently Named Sheets, and Import into a List
How to Close a Tkinter Window by Pressing a Button
How to Loop Over Multiple Dataframes and Produce Multiple List
How to Remove Text Within Parentheses With a Regex
How to Fill a Column With the Value of Another Column Based on a Condition on Some Other Columns
How to Eliminate Null Valued Cells from a CSV Dataset Using Python
No Output Displays When Execute Python File
Keras Valueerror: Input 0 Is Incompatible With Layer Conv2D_1: Expected Ndim=4, Found Ndim=5