[Tutor] Python Programming Help
Hello, I am a beginner in computer programming. I am studying math, and the math class that I will be taking requires knowledge in Python. So, I am in a computer science class. Therefore, I do not have an in-depth knowledge of computer programming. I am currently trying to write a program in Python version 2.7.5 that uses the math module to compute sinh(x) = 1/2(e^x - e^(-x)) in three ways for a given value of x: 1a) by using the sinh function 1b) by using the exp function 1c) by using the value of e and the exponentiation operator ** 2. Print the results and note any differences that appear. So, I know that I have to create a NotePad file so that I can import that into my command prompt. In my NotePad file, I have the following...I'm not sure if I am even going about doing this problem correctly... def sinh(x): return (1/2)*(e^x - e^(-x)) def exp(x): return e**x I am stuck, and don't know where to go from here. I would appreciate help please. Thank you. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python Programming Help
On 2013-09-10 21:01, Katie wrote: In my NotePad file, I have the following...I'm not sure if I am even going about doing this problem correctly... I don't envy you having to use notepad. Consider using a more sane editor... you'll thank yourself for it. def sinh(x): return (1/2)*(e^x - e^(-x)) def exp(x): return e**x I am stuck, and don't know where to go from here. I would appreciate help please. You'd have done well to present the problem you're having, but you should know that ^ is XOR (as it is in most programming languages), you probably want ** (and space out your function definitions -- PEP8 says to use two blank lines between functions). pgp_uNjpYYMed.pgp Description: PGP signature ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python Programming Help
Hi Katie, On Wed, Sep 11, 2013 at 11:01 AM, Katie xrandomhear...@aim.com wrote: Hello, I am a beginner in computer programming. I am studying math, and the math class that I will be taking requires knowledge in Python. So, I am in a computer science class. Therefore, I do not have an in-depth knowledge of computer programming. Welcome to the forum and welcome to the world of programming! I am currently trying to write a program in Python version 2.7.5 that uses the math module to compute sinh(x) = 1/2(e^x - e^(-x)) in three ways for a given value of x: 1a) by using the sinh function 1b) by using the exp function 1c) by using the value of e and the exponentiation operator ** 2. Print the results and note any differences that appear. So, I know that I have to create a NotePad file so that I can import that into my command prompt. In my NotePad file, I have the following...I'm not sure if I am even going about doing this problem correctly... Okay, so when you are programming, there are two things you do to see the result of your programs: 1. First, you write the program. You write this in an text editor. Notepad is a text editor (There are better options, but let's keep it as it is for now) 2. Then, you run the program. Sometimes you can run the program from the editor itself, such as in IDLE. Other times, you have to run it separately. So, before you write the solution to the programming problem above, can you first try to write a program and then run it? What operating system are you working? If you are on Windows, can I suggest you to take a look at these videos I created a while back and they may help you: http://www.youtube.com/playlist?list=PLD0rs_vnIQS5r8IhNpu0rWIAyKpTX34yo Only when you have written your first program, which is really simple, you should try to attempt your exercise. Hope that helps. -Amit. -- http://echorand.me ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python Programming Help
On 11/09/13 02:01, Katie wrote: I am a beginner in computer programming. Hi welcome to the tutor list. I am currently trying to write a program in Python version 2.7.5 *that uses the math module to compute sinh(x) = 1/2(e^x - e^(-x)) in three ways for a given value of x:* *1a) by using the sinh function* *1b) by using the exp function* *1c) by using the value of e and the exponentiation operator So, I know that I have to create a NotePad file so that I can import that into my command prompt. You don;t need to import it into the command prompt although you may for that for testing purposes. But you can run Python programs directly by double clicking them in your file explorer. It will help if you tell us which version of Python you are using and which Operating System you use. (I'll assume Windows in this post since you mention Notepad) Finally, Notepad is fine for short text notes but it's not good for programming. You should get a proper programer's editor. IDLE comes with Python in most cases and if you are using Windows and download the ActiveState version of Python you will also get Pythonwin. Either of those will be better than Notepad for writing code. In my NotePad file, I have the following...I'm not sure if I am even going about doing this problem correctly... def sinh(x): return (1/2)*(e^x - e^(-x)) The ^ sign does not do what you think in Python. The operator you need is ** (or use the pow() function). Also e is not defined directly, it's in the math module so you need to import math and then reference it as math.e Alternatively, import e from math: from math import e,exp # import e and exp() from math And then use them directly: import math math.exp(3) 20.085536923187668 or from math import e,exp exp(3) 20.085536923187668 I am stuck, and don't know where to go from here. I would appreciate help please. You have defined your first version of sinh() you now need to fix and test it. You can either import it into your python session import sinh# assuming the file is called sinh.py sinh.sinh(5) # remember to include the module name as a prefix or You could call the function in your file and run it by clicking in explorer. To do that you should add to the end of your file: print( sinh(2) ) # use any test value you wish... input('Hit enter to quit') # pauses output so you can see it If you are comfortable using the OS command prompt you can also run it from there with C:\PATH\TO\YOUR\FILE python sinh.py This has the advantage that you will see any error messages too. Finally, if you use IDLE or Pythonwin you can run the program from inside those tools using the menus. At this stage that's probably the best option. You can get a video tutorial on using IDLE here: http://www.youtube.com/watch?v=bOvqYw1SZJg HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python Programming Help
On 11 September 2013 10:48, Amit Saha amitsaha...@gmail.com wrote: Hi Katie, So, before you write the solution to the programming problem above, can you first try to write a program and then run it? What operating system are you working? If you are on Windows, can I suggest you to take a look at these videos I created a while back and they may help you: http://www.youtube.com/playlist?list=PLD0rs_vnIQS5r8IhNpu0rWIAyKpTX34yo Good work Amit. I don't have any sound on this machine but I skimmed those videos and they seem really useful. I'll remember to point someone there in future. Do you know of anything similar for OSX? Oscar ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] [Re:] I need help with the following question
I'm resending this to the list. Please reply to the tutor list rather than directly to me. Also please don't top-post. My answer is below. On 11 September 2013 10:47, Thabile Rampa thabilera...@gmail.com wrote: On Tue, Sep 10, 2013 at 11:57 AM, Oscar Benjamin oscar.j.benja...@gmail.com wrote: On 10 September 2013 08:58, Thabile Rampa thabilera...@gmail.com wrote: On Aug 27, 2013, at 3:40 AM, isaac Eric wrote According to my understanding, the purpose of the %s is to turn the numbers, which the program has recognized as numbers, into strings, so that they fit in the print command without any syntax errors. You are correct. '%s' is used to convert numbers (or other non-string things) into strings so that they can be used in places where text is required. In the particular case of the print command, this is done automatically so printing a number directly works just fine: Wow! Thanks so much guy! The last two paragraphs especially made it a lot easier to understand! but why are there so many ways to achieve one goal in Python? The str() function is the default way to convert an object to a string. The print statement uses this implicitly if you try to print something that is not a string which is convenient for simple output. The % formatting codes allow for more advanced usage (see below) but %s is just for the specific case where you want to convert each object using str(). Here's how you can use % formatting to represent the same number in different ways: radius = 12.3456789 radius 12.3456789 print 'radius =', radius radius = 12.3456789 print 'radius = %s' % radius radius = 12.3456789 print 'radius = %.3f' % radius radius = 12.346 print 'radius = %.5f' % radius radius = 12.34568 print 'radius = %.5e' % radius radius = 1.23457e+01 There is also the .format method. This was initially intended to replace % formatting but it was ultimately decided that removing % formatting was not necessary. Consequently there are now two ways of doing advanced string formatting in Python. Oscar ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] [Re:] I need help with the following question
On Wed, Sep 11, 2013 at 11:15:26AM +0100, Oscar Benjamin wrote: There is also the .format method. This was initially intended to replace % formatting but it was ultimately decided that removing % formatting was not necessary. Consequently there are now two ways of doing advanced string formatting in Python. Three ways. People forget the string.Template class. py import string py template = string.Template(Hello $name) py template.substitute(name=Oscar) 'Hello Oscar' py template.safe_substitute(nmae=Oscar) 'Hello $name' -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python Programming Help
On Wed, Sep 11, 2013 at 7:59 PM, Oscar Benjamin oscar.j.benja...@gmail.com wrote: On 11 September 2013 10:48, Amit Saha amitsaha...@gmail.com wrote: Hi Katie, So, before you write the solution to the programming problem above, can you first try to write a program and then run it? What operating system are you working? If you are on Windows, can I suggest you to take a look at these videos I created a while back and they may help you: http://www.youtube.com/playlist?list=PLD0rs_vnIQS5r8IhNpu0rWIAyKpTX34yo Good work Amit. I don't have any sound on this machine but I skimmed those videos and they seem really useful. I'll remember to point someone there in future. Do you know of anything similar for OSX? Thanks, Oscar. I haven't really seen something similar for OSX, mainly because I have never used one. I searched now though on YouTube and there seems to be a few. -- http://echorand.me ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] class data member and objects of class in python
i am learning how a __class__ data member behaves in python as compared to static data member in java, but following code is throwing error class PizzaShop(): pizza_stock = 10 def get_pizza(self): while not PizzaShop.pizza_stock: PizzaShop.pizza_stock -= 1 yield take yours pizza order, total pizzas left {}.format(PizzaShop.pizza_stock) mypizza_shop = PizzaShop() pizza_order = mypizza_shop.get_pizza() # iterator is obtained print a pizza pls!! {}:.format(pizza_order.next()) print a pizza pls!! {}:.format(pizza_order.next()) output: Traceback (most recent call last): File /home/scott/pythonfiles/core_python/pizza.py, line 10, in module print a pizza pls!! {}:.format(pizza_order.next()) StopIteration don't know where i am doing mistakeany help will be appreciated... i have other questions on based on this class ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] class data member and objects of class in python
On Wed, Sep 11, 2013 at 5:40 AM, zubair alam zubair.alam@gmail.comwrote: i am learning how a __class__ data member behaves in python as compared to static data member in java, but following code is throwing error class PizzaShop(): pizza_stock = 10 def get_pizza(self): while not PizzaShop.pizza_stock: PizzaShop.pizza_stock -= 1 yield take yours pizza order, total pizzas left {}.format(PizzaShop.pizza_stock) mypizza_shop = PizzaShop() pizza_order = mypizza_shop.get_pizza() # iterator is obtained print a pizza pls!! {}:.format(pizza_order.next()) print a pizza pls!! {}:.format(pizza_order.next()) output: Traceback (most recent call last): File /home/scott/pythonfiles/core_python/pizza.py, line 10, in module print a pizza pls!! {}:.format(pizza_order.next()) StopIteration don't know where i am doing mistakeany help will be appreciated... i have other questions on based on this class Change while not PizzaShop.pizza_stock: to while PizzaShop.pizza_stock:; I get the following output: a pizza pls!! take yours pizza order, total pizzas left 9: a pizza pls!! take yours pizza order, total pizzas left 8: ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor