The input in Python3 is the same as the raw_input in Python2
The type of input () that the user enters in Python2 is defined as the why type, so in Python2
Input () needs to be defined as a string, otherwise it will be considered a variable name, but there is no value for the variable name, so it will be an error.
End= "
Change the line break from print to null
Output 1-10 in addition to 7
1-100 of and
Find out all the odd numbers
#求1 -2+3-4+5 ... 99 of all numbers of the and
Restrict User Login
Other
String format operators
2.7
Print "%s is number%d!"% ("Python", 1)
3.5
Print ("%s is number%d"% ("Python", 1))
%s is replaced by a string,%d is replaced by a shape, and%f is replaced by a floating-point type.
Program input and Raw_input () built-in function 2.x, 3.x no, 2.x as far as possible without input
user = Raw_input (' Enter login ' name: ')
print ' Your login is: ', user
Enter a numeric string and convert the string to reshape
num = raw_input (' Now enter a number: ')
print ' Doubling your number:%d '% (int (num) * 2)
Name = "Aliex li"
name2 = Name #name2赋值为name
Print ("My name is", name,name2)
Name = "Tom" #更改name赋值 see if Name2 will change with it.
Print (name,name2)
The result of the experiment is not to change, because Name2 does not point to name, but directly to Alex Li
Variable definition rules:
1 variable names can only be any combination of letters, numbers, underscores
2 The first character of a variable name cannot be a number
3 There are also some keywords that cannot be declared as variable names such as and as break assert class and so on
General writing method of variable name
Gf_of_oldboy = "XXX" #下划线
Gfofoldboy = "XXX" #首字母大写
Pie = "Apple pie" custom constants, capitalized, meaning that they should not be changed
Input user Inputs
Username = input ("Your username:")
Password = input ("Your password:")
Print ("Your name is%s, your password is%s"% (Username,password))
Input and convert character types
age = Int (input (' Your-Age: '))
Small example of formatted output
Name = input (' Your name: ')
age = Int (input (' Your-Age: '))
Print (Type (age)) #打印变量类型
Job = input (' Your job: ')
info = ""
--------------Info of%s--------------
Name:%s
Age:%d
Job:%s '% (name,name,age,job)
Print (info)
Another format of the stitching output
Name = input (' Your name: ')
age = Int (input (' Your-Age: '))
Print (Type (age)) #打印变量类型
Job = input (' Your job: ')
Info2 = "'
--------------Info2 of {_name}--------------
Name: {_name}
Age: {_age}
Job: {_job}
". Format (_name = name,
_age = age,
_job = Job)
Print (Info2)
The third type of formatted splicing output
Name = input (' Your name: ')
age = Int (input (' Your-Age: '))
Print (Type (age)) #打印变量类型
Job = input (' Your job: ')
Info3 = "'
--------------Info3 of {0}--------------
Name: {0}
Age: {1}
Job: {2}
". Format (Name,age,job)
Print (INFO3)
Ciphertext input, call Getpass module, the module cannot be used in Pycharm, can be executed with cmd in the py file
Import Getpass
Name = input (' Your name: ')
Age = Getpass.getpass (' Your Age: ')
Job = input (' Your job: ')
Info3 = "'
--------------Info3 of {0}--------------
Name: {0}
Age: {1}
Job: {2}
". Format (Name,age,job)
Print (INFO3)
A simple If statement example
Username = ("Tom")
Password = ("abc")
Import Getpass
Name = input ("Enter Your Name:")
passwd = Getpass.getpass ("Enter Your password")
If name = = Username and passwd = = password:
Print ("Welcome user {User} login". Format (user = username))
Else
Print ("Invalid username or password")
Error hints
Indentationerror Indent Error
First level code to shelf write
If statement
Age_of_old_boy = 56
guess_age = Int (input ("Guess Age:"))
if guess_age = = Age_of_old_boy:
Print ("Bingo,you got It")
Elif guess_age > Age_of_old_boy:
Print ("Try to smaller")
Else
Print ("Try to bigger")
While loop
Age_of_old_boy = 56
Count = 0
While True:
if Count = = 3:
Break
guess_age = Int (input ("Guess Age:")) #这个赋值相当于把上面的if结束了, so use if instead of elif
if guess_age = = Age_of_old_boy:
Print ("Bingo,you got It")
Break
Elif guess_age > Age_of_old_boy:
Print ("Try to smaller")
Else
Print ("Try to bigger")
Count + = 1
End Add judgment output:
Age_of_old_boy = 56
Count = 0
While Count < 3:
guess_age = Int (input ("Guess Age:"))
if guess_age = = Age_of_old_boy:
Print ("Bingo,you got It")
Break
Elif guess_age > Age_of_old_boy:
Print ("Try to smaller")
Else
Print ("Try to bigger")
Count +=1
if Count = = 3:
Print ("You have tried too many times,fuck off")
Or
Age_of_old_boy = 56
Count = 0
While Count < 3:
guess_age = Int (input ("Guess Age:"))
if guess_age = = Age_of_old_boy:
Print ("Bingo,you got It")
Break
Elif guess_age > Age_of_old_boy:
Print ("Try to smaller")
Else
Print ("Try to bigger")
Count +=1
Else
Print ("You have tried too many times,fuck off")
While loop upgrade version
Age_of_old_boy = 56
Count =0
While Count <3:
guess_age = Int (input ("Guess Age:"))
if guess_age = = Age_of_old_boy:
Print ("Bingo,you got It")
Break
Elif guess_age > Age_of_old_boy:
Print ("Try to smaller")
Else
Print ("Try to bigger")
Count +=1
If Count ==3:
keep_guessing = input ("Does want to keep guessing?")
If keep_guessing! = ' No ':
Count =0
Continue function is to jump out of this cycle, continue the next cycle
For I in Range (0,10):
If I <3:
Print (' Loop ', i)
Else
Continue
Print (' hehe ')
For loop, break jumps out of the loop
For I in range (10):
Print ('-----', i)
For j in Range (10):
Print (j)
If J >5:
Break
Read and Write
Fout = open (' Output.txt ', ' W ') #打开文件output, in W parameter mode
Line1 = "This is here ' s the wattle,\n"
Fout.write (line1) #将line1写到output. txt
Line2 = ' The emblem of our land.\n '
Fout.write (line2) #将line2写到output. txt
Fout.close () #关闭文件
File name and path
The OS module provides functions for manipulating files and directories (OS stands for operating system, i.e. operating system).
OS.GETCWD returns the name of the current directory
Import OS
CWD = OS.GETCWD ()
Print (CWD)
CWD represents the current working directory of working directory, which is a relative path.
Os.path.abspath (' Memo.txt ') to find the absolute path to the file.
Absolute_path=os.path.abspath (' output.txt ')
Print (Absolute_path)
Use Os.path.exists (' Memo.txt ') to determine whether a file exists.
Import OS
Exists=os.path.exists (' Relativity.txt ')
Print (exists)
And check if it's a directory, whether it's a file, etc. refer to "thinking about Python like a computer scientist" page 147. (Os.path.isdir os.path.isfile Os.listdir (CWD)
Save the script as a file example:
Say_daddy= ""
#/usr/bin/env python
#-*-Coding:utf-8-*-
#Author: Yuqiuyang
Baba = "Papa"
Count = 0
While True:
if Count = = 10:
Break
Shout = input (' Call Dad: ')
If Shout ==baba:
Print (' Good boy, Cool ')
Break
Elif Shout! = Baba:
Print (' See I can't kill you ')
Count +=1
Print (' Ah shoot, good tired. ‘)‘‘‘
Fout = open ("say_daddy.py", ' W ')
Fout.write (Say_daddy)
Fout.close ()
Module initial recognition
Import Sys
Print (Sys.path) #打印绝对路径
Print (SYS.ARGV) #打印相对路径 and can also be printed with parameters
Import OS
Cmd_res = Os.system ("dir") #os. System () can interact directly with the systems and execute the dir command without saving the results
Print (cmd_res) #并不能打印出dir命令, because the system () output directly to the screen, the print result is 0, to perform the successful meaning.
Import OS
Cmd_res = Os.popen ("dir") #打印为内存对象地址
Print (Cmd_res)
String manipulation
name = ' My name is Alex '
Print (Name.count (' a ')) #计数a有多少个
Print (Name.capitalize ()) #首字母大写
Print (Name.center, '-') #重复50遍-and the name to the middle
Print (Name.endswith (' ex ')) #判断以ex结尾
name2 = ' My \tname is Alex '
Print (Name2.expandtabs (tabsize=30)) #将tab键转换为30个空格
Print (Name.find (' e ')) #查找索引
Print (Name[name.find (' names '):]) #字符串也可以切片
Name3 = ' My name is {Mingzi},and My age is {years} '
Print (Name3.format (Mingzi = ' Yuqiuyang ', years = 23°c)) #格式操作
Print (Name3.format_map ({' Mingzi ': ' Yuqiuyang ', ' Years ': ') ') #字典的方式格式操作
Print (' abc123 '. Isalnum ()) #包含英文字符和数字为true
Print (' abc123= '. Isalnum ()) #包含特殊字符为false
Print (' AbC '. Isalpha ()) #判断是否为英文字母
Print (' 1 '. Isdecimal ()) #判断是否为十进制
Print (' 1 '. IsDigit ()) #判断是否为数字
Print (' A1A '. Isidentifier ()) #判断是不是一个合法的变量名
Print (' A '. Islower ()) #判断是否为小写
Print (' 2 '. IsNumeric ()) #判断是否为数字
Print (". Isspace ()) #判断是否为空格
Print (' My Name is '. Istitle ()) #判断是否每个首字母大写
Print (' My name is '. Isprintable ()) #判断是否能打印. TTY file,drive file cannot be printed
Print (' MY NAME is '. Isupper ()) #判断是否都为大写
Print (Name.ljust (50, ' * ')) # This is ljust, length 50, no ending with * #
Print (Name.rjust (50, '-')) # This is ljust, length 50, no use-No.
Print (' ALEX '. Lower ()) #将大写变成小写
Print (' ALEX '. Upper ()) #将小写变成大写
Print (' alex\n '. Lstrip ()) #将左边空格和回车, no removal on the right
Print ('---')
Print (' alex\n '. Rstrip ()) #将右边空格和回车, left not removed
Print ('---')
Print (' alex\n '. Strip ()) #去掉所有空格和回车
Print ('---')
p = Str.maketrans (' Abcdefghi ', ' 123456789 ') #字母和数字一一对应
Print (' Abed bi '. Translate (p)) #将前面的字母转换成相应的数字
Print (' Alex Li '. replace (' l ', ' l ')) #将l替换成L
Print (' Alex Li '. replace (' l ', ' l ', 1)) #将第1个l替换成L
Print (' Alex Li '. RFind (' l ')) #找到最右边l的下标
Print (' Al ex Li ', Split (' L ')) #将字符串以l做分隔符做成列表
Print (' 1+2+3+4 '. Split (' + ')) #将字符串以 + make a list of delimiters
Print (' 1+2+3+4 '. Splitlines ()) #将以换行符为分隔符做列表
Print (' Alex Li '. Swapcase ()) #字母大小写取反
Print (' Alex Li '. Title ()) #首字母大写
Print (' Alex Li '. Zfill ()) #字数不够在前面补齐0
Python Learning section II