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! '''
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')
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
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)
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))
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 ')