程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python Basics

編輯:Python

One 、 Annotation problem

 

 

Use... For single line comments “#”----(“Ctrl+/” Shortcut key ) Annotation top grid , One space behind

Multiline comment '''  '''  perhaps """    """

In line comments   There are two spaces between comments and code

Two 、 data type

  How to get data types ?

It can be used type() Function to get the data type of the variable

result:

a = False;
print(type(a)) # bool

3、 ... and 、 Output

Output format

“%s Represents string substitution ,%d representative int type ,%f Represents a floating point type ”

There is also the second output mode :

f-string

name = 'Jimmy'
print("My name is %s" % name) # %s It's a string type
age = 19
print("My age is %d" % age) # %d Represents an integer int type
height = 167.2
# Ctrl + D Quick copy of a line of code ;shift + Enter Switch to the next line
print("My height id %f cm" % height) # %f Represents a floating point type
print("My height id %.2f cm" % height) # %f Represents a floating point type ,.2 Means to keep two decimal places
# Output 50%, When using formatted output , Want to output a %, You need to use two %
print(' The proportion of qualified people is %d%%' % 50)
print(f" My name is {name}, Age is {age} year , Height is {height}cm")

result :

 

Little question about line feed

Python Default output wrap , Can be removed

print('hello', end=' ') # Add one more end = ‘ ’
print('world')
# You want a newline in the middle of the statement , Use escape characters ‘\n’
print('hello\nworld')

Four 、 Input

# Input : Get the input from the keyboard , Into a computer program
# input() function
# input(' Tips for users '), Get user input , Entering means the end of input , The data obtained is of string type
password = input(" Please input a password :")
print(" The password you entered is %s" %password)

password = input(" Please input a password :")
print(" The password you entered is %s" %password)
# Type conversion
# Get the price of apple from the keyboard
price = input(" Please enter the price :") # str type
weight = input(" Please enter the weight :") # str
# Forced type conversion
result = float(price) * float(weight)
print(f" The unit price of apple is {price} element / Jin , bought {weight} Jin , Need to pay {result} element ")

Insert the number operator here :

 Python keyword ['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']

5、 ... and 、 About data type conversion

pi = 3.14
num = int(pi)
print(type(pi)) #float
print(type(num)) #int
my_str = '10'
num1=int(my_str) #int
num2 = 10
num3 = float(num2) #float
num4 = float("3.14") #float
num5 = float("10") #float
# eval() Restore the original data type , Remove the quotation marks from the string
num6 = eval('12') #int
num7 = eval('3.14') #float
num8 = eval('num7') #float
print(num8,type(num8))
num9 = eval('hello')
print(num9) #error, because hello Here are variables , No definition 
# practice , Personal information
name = 'Weiwei'
print("My name is %s" %name)
age = 25
print("My age is %d" %age)
weight = 45
print("My weight is %d Kg" %weight)
number = 17816126041
print("My phone number is %d" %number)
Address = 'HuaiNan'
print(f"I live in {Address}")
height = 167.2
print(f" height {height:.2f}cm") # Keep two decimal places
user_name = input("Please input your name:")
user_password = input("Please input your password:")
print(f"your name is {user_name},your password is {user_password}")


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved