Python entry-level Learning Record

Source: Internet
Author: User

1. Configure the python development tool-Eclipse + pydev

2. New project --> select create SRC, select version 3.0, and finish

3. Create a Python source file, select new-> pydev module, create a new Python module, and click Finish.

4. If the project uses version 2

Print welcome

Eclipse does not report errors,

Version 3 should be written like this

Print (welcome)

5. File Reading

Indium = open ("menu.txt", "R ")
# Iterate over the file printing each item
For line in indium:
Print line
# Now close it again
Indium. Close ()

Import time
# Create daily menu based on menu. txt
# First open the files to read (R) and write (W)
Indium = open ("menu.txt", "R ")
Outp = open ("menu. PRN", "W ")

# Create todays date string
Today = time. localtime (Time. Time ())
Thedate = time. strftime ("% A % B % d", today)

# Add banner text and a blank line
Outp. Write ("menu for % s \ n" % thedate)

# Copy each line of menu.txt to new file
For line in indium:
Outp. Write (line)

Print "menu created for % s..." % thedate

# Now close the files
Indium. Close ()
Outp. Close ()
6. Loop Structure
For line in indium:
Total = total + numwords (line) # accumulate totals for each line
Print "dddddd" # not included in the for loop body
&&&&&&&&&&&&&&&&&&&

For line in indium:
Total = total + numwords (line) # accumulate totals for each line
Print "dddddd" # Contained in the for loop body

7,

Function comments

It must be the first content to be defined by a function (that is, the first content after the colon ). Technically, functions are not required.
doc string
But you should do this.

**************************************** ******************

8. In python, everything is an object, and almost everything has attributes and methods.

All functions have a built-in__doc__Property, it will returndoc string;

A module is an object and all modules have a built-in attribute.__name__. One module__name__The value depends on how you apply the module. IfimportModule, then__name__The value is usually the module file name, without the path or file extension. However, you can run modules directly like a standard program. In this case__name__
Will be a special default value,__main__.

9. Python does not have obvious parentheses, braces, or keywords. Use a hard press enter to separate the statement, and use a colon and indentation to separate the code block.

Code blocks are defined by their indentation. The "code block" I am talking about refers to: functions,ifStatement,forLoop,whileLoop, and so on. The START indent indicates the start of the block, and the unindent indicates the end of the block. There are no explicit parentheses, braces, or keywords. This means that the blank space is important and must be consistent. In this example, the function code
(Includingdoc string4 spaces are indented. Not necessarily four, as long as they are consistent. The first line without indentation is considered outside the function body.

10. python3.0 evaluate the class function (contact code indent and condition judgment)

Def fib (n ):
Print ('n' = ', n)
If n> 1:
Return N * fib (n-1)
Else:
Print ('END of the line ')
Return 1

Print (FIB (5 ))

11. Test Module

You can design a test suite for your module within the module.ifStatement. When you directly run the module,__name__The value is__main__So the test suite is executed. When you import the module,__name__So the test suite is ignored. This makes it much easier to develop and debug new modules before they are integrated into a large program.

12. Tips

Usercount = 6
T = "users connected: % d" % (usercount ,)
Print (t)

Users connected: 6

>>> Print "Today's stock price: %. 2f" % 50.4625

50.46

>>> Print "change since yesterday: % +. 2f" % 1.5

+ 1.50

The % F format character option corresponds to a decimal floating point number. If the precision is not specified, a 6-digit decimal number is printed.
 
Use the % F format option that contains the ". 2" precision modifier to print only two decimal places.
 
You can even use various modifiers in combination. The add + modifier is used to display a positive or negative number before a value. Note that the ". 2" precision modifier is still in its original position and is used to print only two decimal places.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.