2

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.

3
  • 3
    What results are you expecting? It looks like the book expects you to type that into an interpreter, not run a script. Commented Sep 22, 2015 at 18:43
  • I'm trying to run this code into NotePad++ then save that file as a python script then drag and drop it into the command prompt but it's still not working through NotePad++ Commented Sep 22, 2015 at 19:38
  • Why did you expect that it would? Does the book not have instructions for how to run the examples? Commented Sep 22, 2015 at 19:39

2 Answers 2

4

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
Sign up to request clarification or add additional context in comments.

2 Comments

Very well written. Kudos.
Thank you but how do I make this script run in command prompt? It ends up back to C:\python27
4

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.

6 Comments

One small nitpick; the slice operation (ls[2:4]) does not split the list. It returns a new list containing the elements from ls with indices 2 up to but not including 4.
Hi Andrew - thanks but what I was trying to do was write up that code in NotePad++ and then do the print function. I don't know either what the tp and st is? I believe Tp is for Tuple and st is for string but if I want to define and make: print(tp[2] + st[2]) how do I make this output in command prompt? First I want to put this in NotePad++ then save it as python and run it in command prompt....
@Dilshad1 It is difficult to say what is going wrong as there are a number of options. 1. You are using tp and st as variables/lists, so if they have not be created yet it will not work. I suspect this may be the case if you cant tell me what they are. 2. There could be issued with data types mismatching. If you can give more information like a complete set of code and error messages I'd be happy to have a look.
tp and st aren't pre-defined python keywords, so you won't be able to do anything with them unless you define them. You'd have to start by going back to the example in the book that defines these terms. But the very first thing to do is to read all these answers and learn the difference between typing code into the interpreter, to be executed at once, and saving code in a script to run later.
you are correct - the tp and st are not even defined in my NotePad++ how can I do that? This is what I have in total of coding in NotePad++: ls = ["Mon","Tue","Wed","Thu","Fri","Sat","Sun"] a = ls[2:4] print(a)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.