2

I am completely new to Python, just learning strings and variables at the moment. I know you can put a "#" before code to comment it, but is there an option for Python to completely ignore part of the code and start working from the lines after this code?

For example:

old_price = 30
new_price = 25
difference = old_price - new_price

name = "John Smith"
age = 15
print(name, age)

I would like the code to start working from the line name="John Smith", without having to comment the first 3 lines.

Scavenger23
  • 209
  • 1
  • 6
  • Do you need something like goto command in legacy languages : https://stackoverflow.com/questions/438844/is-there-a-label-goto-in-python – furkanayd Dec 05 '19 at 15:05
  • 1
    you should try use jupyter notebook or google colab – bcosta12 Dec 05 '19 at 15:05
  • You could put either of your codes into two functions and run one of them from the main thread. – Jason Chia Dec 05 '19 at 15:07
  • could you elaborate on why you would need such a thing? – Amir Afianian Dec 05 '19 at 15:10
  • I just want to make something like a cheat note with simple definitions and examples of how to execute them without having to open a new program for each new definition. When i comment the code it goes grey and it is hard to distinguish the actual comments from the commented code. – Scavenger23 Dec 05 '19 at 15:12

4 Answers4

8

You can use multiline strings to comment the whole block, not having to put # in each line:

"""
old_price = 30
new_price = 25
difference = old_price - new_price
"""
name = "John Smith"
age = 15
print(name, age)
Diego Palacios
  • 1,096
  • 6
  • 22
0

You could define a function an call it:

# define function old()
def old():
    old_price = 30
    new_price = 25
    difference = old_price - new_price
    print (difference)

# define function string()
def string():
    name = "John Smith"
    age = 15
    print (name, age) 

string() # out: John Smith 15
0

I don't believe that there is and I don't think that it's actually a good idea to do so. Why would you have code you don't want to execute?

If you don't want to execute it YET, you can put code into a function and not call it.

If you're just experimenting and want to play around around you should consider things like IPython or Jupyter Notebook where you can execute code interactively

sedavidw
  • 11,116
  • 13
  • 61
  • 95
  • Yeah I fully understand that it is not something desired, For now I just want to create something like a cheat note with basic definitions and few examples of how they are executed. The solution of course is to open a new file for each of the new things, but I thought I can do it all in one program. – Scavenger23 Dec 05 '19 at 15:10
  • Sounds like what you want is a Jupyter Notebook (https://jupyter.org/). You can try it in your browser at that site and run for yourself locally. Essentially it's a web view that you can make individual cells with python code and execute them as you see fit – sedavidw Dec 05 '19 at 17:05
0

Python is a structured programming language and therefore this type of things is discouraged and not supported internally (just like goto instructions). Moreover, usually the first lines of code would be import statements that would be needed later, so you don't really want to skip them.

You SHOULD use the benefits of a structured language and put your code into functions, then you can decide later if you want to execute those functions. Another solution is to use comments (or block comments) for code that you don't want to execute currently, or for other textual lines that don't contain code.


However, just for fun, here are two ways to skip lines.

Using -x flag

You can run your program with -x flag: python -x <python_file>. This will only skip the first line. This flag is used for allowing non-Unix forms of #!cmd (as describing when running python -h).

Using another script to run your script

Python has no internal support to run from a given line, so let's cheat. We can create a script file that its only purpose is to execute another file from a given line. In this example I implemented three ways to do so (note that the exec command is bad and you should not really use that).

import os
import subprocess
import sys
import tempfile

skip_to = int(sys.argv[1])
file_path = sys.argv[2]

with open(file_path, 'r') as sourcefile:
    new_source = '\n'.join(sourcefile.readlines()[skip_to:])

    # Several ways to execute:

    # 1. Create tempfile and use subprocess
    temp = tempfile.NamedTemporaryFile(mode='w+', delete=False)
    temp.write(new_source)
    temp.close()
    subprocess.call(['python', temp.name] + sys.argv[2:])
    os.remove(temp.name)

    # 2. Run from memory using subprocess and python -c
    subprocess.call(['python', '-c', new_source] + sys.argv[2:])

    # 3. Using exec (usually a very bad idea...)
    exec(new_source)

An example code to skip:

print("first line")
print("second line")
print("third line")
from datetime import datetime

print(datetime.now())
>>python skip.py 3 main.py
>>2019-12-05 20:50:50.759259
>>2019-12-05 20:50:50.795159
>>2019-12-05 20:50:50.800118
YAYAdest
  • 108
  • 8