python script code example

Example 1: run python file using python code

exec(open('file.py').read())

Example 2: how to run a python script

# Save the script in a file with .py extension
# run the script using the below command
python fileName.py

Example 3: python script example

# Import the modules
import sys
import random

ans = True

while ans:
    question = raw_input("Ask the magic 8 ball a question: (press enter to quit) ")
    
    answers = random.randint(1,8)
    
    if question == "":
        sys.exit()
    
    elif answers == 1:
        print "It is certain"
    
    elif answers == 2:
        print "Outlook good"
    
    elif answers == 3:
        print "You may rely on it"
    
    elif answers == 4:
        print "Ask again later"
    
    elif answers == 5:
        print "Concentrate and ask again"
    
    elif answers == 6:
        print "Reply hazy, try again"
    
    elif answers == 7:
        print "My reply is no"
    
    elif answers == 8:
        print "My sources say no"

Example 4: python

Python is an interpreted,
high-level and general-purpose
programming language.
Created by Guido van Rossum
and first released in 1991, Python's
design philosophy emphasizes code
readability with its notable use of
significant whitespace. Wikipedia

Example 5: how to run python in terminal

#For Python 3:
$ python3 helloworld.py
#For Python 2:
$ python helloworld.py

Example 6: run python program from command line

# press [windows] + [R] (on windows only)
# use the command : cd/the_way_of_your_program
# and now use this command :

py the_name_of_your_program.py 

# don't forget to write the extension (py for python) 
# at the end of your program name