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

[python notes \u 1] essentials of Python Basics

編輯:Python

List of articles

  • 1 Python The basics
  • 2 Input and output
  • 3 Variable
  • 4 Operator
  • 5 Branching structure

1 Python The basics

1、 All punctuation marks in programming languages are in English

print('hello world!')

2、Python advantage

1. Simple and clear , Easier to use than other languages
2. Open source , Strong ecology and community
3. In the Windows、mac、Linux Running on the system

3、Python Application field

Python stay web Server development 、 Cloud infrastructure development 、 Network data collection ( Reptiles )\ Data analysis 、 data mining 、 machine learning 、 automated testing 、 Artificial intelligence 、 Automatic operation and maintenance

4、 There are two forms of code comments

Single-line comments : With “#” and “ ” start , Shortcut key :Ctrl + /
Multiline comment : Start with three quotation marks , End with three quotation marks ( You can use single or double quotation marks ), Add multiline comments ,“’”x3

''' Hello,World! '''

2 Input and output

1、 Input :Python Built in functions input()

All through input() The input and output types of the function are all string types

a = input(' Please enter a sentence :')

Add with int Change the output form to int type

b = int(input(' Please enter a number :'))

2、 Output :Python Built in functions print()

Formatted string
1. Format string literals —— Change the contents of the string directly

age = 10
print(f' Xiaoming this year {
age} year ')

2. A string of format() Method ——format The input will be filled up to {} in

print(' Xiaoming this year {} year '.format(10))
print('{} This year, {} year '.format(10))—— Perform error
print('{} This year, {} year '.format(' Xiao Ming ', 10))

3.% Method

num = 3
# Integer output 
print('%d' % num)

4. Floating point output ,Python Only %f( Default hold 6 Decimal place ), No, double type

print('%f'% num)
# Specify the output length 
print('%.1f'% num) Retain 1 Decimal place

5. String output

str_1 = 'abc'
print('%s' % str_1)

6.print() Parameters —— The last character defaults to end=‘\n’, It can be modified manually , Such as ’\t’,‘’
sep Default is space

print(1,2,3,end='\t')
print('abc')
print(1,2,3,sep = '')
print(1,2,3,sep = '',end = '\t')

3 Variable

1、 Three parts of variables

Variable name Assignment symbol A variable's value
name = ‘Violets’

2、 Variable naming conventions

1. By digital 、 Letter 、 Underline composition , Cannot start with a number
2. Out of commission Python System keyword as variable name
3.Python Variable names are case sensitive
Such as :name_1 = ‘Violets’

3、Python System keywords

keyword explain and Logic and or Logic or not Logic is not if Conditional statements , Often with else、elif Use a combination of elif Conditional statements , Often with if、else Use a combination of else Use... In conditional statements , And if、elif Use a combination of . It can also be used for exception and loop statements forfor Loop statement whilewhile Loop statement True Boolean type value , Said really , And False contrary False Boolean type value , Said the false , And True contrary continue Jump out of this cycle , Execute the next cycle intermittently break Interrupts the execution of the entire loop statement pass Empty class 、 A placeholder for a method or function try Often used to catch exceptions , And except、finally Use a combination of exceptexcept Contains the operation code block after the exception is caught , And try、finally Use a combination of finally After exception , Always carry out finally Contains code blocks , And try、except Use a combination of .raise Throw an exception from For importing modules , And import Use a combination of import For importing modules , And from Use a combination of def Define a function or method return The return value of a function or method class Define a class lambda Anonymous functions del Delete a value from a variable or sequence global Define a global variable nonlocal Declare a nonlocal variable , Variables used to identify external scopes in Determine whether a variable is in a sequence is Determine if it is the same object None It means nothing , It has its own data type - NoneTypeassert For debugging as Create an alias with Constant harmony open Use , Used to read or write files yield End a function , Return to a generator , Used to return values from a function in turn

4、 Variable naming
Hump nomenclature

Hump : The first letter of every word should be capitalized (FirstName)
Little hump : Start with the second word , Capitalize the first letter of each word (firstName)

5、 Use of variables

a = 7
b = 2
print(a + b)
print(a - b)
print(a * b)

4 Operator

1、 Operator

Arithmetic operator : Add (+)、 reduce (-)、 ride (*)、 except (/)、 to be divisible by (//)、 Remainder (%)、 Power operation (**)

a = 7
b = 2
print(a + b)
print(a - b)
print(a * b)

2、 division
The result of division is a floating point number

print(a / b)

3、 to be divisible by
Divisor and divisor, whether integer or not , The results are all integers , Rounding down ( The maximum integer less than the current decimal )

4、 Remainder
The remainder is equal to the last remainder of the division

print(9 % 4)

5、 Power operation

print(a ** b)

6、 Assignment operator
Add is equal to (+=)、 Minus is equal to (-=)、 Multiply is equal to (*=)、 Divide equal to (/=)、 Division is equal to (//=)、 Take remainder equal to (%=)、 Power operation equals (**=)

a += b
# Equivalent to a = a + b
print(a)
# The rest is the same as above 

7、 Comparison operator :<、<、<=、>=、==、!=
The result of the comparison operator is a Boolean value :True perhaps False

print(a < b)

8、 Logical operators
and、or、not

year = 2000
if not((year % 4 == 0 and year % 100 != 0) or year % 400 == 0):
print(' Ordinary year ')
else:
print(' Leap year ')

practice 1: Convert Fahrenheit to Celsius
Centigrade temperature = ( Fahrenheit - 32)/ 1.8

temperature_F = float(input(' Please enter a temperature :'))
temperature_T = (temperature_F - 32) / 1.8
print('%.1f Fahrenheit = %.1f Centigrade temperature ' % (temperature_F,temperature_T))

5 Branching structure

1、if sentence

Python in , To construct a branching structure, you can use if、elif、else keyword

practice : Determine if a year is a leap year

# Method 1 
year = int(input(' Please enter a year :'))
if year % 4 == 0 and year % 100 != 0 or year % 400 == 0:
print(' Leap year ')
else:
print(' Ordinary year ')
# Method 2 
year = int(input(' Please enter a year :'))
if year % 4 ==0 and year % 100 != 0:
print(' Leap year ')
elif year % 400 == 0:
print(' Leap year ')
else:
print(' Ordinary year ')

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