Showing posts with label function. Show all posts
Showing posts with label function. Show all posts

How to use extend() method of List in Python?

 The extend() method adds all the elements of an iterable (list, tuple, string etc.) to the end of the list.

For example, the following code extends list1 with iterable - list2/set2.

list1 = [1, 2, 3]
list2 = [4, 5]

list1.extend(list2)
print(list1)

# Output
[1,2,3,4,5]

list1 = [1, 2, 3]
set2 = (4, 5)

list1.extend(set2)
print(list1)

# Output
[1,2,3,4,5]
This is the same behavior as += for list if you have been using it.

list1 = [1, 2, 3]
list2 = [4, 5]

list1 += list2
print(list1)

# Output
[1,2,3,4,5]
Compared to append(), extend() adds all elements of an iterable as separate elements to the list while append() simply appends iterable as an element.

list1 = [1, 2, 3]
list2 = [4, 5]

list1.append(list2)
print(list1)

# Output
[1, 2, 3, [4, 5]]

python function arguments with colon?



It's a function annotation; function arguments and the return value can be tagged with arbitrary Python expressions. Python itself ignores the annotation (other than saving it), but third-party tools can make use of them.
In this case, it is intended as type hint: programs like mypy can analyze your code statically (that is, without running it, but only looking at the source code itself) to ensure that only str values are passed as arguments to splitComma.
A fuller annotation to also specify the return type of the function:

def splitComma(line: str) -> str:
    ...

(Note that originally, function annotations weren't assumed to have any specific semantics. This is still true, but the overwhelming assumption these days is that the annotations provide type hints.)



Excel - Some useful functions

=LEFT(A2, LEN(A2)-2): Removes last two characters from contents of A2

=SUMPRODUCT(ARRAY1, ARRAY2...): Sum of the arrays' values

=IFERROR(VLOOKUP(A1, B2:E3, 4,0), 0): Change #NA to 0 while applying vlookup

jQuery - with keydown event animate your elements!

 $(document).ready(function(){  
   $(document).keydown(function(){  
     $('div').animate({left:'+=10px'},500);    
   });    
 });  

jQuery - focus() function for input element

 $(document).ready(function(){  
   $('input').focus(function(){  
     $(this).css('outline-color','#FF0000');    
   });  
 });  

Name:

JavaScript - Object Constructor with properties and functions

 function Rectangle(height, width) {  
  this.height = height;  
  this.width = width;  
  this.calcArea = function() {  
    return this.height * this.width;  
  };   
  this.calcPerimeter = function() {  
   return 2*(this.height+this.width);    
  }  
 }  
 var rex = new Rectangle(7,3);  
 var area = rex.calcArea();  
 var perimeter = rex.calcPerimeter();  

PHP function for removing URLs in string





While filtering URL information within tweet, function with regular expression to find and replace it is needed. The remove_URL() function below could do exactly what I want to do:)

PHP function for removing URLs in string:

1:  <?php  
2:  $string = "100% 준비되면 하겠다는 것은 하늘나라 가서 시작하겠다는 겁니다..http://t.co/S2sTmdF45o";  
3:  echo remove_URL($string);  
4:  function remove_URL($html){  
5:    return $result = preg_replace(  
6:      '%\b(([\w-]+://?|www[.])[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/)))%s',  
7:      '',  
8:      $html  
9:    );  
10:  }  
11:  ?>  

PHP - difference between print_r() and var_dump()

1:  <?php  
2:       $a = array('name'=>"Alice", 'age'=>35,'wife'=>'Blia');  
3:       print_r($a);  
4:       //print_r can not be displayed by print_r()  
5:       echo "print_r() result";  
6:       print_r(true); //print "1"  
7:       print_r(false); //print ""  
8:       print_r(null); //print ""  
9:       //For this reason,var_dump() is preferred  
10:       echo "var_dump() result";  
11:       var_dump(true); //print "boolean true"  
12:       var_dump(false); //print "boolean false"  
13:       var_dump(null); //print "null"  
14:  ?>  

PHP - return value by reference in function

To return a value by reference, both declare the function with an & before its name and when assigning the returned value to a variable:
1:  <?php  
2:       $names = array("A","B","C");  
3:       function &findOne($n){  
4:            global $names;  
5:            return $names[$n];       
6:       }  
7:       $person = &findOne(1);  
8:       $person = "D";  
9:       // the result will be "D"  
10:       echo $names[1];  
11:  ?>