Python Points
Python Points
-->Call the str(),int(),float() functions and pass them values of the other data
types to obtain a string,integer
,or floating-point form of those values.
-->The input function always returns string value,even if the user enters a number.
-->The boolean operators have an order of operations just like the math operators
do. After any math and compar-
ison operators evaluate, Python evaluates the not operators first,then the and
operators,and then the or operators.
-->else statement:
*An if statement can optionALly followed by an else statement.
*The else clause is executed only when the if statement's condition is false.
-->When there is chain of elif statements,only one or none of the clauses will be
executed.
*Once one of the elif condition is found to be true,the rest of the elif clauses
are automatically skipped.
*If one elif statement is true among other elif statement,Other elif statements
are skipped even if they are
true.
-->If you need to create a global variable, but are stuck in the local scope, you
can use the global keyword.
The global keyword makes the variable global.
-->Break statement: There is a short cut to getting the program execution to break
out of a while loop's clause
early.
-->In for loop it keeps looping even if the condition is False,Where as while loop
starts looping only if the
condition is true.
hello('Alice')
hello('Bob')
-->One special thing to note about parameters is that the value stored in a
parameter is forgotten when the
function returns.For example,if you added print(name) after hello(bob) in the above
program,the program will
give you a NameError because there is no variable named name.This variable is
destroyed after function call
hello(bob)returns, so print(name) would refer to a name variable that does not
exist.
-->import random
def getAnswer(answerNumber):
print(answerNumber)
if answerNumber == 1:
return 'It is certain'
elif answerNumber == 2:
return 'It is decidedly so'
elif answerNumber == 3:
return 'Yes'
elif answerNumber == 4:
return 'Reply hazy try again'
elif answerNumber == 5:
return 'Ask again later'
elif answerNumber == 6:
return 'Concentrate and ask again'
elif answerNumber == 7:
return 'My reply is no'
elif answerNumber == 8:
return 'Outlook not so good'
elif answerNumber == 9:
return 'Very doubtfull'
print(getAnswer(random.randint(1,9)))
The program execution returns to the line at the bottom of the program that
originally called getAnswer()
One rule when function is called the evaluated value is called return value,that
return value is stored inside
function.
Instead of using the range(len(someList)) technique with a for loop to obtain the
integer index of the items in
the list, you can call the enumerate() function instead. On each iteration of the
loop, enumerate() will return
two values:the index of the item in the list, and the item in the list itself. For
example, this code is
equivalent to the code in the "Using for loops with Lists" on page 84:
-->A method is the same thing as a function, except it is "called on" a value.
-->If you have only one value in your tuple, you can indicate this by placing a
trailing comma after the value
inside the parentheses. Otherwise, Python will think you've just typed a value
inside regular parentheses. The
comma is what lets Python know this is a tuple value. (Unlike some other
programming languages, it's fine to
have a trailing comma after the last item in a list or tuple in Python.)