Machine learning Python basics _day1_ "I am proficient in more than twenty programming languages Hello world program" by the way, record that pycharm has no code completion

Python first lesson "Hello World"

-basis

1. The first sentence of python

-The suffix can be anything?
The content of running under cmd is print("hello world")
XXX.py, XXX.txt, XXX.sb and found that it can also be run

-When the program is more complicated, when multiple files are needed to import the module together, the import between files is involved. If it is not a .py file, an error will occur.

2. Enter the python interactive interface

Enter python under cmd and press Enter, and
'>>>_' will appear.
This is an interactive interface for real-time processing.

From this summary, python can be executed in two ways, the
file is executed directly, and the interpreter is executed in real time.

An interpreter definition is required in the header of the XXX.py file.
Example:

	 #!/usr/bin/env python #解释器声明
	 #-*-conding:utf-8 -*- #若下文代码中出现中文,需要指明编码格式
	 #否则在python2环境下会报错,在python3中会自动处理为中文编码
      print("hello world")

In the window system, it can run without the first line of code, but in Linux, there will be an operation of ./XX.py. If there is no declaration of the interpreter position in the first sentence, an error will be reported.

3. Perform an operation

Try to simulate the user login and enter the account password.
Verify that the account password is correct and take the corresponding action

usr=input('请输入用户名') #等待用户输入账户
passworld=input('请输入密码')#等待用户输入密码

usr1="root"
passworld1="root"
if usr==user1:
	if passworld==passworld1:
		print("登入成功")
	else:
		print('密码错误')
else:
	print('无此账户')
if1==1:
	pass #若没有此处的代码,就是令1==1判断执行,python是会报错的
		#pass是指空代码,无意义,仅仅用于表示代码块
else:
	print('你好')

4. Types of variables

Python is very smart and can automatically perform certain recognition conversions. But sometimes when there are special needs, there are corresponding methods to transform.

String:

name='水牛'
name=""水牛""
name='''水牛'''#' " 都可以声明字符串,常用的就是' 
#声明的时候用什么开头就用什么结尾,就可以被识别为字符串
n1='abc'
n2='def'
n3=n1+n2
print(n3) #'abcdef'

n3=n1*10
print(n3) #'abcabcacb.......abc'一共出现十次

digital:

a=10
b=5
c=a+b+3
print(c)#18
c=a*b
print(c)#50
c=a/b
print(c)#2
c=a**c #此时的C要注意哦!
print(c)#100 

a=39
b=4
c=a//b
print(c)#4求商 实际得4.XXXX取了正数部分称为商

c=11%10
print(c)#1 求余数

a=13
temp=a%2
if temp==0:
	print('偶数')
elseprint('奇数')

5. Loop

while

improt time
while 1==1:
	print(time.time())#疯狂输出时间,只能人为暂停
print("此句代码一定不会执行,因为上述代码为死循环")

#求1-100的合
a=1
sum=0
while(a<=100):
	sum+=a
print(sum) 

#输出1-100所有的奇数
a=1
while (a<=100):
	c=a%2
	if(c!=0):
		print(c)
	a++

#/usr/a/b/c python #Statement Interpreter
#-- conding:utf-8 --#Code declaration If there is no such declaration, python in 2.X will report an error to the Chinese in the code
print('hello ')

Use ./a.txt to call in Linux, and you need to add permissions to the file. Executable permissions.
About encoding: Unicode (Universal Code 16 bytes per character) including utf8 (3 bytes Chinese) GBK (2 bytes Chinese) …And so on. If you want to convert UTF8 to GBK, you can only use Unicode as an intermediate media for conversion. What is maintained in python3 is Unicode but not in python2, so conversion is required

inp=input('>>>')
等待输入
PS:
>>>hello
inp=“hello”

>>>10
print(inp*10) #"10101010"十次,此时是当成了字符串赋值给inp
new_inp=int(inp)
print(inp) #100 #转型操作 int(数字字符串)若传入的非数字串会报错 ValueError: invalid literal for int() with base 10: 'a'

6. Tired of writing this way? pycharm!

Wow, pycharm is a good news for python. Note that what you need must be the professional version. Fool-style installation, installing various support is also next-next-next. Must be the professional version! Must be the professional version! Must be the professional version! The important thing is said three times. Furthermore, don’t localize, the function is missing, and then you will go further and further on the road of programming.
About the time limit:
1. Trial for 30 days
2. Apply for a learning account, there are two or three years of time
3. You know, you can chat privately I want the corresponding tool software

Return to learning python

name='廖乃琳'
#判断连续字符串是否被包含 亦可以not in操作
if'乃琳'in name: 
	print('存在')
else:
	print('不存在')

Before this article, we talked about numeric values ​​and strings.
Next, we introduce Boolean variables.

name='乃大琳'
v='琳' not in name #此时他的结果非true 就false
if v:
	print(v)
a=1==1 #思考a是什么
print(type(a))
#得出结论,数的比较操作得出的均为布尔型 
#不等于!=
#不等于<> 此两种方法都可以使用,前者常用于代码,后者一般用于sql中
if a==1 and b==a:
	pass
#python可以使用 and or 等进行多判断 and or中没有优先级
#只会从前到后执行所以记得使用()包裹优先的条件

Operator

结果是值:
算数运算 a=10*10
赋值运算 a=a+1

结果是布尔值:
比较运算 a=1>5
逻辑运算a=1>6 or 1==1
成员运算a='琳' in '乃大琳'

The method of variable existence, all exist in this type

int #control +鼠标指向该int点击就可以看见该类型的所有方法

Common types of methods

digital:

a=int'123'#将字符串转换为数字;
num='0011'
b=int(num,base=2)
print(b) #3 将二进制转为十进制打印
age=5
r=age.bit_length()
print(r)#3 因为5的二进制位101 他的二进制长度为3

String:

test='alex'
v=test.capitalize()
print(v) #Alex 首字符大写
test='AAA'
v2=test.lower()
print(v2)#所有的变小写
v3=test.center(20,'*')#********AAA********* 剧中两边填充一共20个*
print(v3)
v4=test.count('A')#3 计数A出现了多少个,可以添加参数test.count('A',1,2) 从test字符串第1个找到第2个字符
print(v4)
test='abcde'
v=test.endswith('e')
print(v)#true 判断是否为e结尾,同样的有star的方法判断开头
text='alexalex'
v=text.find('ex')
print(v)#2 查找text中的子串
text='i am {name}'
v=text.format(name='alex')
print(v)#i am alex 此操作称为字符串格式化,将括号中的name替换为alex

text='i am {0},age{1}'
v=text.format('alex',1)
print(v)#i am alex,age1 格式化占位符,按顺序填入,有两种方式
text='alexalex_+'
v=text.isalnum()
print(v)#False 判断是否是数字串

Tuple:
Dictionary:
Boolean:

Make good use of pycharm's code hints, by the way, record that pycharm does not have code completion

Check the first point. Don't check
Don't check it. Check the mark here to disable the prompt.
Second check whether the interpreter is setInsert picture description here

Guess you like

Origin blog.csdn.net/LiaoNailin/article/details/108601433