LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-14-2014, 11:13 PM   #1
Drone4four
Member
 
Registered: Feb 2006
Distribution: Slackware, Gentoo, Manjaro
Posts: 205

Rep: Reputation: 30
beginner babysteps with python, learning functions


I purchased a course on udemy during one of their 75% off sales not that long ago. The course is called “Learn Python, it's CAKE (Beginners).” It’s taught by Jason Elbourne.

Jason begins his chapter on functions by assembling the following code sample:

Code:
1 def print_name(name='Jason'):
2         print name
3 print_name('Josh')
I had difficulty learning from that code. So I turned to Google and came across, “Think Python: How to Think Like a Computer Scientist.” According to this guide:

Quote:
Functions are defined using the def keyword. After this keyword comes an identifier name for the function, followed by a pair of parentheses which may enclose some names of variables, and by the final colon that ends the line. Next follows the block of statements that are part of this function.
This quote is from Marios Zindilis's section on functions in A Byte of Python 2.

I came across another book called, Python for Software Design: How to Think Like a Computer Scientist authored by Allen B. Downey. Downey's chapter on functions has helped me.

The only issue I have with Downey's guide is that its all done in the CLI Python interpreter. I prefer to use a text editor. Here is the example in Think Python which I adapted to be executed from file:

Code:
1 def print_twice(bruce):
2         print bruce
3         print bruce
4 
5 michael = 'Eric, the half a bee.'
6 print_twice(michael)
This is easier for me to understand I think. I have written an analogy in my effort to help determine how well I understand how this function in particular works.

Functions work like a telephone. The name of the function - - defined at line 1 - - “print_twice” - - is like a phone number which must be unique. You can’t use one phone number to reach two people. In line 1, “(bruce)” is a placeholder for an argument to be specified by the user each time the unique name is called in the future. “print bruce” at lines 2 and 3 are the operations performed, kinda like a voicemail message. Dialing (or calling) one unique number (a function) will connect you one person's voicemail answering machine, while dialing another unique number (a different function) will connect you to a different person. At line 4, “michael” is a variable with a string attributed to it. At line 6, the user passes the newly defined variable as the argument as it calls the function defined in lines 1 through 3.

Getting back to the phone analogy, some phone messages are longer and more complicated than others. In this case, the message is a useless simple print statement. There are other functions, like for Physicists and Engineers, which are used to perform complicated algorithms. I can see how useful functions could be for these scientists. I suppose I am working with print statements because I am a beginner who majored in Philosophy?

Have I demonstrated an accurate basic understanding of how to use functions in Python? If not, then could someone please add something or correct me? Am I way off? Or am I on the right track?

Last edited by Drone4four; 06-17-2014 at 08:09 PM. Reason: added a sentence introducing the first code sample + fixed the 2 hyperlink reference + revised analogy slightly
 
Old 06-15-2014, 10:55 AM   #2
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,224

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
That's perfect!
 
1 members found this post helpful.
Old 06-15-2014, 12:41 PM   #3
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
Originally Posted by Drone4four View Post
Dialing one number will give you one voicemail message, while calling (or dialing) another number (a different function) will return a different message.
Careful not to confuse the block of code referred to by the function name, with the value returned by the function (see 3.11 Fruitful functions and void functions in the page you linked to).
 
1 members found this post helpful.
Old 06-17-2014, 07:44 PM   #4
Drone4four
Member
 
Registered: Feb 2006
Distribution: Slackware, Gentoo, Manjaro
Posts: 205

Original Poster
Rep: Reputation: 30
Thanks, ntubski, for your clarification.

A friend of mine in RL responded to me over email. Here is bb@yte's email comment:

Quote:
In the first function print_name you posted, name='Jason' refers to a default value that gets set when no data is passed. ie, if you called print_name('Josh') it prints 'Josh' as expected while if you call print_name() it will print 'Jason' because you omitted the argument.

This is used for providing default values but also usefull for naming arguments. For example I could have the following

Code:
def say_hello(age, name):
    print 'Hello %s, you are %d years old!' % (name, age)
and if i called it with say_hello(31, 'Michael') it would print what you would expect, however if I did this

Code:
def say_hello(age, name="I didn't catch your name"):
     print 'Hello %s, you are %d years old!' % (name,age)
and I called it with say_hello(23) it would say Hello I didn't catch your name, you are 23 years old!. Because I did not provide a name while say_hello(31, 'Michael') produces the output one would expect. When it encountered Michael it used that instead of the default 'Jason'.

I hope that I did not confuse you with that, let me know.

Thank-you bb@yte, ntubski, dugan.

Last edited by Drone4four; 06-17-2014 at 08:12 PM. Reason: fixed the grammatical error in the first sentence about my friend in RL
 
  


Reply

Tags
beginner, functions, learning, python



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
books about learning linux for beginner yzzwolf Linux - General 6 11-15-2012 07:11 AM
[SOLVED] For a beginner learning C- is it OK to use old text books? keithostertag Programming 3 02-01-2012 11:25 AM
[SOLVED] Learning Python beginner Help toli Programming 9 01-31-2012 08:49 AM
Beginner Linux-only tools and info on learning C programming? linus72 Programming 1 02-23-2009 11:08 AM
Try Python, O'reilly Learning Python haknot Programming 5 02-15-2002 08:27 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:55 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration