Stupid methodology python3

Read "stupid methodology python3", summed up the knowledge

Problem 1: Installation environment using the print function + practice main difference between double and single quotation marks to distinguish

Problem 2: comment symbol #

Problem 3: The operator precedence, with C / C ++, Java similar

The following operator precedence: down incrementally from this, the same behavior the same priority 

Lambda # operator has the lowest priority logical operators: or logical operators: and logical operators: not members of the test: in, not in identity test : is, is not the comparison: <, <=,>,> =, =, ==! bitwise oR: | bitwise XOR: ^ bitwise aND: & shift: <<, >> addition and subtraction: +, - multiplication, division and modulo: *, /,% sign: + x, -x

Problem 4: variable name + print describes this form of print

print("There are", cars, "cars available.")  

Problem 5: More printing method such as f "XXX"

my_name = 'Zed A. Shaw'
print(f"Let's talk about {my_name}")

Problem 6: continue to use the f "XX" Print

Problem 7: format printing method, may be employed end = '' instead of line feed

print("Its fleece was white as {}.".format('show'))
print(end1 + end2 + end3 + end4 + end5 + end6, end=' ')
print(end7 + end8 +end9 + end10 +end11 + end12)

显示如下:
Its fleece was white as show.
Cheese Burger

Problem 8: More print mode (print this way to see less)

formatter = '{} {} {} {}'

print(formatter.format(1, 2, 3, 4))
print(formatter.format("one", "two", "three", "four"))
print(formatter.format(True, False, False, True))
print(formatter.format(formatter, formatter, formatter, formatter))
# print("\n")
print(formatter.format(
    "Try your",
    "Own text here",
    "Maybe a poem",
    "Or a song about fear"
))

显示如下:
1 2 3 4
one two three four
True False False True
{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {}
Try your Own text here Maybe a poem Or a song about fear  

  

Problem 9: multi-line printing, a new line

Summary: Print mode

1)print("There are", cars, "cars available.")  # 变量名直接打印
2)print(f"Let's talk about {my_name}") # 带有f"{}"的打印方式
3) 变量名+format()

hilarious = False
joke_evaluation = "Isn't that joke so funny?! {}"

print(joke_evaluation.format(hilarious))

4)print("Its fleece was white as {}.".format('show'))  #   
5)formatter = '{} {} {} {}'
    print(formatter.format(1, 2, 3, 4))
    print(formatter.format("one", "two", "three", "four"))
6)print('''
There's something going on here.
With the three double-quotes.
We'll be able to type as much as we like.
Even 4 lines if we want,or 5,or 6
''')                                                  # 多行打印     
        

  

Exercise 10: The escape character

11 Problem: The input () function, and a printing mode using the exercises described 5

print("How old are you?", end=' ')
age = input()
print("How tall are you?", end=' ')
height = input()
print("How much do you weight?", end=' ')
weight = input()

print(f"So,you're {age} old,{height} tall and {weight} heavy.")  

12 Problem: The input () function prompt plus   

age = input("How old are you?")
print ( "How old are you? {}". format (input ())) # prompt before running back to run in front of

Exercise 13: parameters, variables and unpacking

from SYS Import argv 
# the Read at The WYSS Section for How to RUN the this 
Script, First, SECOND,, THIRD, = argv 

Print ( "at The Script IS Called:", Script) 
Print ( "Your First variable IS:", First) 
Print ( " iS SECOND, variable Your: ", SECOND,) 
Print (" Your THIRD, variable iS: ", THIRD,) 

# Note: argv things out in, unpack, all parameters in order to replicate these variables on the left 
>> python ex13. py first second third

  

14 Exercise: Tips and passed mainly on input () function with the input format

from sys import argv
'''
PS E:\dev\code\笨方法学python3> python ex14.py Zed
Hi Zed,I'm the ex14.py script.'
I'd like to ask ypu a few questions.
Do ypu like me Zed?
>yes
Where do ypu live Zed?
>yicheng
What kind of computer do you have?
>Dell
'''
script , user_name = argv
prompt = '>'

print(f"Hi {user_name},I'm the {script} script.'")
print("I'd like to ask you a few questions.")
print(f"Do ypu like me {user_name}?")
likes = input(prompt)

print(f"Where do you live {user_name}?")
lives = input(prompt)

print("What kind of computer do you have?")
computer = input(prompt)

print(f'''
Alright, so you said {likes} about liking me.
You live in {lives}. Not sure where that is.
And you have a {computer} computer. Nice.
''')

  

Practice 15: read the file

txt = open(filename)
print(f"Here's your file {filename}:")
print(txt.read())
txt.close()

 

Exercise 16: Reading and Writing Files

close: close the file. With your editor in the "File" -> "Save" is a meaning 
read: read the contents of the file 
readline: only read text file line 
truncate: empty file, please be careful using this command 
write ( 'stuff' ): the "stuff" to write the file 
seek (0): the read position to the beginning of the file  

Practice 17: Continue to read and write files

in_file = open(from_file)
indata = in_file.read()

out_file = open(to_file,'w')
out_file.write(indata)

  

Exercise 18: Functions

Exercise 19: Functions and Variables   

pytyon when defining the function with the C / C ++ is quite big difference, Python no need to define the type of parameters, defining a particular function can refer to the following:

def function(vailable1, vailable2,*):

Exercise 20: functions and files

Import the argv SYS from 

Script, the argv = input_file 

DEF print_all (F): 
    Print (reached, f.read ()) 

DEF rewind (F): 
    f.seek (0) # print with line numbers 
def print_a_line (line_count, f) : 
    Print (line_count, f.readline ()) 
current_File = open (input_file) # accept parameters, open the file 
Print ( "First the let's at the Whole file Print: \ the n-") 
print_all (current_File) # print the file 
print ( "Now apos rewind the let, kind of like a Tape. ") 
rewind (current_File) # read position to the beginning of the file (in front of a position of the first character) to facilitate the subsequent read operation is not performed as the subsequent printing blank 
print ( "Let apos Print Three Lines:") 
current_line. 1 = 
print_a_line (current_line, current_File) 
print_a_line (current_line, current_File)

current_line current_line +. 1 = current_line = current_line + 1 print_a_line(current_line, current_file)

  

  

 

Guess you like

Origin www.cnblogs.com/guohaoblog/p/11282948.html