python-Lesson One-Introduction Grammar (1)-Basic Part

Printout
format:
print ("")
print ""

Conditional branch
if condition:
  The operation performed if the condition is true
else:
  The operation performed if the condition is false (False)

while loop
while condition:
   The operation performed if the condition is true

Import module, import random function
import random
temp = random.randict (a, b)
function
type() returns the type of the value
isinstance(a,b) returns boolean. a is a variable, b is a numeric type, and judge true or false.  
range()  
Help document -help()

variable
Python is slightly different from most other computer languages. Ta does not store the value in a variable, but more like sticking the name on top of the value. Therefore, python has no variables, only names.

teach = "Learn python for the first time."
print(teach)

Variable naming theory:
Before using a variable, you need to assign a value to it.
Variable names can include letters, numbers, and underscores, but variable names cannot start with numbers.
*: Letters can be uppercase or lowercase, but capitalization is different. It also means that fishc and FishC are completely different names for PYTHON.
The symbol (=) means assignment. The left side is the name, and the right side is the value. You cannot write the reverse.

String
"xx"
'x'
'''s'''

Strings are everything inside quotation marks. Strings are also called text. Text and numbers are completely different.
Note: When creating a string, you must add quotation marks around the characters, which can be single quotation marks or double quotation marks. But it must be in pairs.

If single or double quotation marks need to appear in the string, there are two ways.
The first comparison is to use our common escape character (\) to escape quotation marks in a string:
The second type, the original string:
str = 'C:\now'
str = 'C:\\now'
str = 'C:\Program Files\Intel\WiFi\Help'
The use of the original string is very simple, just add an English letter r before the character:
str = r'C:\now'
Long string
If you want to get a string that spans multiple lines, you need to use triple quoted strings

Python comparison operators
Left is greater than right
>= Left is greater than or equal to right
< Left is smaller than right
<= Left is less than or equal to right
== Left is equal to right
!= Left is not equal to right
legend:

Conditional branch:
Application-Guess the number game:
print ("Welcome to the guessing game!")
temp = input("Enter the number you think of:")
guess = int(temp)
if guess == 8:
   print("You guessed it.")
else:
    if guess > 8:
      print("It's big.")
   else:
      print("Small.")
print("Game over")
note:  
① When entering the shell directly, typing in the code will cause an error notification. If it is a single statement, it can only be written line by line.

②Ctrl+N creates a new blank code page, after saving it (if you don’t save the run, there will be no response, or the same error is reported) press f5 to run, and the code function will start to execute.
while loop

Application-Guess the number game-increase the loop:
print("Welcome to the guessing game!")
temp = input("Enter the number you think of:")
guess = int(temp)
if guess == 8:
   print("Your sixth sense is really strong!")
while guess !=8:
   temp = input("You guessed wrong, please re-enter:")
   guess = int(temp)
   if guess == 8:
      print("You guessed it.")
   else:
      if guess > 8:
         print("It's big.")
      else:
         print("Small.")
print("Game over")
Expansion:
There is a function in the random module called randint(): it can return a random integer.

Application-Guess the number game-Introduce module, import random function:
import random
secret = random.randint(1,100)
print("Welcome to the guessing game!")
temp = input("Enter the number you think of:")
guess = int(temp)
if guess == secret:
   print("Your sixth sense is really strong!")
while guess !=secret:
   temp = input("You guessed wrong, please re-enter:")
   guess = int(temp)
   if guess == secret:
      print("You guessed it.")
   else:
      if guess > secret:
         print("It's big.")
      else:
         print("Small.")
print("Game over")
Numerical types required by python:

Python3 achieves a seamless connection between integer and long integer, while python2 needs to add an L after the long integer.
e notation (scientific notation):
Boolean type:
True = 1 , False = 0 
类型转换:

整数-int()
字符串-str()
浮点数-float()


type():
isinstance():
算术操作符:
+  -   *   /   %    **  //
加  减  乘  除  取余数  幂  商取整
运算符 描述 实例
+ 加 - 两个对象相加 a + b 输出结果 30
- 减 - 得到负数或是一个数减去另一个数 a - b 输出结果 -10
* 乘 - 两个数相乘或是返回一个被重复若干次的字符串 a * b 输出结果 200
/ 除 - x除以y b / a 输出结果 2
% 取模 - 返回除法的余数 b % a 输出结果 0
** 幂 - 返回x的y次幂 a**b 为10的20次方, 输出结果 100000000000000000000
// 取整除 - 返回商的整数部分 9//2 输出结果 4 , 9.0//2.0 输出结果 4.0

运算符优先级:

运算符 描述
** 指数 (最高优先级)
~ + - 按位翻转, 一元加号和减号 (最后两个的方法名为 +@ 和 -@)
* / % // 乘,除,取模和取整除
+ - 加法减法
>> << 右移,左移运算符
& 位 'AND'
^ | 位运算符
<= < > >= 比较运算符
<> == != 等于运算符
= %= /= //= -= += *= **= 赋值运算符
is is not 身份运算符
in not in 成员运算符
not or and 逻辑运算符
应用-分数判定-1-if条件判断:
score = int(input('请输入一个分数:'))
if 100 >= score >= 90:
   print('A')
if 90 > score >= 80:
   print('B')
if 80 > score >= 60:
   print('C')
if 60 > score >= 0:
   print('D')
if 100 < score or score < 0:
   print('输入错误')
应用-分数判定-2-if嵌套语句:
score = int(input('请输入分数:'))
if 100 >= score >= 90:
   print('A')
else:
   if 90 > score >= 80:
      print('B')
   else:
      if 80 > score >= 60:
         print('C')
      else:
         if 60 > score >= 0:
            print('D')
         else:
            print("输入错误!")
应用-分数判定-3-elif语句:
score = int(input('请输入一个分数:'))
if 100 >= score >= 90:
   print('A')
elif 90 > score >= 80:
   print('B')
elif 80 > score >= 60:
   print('C')
elif 60 > score >= 0:
   print('D')
else:
   print('输入错误')
注意点:
格式问题容易导致一些难以预料的情况发生。

条件表达式:
三元操作符:
x,y=4,5
if x < y:
   small = x
else:
   small = y
化简为:
small = x if x < y else y
三元操作符的语法:x if 条件 else y
断言(assert):

assert 3 > 4
for循环:
语法:
for 目标 in 表达式:
    循环体

循环实例-列表长度:
member = ['今天','你吃','了','吗','?']
for i in member:
   print(i,len(i))
range()
语法:range([strat,]stop[,step=1])
实例:
break-continue语句:
a=2
b=int(input("输入你想的数字:"))
while True:
   if a==b:
      break
   else:
      b=int(input("请重新输入:"))

print("猜对了!")
break是完全跳出循环,而continue是跳出本阶段循环,重新开始循环。
for i in range(10):
   if i%2 != 0:
      print(i)
      continue
      print("调试。")
   else:
      print(i,end="shi\n")
      i += 1
      print(i,end='-x\n')
注意:python对于缩进块极其敏感,如果出现没有缩进的问题,则会报错。

逻辑运算符:
and:
在python中,3 < 4 < 5 相当于(3 < 4) and (4 < 5)
or:
not:



Guess you like

Origin blog.csdn.net/u013362192/article/details/79691828