'Parametric Design /21. Python 툴팁'에 해당되는 글 6건

  1. 2015.06.14 Built-in Functions in Python
  2. 2015.06.13 Loop Mechanisms in Python
  3. 2015.06.13 More Syntax, Keywords, Functions, Variable Scope
  4. 2015.06.13 Operator precedence
  5. 2015.06.13 Capitalization, Indentation, String Splicing
  6. 2015.06.13 [기초] Python in operator와 Advance S

Built-in Functions in Python


The Python interpreter has a number of functions built into it that are always available. They are listed here in alphabetical order.

Built-in Functions
abs()divmod()input()open()staticmethod()
all()enumerate()int()ord()str()
any()eval()isinstance()pow()sum()
basestring()execfile()issubclass()print()super()
bin()file()iter()property()tuple()
bool()filter()len()range()type()
bytearray()float()list()raw_input()unichr()
callable()format()locals()reduce()unicode()
chr()frozenset()long()reload()vars()
classmethod()getattr()map()repr()xrange()
cmp()globals()max()reversed()zip()
compile()hasattr()memoryview()round()__import__()
complex()hash()min()set()
delattr()help()next()setattr()
dict()hex()object()slice()
dir()id()oct()sorted()

In addition, there are other four built-in functions that are no longer considered essential: apply()buffer()coerce(), and intern(). They are documented in the Non-essential Built-in Functions section.

abs(x)

Return the absolute value of a number. The argument may be a plain or long integer or a floating point number. If the argument is a complex number, its magnitude is returned.

출처

https://docs.python.org/2/library/functions.html

Posted by Parametric Culture
Loop Mechanisms in Python

메카니즘 설명

x = int(raw_input('Enter an integer: ')) # asks me to enter a value 
                    # for x that I want to find the cube root of
for ans in range(0, abs(x)+1): # tests answers from x = 0 to x ...
                    # Certainly the cube root of x can't be 
                    # greater than x.
    if ans**3 == abs(x): # compares ans**3 with x
        break # if we have the answer, break out of the loop
if ans**3 != abs(x): # if we run past the end of the range, we end
                # up here, so x is not a perfect cube, and we're
                # done.
     print(str(x) + ' is not a perfect cube')
else: # If we don't run out of range, and we have the answer, we
# end up here.
     if x < 0: # If we began with a negative integer, we will have a 
            # negative root. Note that we used abs(x) in the
            # above code to account for this possibility.
         ans = - ans
     print('Cube root of ' + str(x) + ' is ' + str(ans))

심플예제_1

num = 10
while True:
    if num < 7:
        print 'Breaking out of loop'
        break
    print num
    num -= 1
print 'Outside of loop'


Result

10, ,9, 8, 7, "Breaking out of loop", "Outside of loop"


심플예제_2

num = 2
while num < 11:
    print num
    num += 2
print "Goodbye!"
num = 2
while num <= 10:
    print num
    num += 2
print "Goodbye!"
num = 0
while True:
    num += 2
    print num
    if num >= 10:
        print "Goodbye!" 
        break
num = 1
while num <= 10:
    if num % 2 == 0:
        print num
    num += 1
print "Goodbye!"


심플예제_3

print "Hello!"
num = 10
while num > 0:
    print num
    num -= 2
num = 11
print "Hello!"
while num > 1:
    num -= 1
    if num %2 == 0:
        print num


심플예제_4

total = 0

current = 1

while current <= end:

    total += current

    current += 1


print total

Posted by Parametric Culture

More Syntax, Keywords, Functions, Variable Scope


관련자료 다운로드


2_syntax_keyword_vars_scope.pdf


출처

https://courses.edx.org


List of Python Keywords

https://docs.python.org/release/2.3.5/ref/keywords.html

'Parametric Design > 21. Python 툴팁' 카테고리의 다른 글

Built-in Functions in Python  (0) 2015.06.14
Loop Mechanisms in Python  (0) 2015.06.13
Operator precedence  (0) 2015.06.13
Capitalization, Indentation, String Splicing  (0) 2015.06.13
[기초] Python in operator와 Advance S  (0) 2015.06.13
Posted by Parametric Culture

Operator precedence

The following table summarizes the operator precedences in Python, from lowest precedence (least binding) to highest precedence (most binding). Operators in the same box have the same precedence. Unless the syntax is explicitly given, operators are binary. Operators in the same box group left to right (except for comparisons, including tests, which all have the same precedence and chain from left to right — see section Comparisons — and exponentiation, which groups from right to left).

OperatorDescription
lambdaLambda expression
if – elseConditional expression
orBoolean OR
andBoolean AND
not xBoolean NOT
innot inisis not<<=>>=<>!===Comparisons, including membership tests and identity tests
|Bitwise OR
^Bitwise XOR
&Bitwise AND
<<>>Shifts
+-Addition and subtraction
*///%Multiplication, division, remainder [8]
+x-x~xPositive, negative, bitwise NOT
**Exponentiation [9]
x[index]x[index:index]x(arguments...)x.attributeSubscription, slicing, call, attribute reference
(expressions...)[expressions...]{key: value...}`expressions...`Binding or tuple display, list display, dictionary display, string conversion


출처

https://docs.python.org/2/reference/expressions.html#operator-precedence

Posted by Parametric Culture

Capitalization, Indentation, String Splicing


Pyhton 툴팁 자료 다운로드


1_caps_indent_splicing.pdf


출처

https://courses.edx.org

Posted by Parametric Culture

The Python 'in' Operator


The operators in and not in test for collection membership (a 'collection' refers to a string, list, tuple or dictionary - don't worry, we will cover lists, tuples and dictionaries soon!). The expression

element in coll

evaluates to True if element is a member of the collection coll, and False otherwise.

The expression

element not in coll

evaluates to True if element is not a member of the collection coll, and False otherwise.

Note this returns the negation of element in coll - that is, the expression element not in coll is equivalent to the expression not (element in coll).


Advanced String Slicing


You've seen in lecture that you can slice a string with a call such as s[i:j], which gives you a portion of string s from index i to index j-1. However this is not the only way to slice a string! If you omit the starting index, Python will assume that you wish to start your slice at index 0. If you omit the ending index, Python will assume you wish to end your slice at the end of the string. Check out this session with the Python shell:

>>> s = 'Python is Fun!'
>>> s[1:5]
'ytho'
>>> s[:5]
'Pytho'
>>> s[1:]
'ython is Fun!'
>>> s[:]
'Python is Fun!'

That last example is interesting! If you omit both the start and ending index, you get your original string!

There's one other cool thing you can do with string slicing. You can add a third parameter, k, like this:s[i:j:k]. This gives a slice of the string s from index i to index j-1, with step size k. Check out the following examples:

>>> s = 'Python is Fun!'
>>> s[1:12:2]
'yhni u'
>>> s[1:12:3]
'yoiF'
>>> s[::2]
'Pto sFn'

The last example is similar to the example s[:]. With s[::2], we're asking for the full string s (from index 0 through 13), with a step size of 2 - so we end up with every other character in s. Pretty cool!


출처

https://courses.edx.org

Posted by Parametric Culture