There are already a few things that caught my attention. At first the webdevelopment seems to look awfully like perl. In a tutorial i found they just printed the the html but i think i will find better ways when i explore python further.
Beside the obvious differences like syntax i found it refreshing there were similar functions for strings and arrays, they are in a datatype group called sequences. I will switch from python to php to show similar output
# define string and array variable
string = 'test'
array = [1, 2, 3, 4]
// define string and array variable
$string = 'test';
$array = array(1, 2, 3, 4);
# get size
len(string)
len(array)
// get size
strlen($string);
count($array);
# get one value
string[2]
array[2]
// get one value
substr($string,2,1);
$array[2]
# get last value
string[-1]
array[-1]
// get last value
substr($string,strlen($string)-1);
$array[count($array)-1];
# get multiple values start in the beginning
string[:2] # display the first 2
array[:2]
string[2:] # display everything except the first 2
array[2:]
// get multiple values start in the beginning
substr($string,0,2); // display the first 2
array_slice($array,0,2);
substr($string,2); // display everything except the first 2
array_slice($array,2);
# get multiple values start in the end
string[:-2]
array[:-2]
string[-2:]
array[-2:]
// get multiple values start in the end
substr($string,strlen($string)-2);
array_slice($array,-2);
substr($string,0,strlen($string)-2);
array_slice($array,0,count($array)-2);
# get part of variable
string[1:3]
array[1:3]
// get part of variable
substr($string,1,2);
array_slice($array,1,2);
# add value
string + ' test'
array + 5
// add value
$string .= ' test';
$array[] = 5;
# copy variable with itself
2*string
2*array
// copy variable with itself
$string .= $string;
foreach($array as $value){ $array[] = $value; }
As you can see there is more php code necessary and it involves different functions as well. There is also a loop necessary in php to mimic the python code.
You can also see a difference getting a part of the variable in python and php. In python you limit the part by the value key and in php you limit the part by how many values you need.
Because strings and arrays are a basic in every language it has to be as easy as possible to work with them.
No comments:
Post a Comment