变量 字符串 数组 函数

以下大部分信息来自w3school 以及 Learn Python The Hard Way

操作 :声明(类型) 创建(赋值) 引用(调用 打印调用 过程调用)

变量
python:
Variables And Names:赋值运算符 不需要声明
code:
cars = 100
space_in_a_car = 4.0
drivers = 30
passengers = 90
cars_not_driven = cars - drivers

print "There are", cars, "cars available."

javascript:
code:

php:
code:
美元符号 定义变量

字符串:
python:
code
x = "There are %d types of people." % 10 //同样不需要声明 ,注释统一使用单行注释,我只懂这个加个分号结束语句 ; Js是按照空白来结束语句,%d=% 10 逆向声明 声明了%d
binary = "binary"
do_not = "don't"
y = "Those who know %s and those who %s." % (binary, do_not)// 声明了%s 百分号定义变量

print x
print y

print "I said: %r." % x
print "I also said: '%s'." % y

hilarious = False
joke_evaluation = "Isn't that joke so funny?! %r"

print joke_evaluation % hilarious

javascript:


php:
code:

strtolower($str) 字符串转换为小写
strtoupper($str) 字符串转换为大写
strnatcmp("4","14") 按自然排序比较字符串
strnatcasecmp() 同上,(区分大小写)

http://www.w3school.com.cn/php/php_ref_string.asp


函数:
python:
code
def print_two(*args):
arg1, arg2 = args
print "arg1: %r, arg2: %r" % (arg1, arg2)//dev定义函数 注意冒号 函数内有特殊的变量机制? arg1: %r, arg2: %r" % (arg1, arg2)(%定义字符串变量 ,为字符串添加变量值)

def print_two_again(arg1, arg2):
print "arg1: %r, arg2: %r" % (arg1, arg2)

print_two("Zed","Shaw")
print_two_again("Zed","Shaw")
//利用函数名字 ,调用 ,输出打印是由于函数的print做成的,投入两个变量参数。带星号允许输入多个参数


%r与%d 变量 变量名 (位置1 ,位置2)

javascript:
code:



//调用函数 按钮类型 可以在html的值上面引用函数

通过点击这个按钮,可以调用一个函数。该函数会提示一条消息。

http://www.w3school.com.cn/js/js_functions.asp
alert与return 是语句

php:
code

";
}

echo "My name is ";
writeMyName("David",".");

echo "My name is ";
writeMyName("Mike","!");

echo "My name is ";
writeMyName("John","...");
?>


返回值语句 :return

数组:
python:
code

the_count = [1, 2, 3, 4, 5]
fruits = ['apples', 'oranges', 'pears', 'apricots']
change = [1, 'pennies', 2, 'dimes', 3, 'quarters'] //同样不需要声明

# this first kind of for-loop goes through a list
for number in the_count:
print "This is count %d" % number //使用了一些奇怪的临时变量 "This is count %d" % number 调用?

http://readthedocs.org/docs/learn-python-the-hard-way-zh_cn-translation/en/latest/ex32.html

animals = ['bear', 'tiger', 'penguin', 'zebra']
bear = animals[0]

你定义一个 animals 的列表,然后你用 0 来获取第一个元素?! 这是怎么回事啊?因为数学里边就是这样,所以 Python 的列表也是从 0 开始的。虽然看上去很奇怪,这样定义其实有它的好处,而且实际上设计成 0 或者 1 开头其实都可以,

You take a list of animals, and then you get the first one using 0?! How does that work? Because of the way math works, Python start its lists at 0 rather than 1. It seems weird, but there's many advantages to this, even though it is mostly arbitrary.

The best way to explain why is by showing you the difference between how you use numbers and how programmers use numbers.

http://learnpythonthehardway.org/book/ex34.html

javascript:
code


如需修改已有数组中的值,只要向指定下标号添加一个新值即可
mycars[0]="Opel";
document.write(mycars[0]);

php:
有三种数组类型:
数值数组
带有数字 ID 键的数组
关联数组
数组中的每个 ID 键关联一个值
多维数组
包含一个或多个数组的数组
$names = array("Peter","Quagmire","Joe");


关联数组 键-值
$ages = array("Peter"=>32, "Quagmire"=>30, "Joe"=>34);


多维数组
$families = array
(
"Griffin"=>array
(
"Peter",
"Lois",
"Megan"
),
"Quagmire"=>array
(
"Glenn"
),
"Brown"=>array
(
"Cleveland",
"Loretta",
"Junior"
)
);

Array
(
[Griffin] => Array
(
[0] => Peter
[1] => Lois
[2] => Megan
)
[Quagmire] => Array
(
[0] => Glenn
)
[Brown] => Array
(
[0] => Cleveland
[1] => Loretta
[2] => Junior
)
)

echo "Is " . $families['Griffin'][2] .
" a part of the Griffin family?";

http://www.w3school.com.cn/php/php_arrays.asp

http://www.w3schools.com/php/php_arrays.asp

Each element in the array has its own index so that it can be easily accessed.

In PHP, there are three kind of arrays:

Numeric array - An array with a numeric index
Associative array - An array where each ID key is associated with a value
Multidimensional array - An array containing one or more arrays