1.安裝pycharm
2.交換兩個變量
在python中使用一行代碼就可以解決
a, b = b, a
print(a, b)
3.標識符
java 字母數字下滑線,美元符, 且不能以數字開頭
c、python 字母數字下滑線、 且不能以數字開頭
4.變量命名方式
駝峰式命名法、下滑線命名
大駝峰 --UserNameInfo
小駝峰 -- userNameInfo
下劃線:user_name_info
5.python中的判斷語句
python語言使用空格進行強制縮進
1、if else
if(判斷條件){
滿足條件要執行的事情
}
python中
if 要滿足的條件:
滿足條件要執行的事情
else:
不滿足條件要執行的事情
eg:
new Scanner(System.in)
input()內置函數
age = input('請輸入您的年齡')
age = 8
判斷一個變量的數據類型
print(type(age))
字符串轉化成 整型
36.elif
public class Main { public static void main(String[] args) {
// write your code here
Scanner scanner = new Scanner(System.in);
String s = scanner.next();
System.out.println(s);
}
}
7.python中的循環
while循環
格式
while 要判斷的條件:
循環體
i = 0
while i < 5:
print(i)
i++
i = i + 1
i += 1
例子 計算 1 ~ 100之間的累加和
![N1QYD9{}W6OU8(R]6F7ZT{L.png](https://upload-images.jianshu.io/upload_images/18958144-7f719740f5663ae5.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
8.跳出循環
break 跳出本層循環
continue 跳出本次循環,執行下次循環
例子 當累加和大于 1000時跳出循環
![H13~P3W9(RIK]62TUF4{EI.png
9.計算所有奇數的和
i = 1
sum = 0
while i <= 100:
% 取模 //整除
if i % 2 == 0:
偶數時跳出
i += 1
continue
sum += i
i += 1
print(sum)
10.猜數字游戲
1、準備知識
隨機整數的生成
from random import randint
from 模塊名 import name1, name2.。。。。
randint(start, end) [start, end]
print(randint(-20, 20))
while True:
print(randint(23,25))
import random
random.randint
11.字符串格式化輸出
hero_name = '魯班七號'
grade = 15
print('您選擇的英雄是'+hero_name+'當前等級為'+grade+'級')
print('您選擇的英雄是{}當前等級為{}級'.format(hero_name,grade))