pythondictionary

How to run a selective part of the Python script individually from .py file in IDLE


I have just started using Python, and, recently, I came up with an issue. When I am using a Python script for saving the code and running the code, right from the script, it always runs the whole code in the script. I would like to know if there is any other option, where I can run only a selective part of the code. Let's suppose that we have the following code with us, and, I want to run the code till printing the thisdict, which is located in the sixth line. But, when I try to run this code from the script in IDLE, it runs the whole code. So, please let me know if there is any other solution to run the selective code from the whole script.

thisdict={
"brand" : "Ford",
"model" : "Mustang",
"year" : 1964
}
print thisdict
#Tracing the model using the index/key named "model"
print "Model of the car:", thisdict.get("model")
#Changing the "year" to 2018 and re-printing the dictionary
thisdict["year"]=2018
print thisdict 
#Print both keys and values from the dictionar
for x,y in thisdict.items():
print x,y
#Add a key and value to the dictionary

thisdict["color"]="red"
print thisdict

#To delete a keyword from the dictionary
del thisdict["color"]
print thisdict
#OR
thisdict.pop("model")
print thisdict

 #To remove the last item from the dictionary
thisdict.popitem()
print thisdict

#Dist constructore for creating a dictionary
thisdict=dict(brand="Ford",model="Mustang", year=1964)
print thisdict

Solution

  • This is a weirdly common problem for beginner programmers in Python! There are a lot of fixes.

    The first an most obvious is to just comment out anything that you don't want to run. Just put a "#" before any line that you don't want to run, and it won't run. Most editors let you automatically comment out whole blocks at once.

    The second, and much better practice, way of doing this is to start using functions. So say you have this code:

    print("HERE 1")
    print("HERE 2")
    

    And sometimes you want to run both lines but sometimes you only want to run one of them. Then try putting them in different functions.

    def print_1():
        print("HERE 1")
    
    def print_2():
        print("HERE 2")
    

    Now, you just need to put in a command (with no indentation), calling the function that you want:

    print_1()
    

    You can put as much code as you like under a function. Also just FYI, this probably isn't important for you yet, but so that you know, the best way to call a function when a script is run directly is like this:

    if __name__=="__main__":
        print_1()
    

    But you can just write print_1() for now without the if name is main stuff. It will be relevant if you start importing the module from elsewhere.

    Then a third hack you can also use, and I still sometimes do this in complex situations where I don't know where control is and just want to kill a program, is this:

    import sys
    sys.exit(0)
    

    That will stop the program running whereever it is.

    Another option is to have a boolean at the top of your code.

    whole_program = True # or False 
    
    print("here 1")
    a = 1+3
    print("here 2")
    if whole_program:
        print("here 3")
        print("something else")
    else:
        pass
    

    Another option, if you are just writing scripts, is to use Jupter notebooks, which are very good at isolating sections of code to run.

    Oh, and of course, Python is an interpreted language. So you can always run one command at a time directly in the console!

    Ultimately, you should start learning to use functions, and then classes, because these are the base structures that you need to use to control the flow of a program.