代碼較長,建議使用電腦閱讀本文。
10分鍾入門Python
本文中使用的是Python3
如果你曾經學過C語言,閱讀此文,相信你能迅速發現這兩種語言的異同,達到快速入門的目的。下面將開始介紹它們的異同。
Python與C語言基本語法對比
Python使用空格來限制代碼的作用域,相當於C語言的{ }
。
第一個程序 Hello,World!
C語言
#include<stdio.h>
int main(){
printf("Hello,World!");
return 0;
}
Python
print("Hello,World!")
怎么樣,是不是已經感受到Python的精巧了呢。
輸入輸出
C語言
#include<stdio.h>
int main(){
int number;
float decimal;
char string[20];
scanf("%d", &number);
scanf("%f", &decimal);
scanf("%s", string);
printf("%d\n", number);
printf("%f\n", decimal);
printf("%s\n", string);
return 0;
}
Python
number = int(input())
decimal = float(input())
string = input()
print(number)
print(decimal)
print(string)
如果你嘗試自己寫一個Python循環輸出語句,你肯定會發現Python的輸出默認的換行的,如果不想讓它換行,可給end
參數復制""
,例如
連續輸出不換行
for i in range(0, 10):
print(i, end="")
代碼注釋
C語言
#include<stdio.h>
int main()
{
// printf("注釋一行");
/**
printf("注釋多行");
printf("注釋多行");
printf("注釋多行");
printf("注釋多行");
**/
}
Python
# print("注釋一行")
# 三個單引號
'''
print("單引號注釋多行")
print("單引號注釋多行")
print("單引號注釋多行")
print("單引號注釋多行")
'''
# 三個雙引號
"""
print("雙引號注釋多行")
print("雙引號注釋多行")
print("雙引號注釋多行")
print("雙引號注釋多行")
"""
基本運算
C語言
#include<stdio.h>
int main()
{
int Result;
int a = 10, b = 20;
// 加法
Result = a + b;
printf("%d\n", Result);
// 自加
Result++;
++Result ;
printf("%d\n", Result);
// 減法
Result = b - a;
printf("%d\n", Result);
// 自減
Result--;
--Result;
printf("%d\n", Result);
// 乘法
Result = a * b;
printf("%d\n", Result);
Result *= a;
printf("%d\n", Result);
// 除法
Result = b / a;
printf("%d\n", Result);
Result /= a;
printf("%d\n", Result);
}
Python
a = 10
b = 20
# 加法
result = a + b
print(result)
# 減法
result = a - b
print(result)
# 乘法
result = a * b
print(result)
result *= a
# 除法
result = b / a
print(result)
result /= a
print(result)
注意:Python沒有自加,自減運算符,即
i++
、++i
、i--
、--i
,其他運算符基本與C語言相同。
判斷語句
C語言
#include<stdio.h>
int main()
{
int a = 1, b = 2, c = 1;
if(a == b)
{
printf("a == b");
}
else if(a == c)
{
printf("a == c");
}
else
{
printf("error");
}
}
Python
a = 1
b = 2
c = 1
if a == b:
print("a == b")
elif a == c:
print("a == c")
else:
print("error")
elif
相當於else if
,其他用法與C語言相同。
循環語句
while循環
C語言
#include<stdio.h>
int main()
{
int a = 0, b = 10;
while(a < b)
{
a++;
}
printf("%d", a);
}
Python
a = 0
b = 10
while a < b:
a+=1
else:
print(a)
for循環
C語言
#include<stdio.h>
int main()
{
for(int i = 0; i < 10; i++){
printf("%d\n", i);
}
}
Python
for i in range(0, 10):
print(i)
range(0, 10)
表示創建一個在[0, 10)區間的整數列表,相當於C語言for循環中的i < 10
條件
函數
C語言
#include<stdio.h>
int function(char name[], int age, float weight)
{
printf("Name:%s\n", name);
printf("Age:%d\n", age);
printf("Weight:%f\n", weight);
return 1;
}
int main()
{
char name[20];
int age;
float weight;
printf("請輸入名字:");
scanf("%s", name);
printf("請輸入年齡:");
scanf("%d", &age);
printf("請輸入體重:");
scanf("%f", &weight);
if(function(name, age, weight) == 1)
{
printf("執行完畢");
}
}
Python
#!/usr/bin/env python
# _*_coding:utf-8_*_
def function(name, age, weight):
print("Name:" + name)
print("Age:", age)
print("Weight", weight)
return 1
if __name__ == "__main__":
name = input("請輸入名字:")s
age = input("請輸入年齡:")
weight = input("請輸入體重:")
if (function(name=name, age=age, weight=weight) == 1):
print("執行完畢")
注意代碼的作用域,縮減相同表達的意思與C語言的
{ }
相同。
導入頭文件
C語言
#include<stdio.h>
#include<math.h>
float make_sqrt(float numA, float numB, float numC)
{
float sum = sqrt(numA + numB + numC);
return sum;
}
int main()
{
float a, b, c, result;
scanf("%f %f %f", &a, &b, &c);
result = make_sqrt(a, b, c);
printf("%f", result);
return 0;
}
Python
#!/usr/bin/env python
# _*_coding:utf-8_*_
import cmath
import cmath as mt
from cmath import sqrt
def make_sqrt_sum(numA, numB, numC):
sum1 = cmath.sqrt(numA + numB + numC)
sum2 = mt.sqrt(numA + numB + numC)
sum3 = sqrt(numA + numB + numC)
return sum1, sum2, sum3;
if __name__ == "__main__":
a, b, c = map(float, input().split())
result1, result2, result3 = make_sqrt_sum(a, b, c)
print(result1, result2, result3)
導入模塊
import cmath
import cmath as mt
from cmath import sqrt
第一種方法是直接導入cmath
庫(sqrt模塊包含在該庫中),
第二種方法是導入后給它起個別名(后面使用的使用不用敲那么長的名字了),
第三種方法是直接導入cmath
庫中的sqrt
模塊(我們只用到了這個模塊)。
數組
Python的數組相當靈活,這里直接介紹Python類似數組的組件,及其常用操作。
列表
列表中每個存儲的每個元素可以是不同的類型,例如整數、小數、字符串等。列表中可以實現元素的添加、修改、刪除操作,元素的值可以被修改。
peopleList = ["eye", "mouth", "nose", "brow", "ear", 1.80, 120]
print(peopleList) # 輸出整個列表
print(peopleList[0]) # 訪問索引為0的元素
peopleList[1] = "head" # 修改索引為1的元素
peopleList.append("arm") # 在列表末尾添加元素
peopleList.insert(1, "foot") # 在列表中插入元素
del peopleList[0] # 刪除索引位置的元素
result = peopleList.pop(0) # 刪除並引用索引位置的元素,先復制給result再從列表中刪除
peopleList.remove("nose") # 根據值來刪除元素
元組
元組與列表類似,不同的是,它的元素初始化后不能再修改。但可以通過重新給變量賦值操作,達到修改元素的目的。
# 元組
peopleTuple = ("eye", "mouth", "nose", "brow", "ear", 1.80, 120)
print(peopleTuple)
peopleTuple = ("eye", "mouth", "nose", "brow", "head", 6.6, 999) # 重新給變量賦值來達到修改元素的目的
字典
字典是由鍵-值對
組成的集合,可通過鍵名對值進行操作。
peopleDict = {"e": "eye", "m": "mouth", "n": "nose", "b": "brow", "h": 1.80, "w": 120}
print(peopleDict)
print(peopleDict["e"]) # 訪問
peopleDict["a"] = "arm" # 添加鍵-值對
peopleDict["w"] = 190 # 修改鍵-值對
del peopleDict["a"] # 刪除鍵-值對
最后
Python博大精深,要想學好建議還是認真研讀一本書。
本文已在公眾號:MachineEpoch發布,轉載請注明出處。