Reputation: 29
I am using the book "Learn Python In A Day" by Acodemy. It seems this book has several typos (though I think so). I have installed Selenium IDE, Python 2.7.10 and NotePad++ on my Windows 7 64-bit. So from the book I was seeing this code:
ls = ["Mon","Tue","Wed","Thu","Fri","Sat","Sun"]
ls[2:4]
So, I typed only that code into NotePad++ saved it as an .py extension for python script/programming. I drag and drop this script into a command prompt window by changing the directory to C:\Python27 and I don't get any results.
What is it that I am doing wrong?
Also sometimes I try this: print(tp[2] + st[2])
this doesn't work either.
This is what I got in command prompt:
C:\python27
that is all even when I drag and dropped my python script into the command prompt.
Upvotes: 2
Views: 102
Reputation: 41
It is important when starting with Python to understand the difference between the interpreter and running saved code. The interpreter will help you out by printing the result of statements, but when saving code into a file it will only print if you tell it it explicitly.
Therefore if you have the line of code you mentioned:
ls = ["Mon","Tue","Wed","Thu","Fri","Sat","Sun"]
and then followed by:
ls[2:4]
You are simply saying split the list between index points 2 and 4.
The interpreter will be kind and realise you want to see the result and print it. However a saved file will not do what you ask, split the list and then leave it in memory never to be seen.
You are correct that using the following code in a file will work:
print(ls[2:4])
Try adding a couple of simple lists like below:
tp = ["Morning ", "Afternoon ", "Hello "]
st = ["Matthew","David","Andrew"]
print(tp[2] + st[2])
This will give you a working result, using the +
symbol to concatenate the Strings. Keep in mind the + symbol will concatenate Strings or add integers, but you will not be able to mix the two data types.
Upvotes: 4
Reputation: 24164
To run the python interpreter, go to the command prompt, type c:\py
, press tab until it says c:\python27
type \py
press tab again until it says c:\python27\python.exe
, then press return.
You will be faced with the python interactive prompt:
>>>
You can type expressions into the prompt, and it evaluates them and prints a result, if any:
>>> 2 + 5
7
From your example:
>>> ls = ["Mon","Tue","Wed","Thu","Fri","Sat","Sun"]
>>> ls[2:4]
['Wed', 'Thu']
If you want to run a script in the command prompt, instead of using the interactive python prompt, hold shift and right click on the directory with the script, let's call it script.py. Choose 'Open command window here'. A prompt will open. Type:
c:\python27\python.exe script.py
Upvotes: 4