Day01_ acquaintance Python

Introduction to Python

Python's history

1 1989 Christmas: Guidao von Rossum began to write Python language compiler

2. February 1991: First Python compiler (also interpreter) was born, he is using the C language (follow-up appeared versions of Jython and IronPython Java and C # implementation, as well as PyPy, Brython, Pyston other to achieve ). You can call the C language library functions. In the earliest version, Python has provided support for the "class", "function", "exception handling" and other building blocks, while providing a "list" and "dictionary" and other core data types, and support on a modular basis to expand the system.

3. January 1994: Python1.0 released.

4. In 2000 October 16: Python2.0 released, increasing the realization of complete garbage collection, offers good support for Unicode ground. At the same time, Python's entire development process more transparent, impact on community development progress gradually expanding ecosystem slowly began to form.

5. 2008, December 3: Python3.0 release, it is not fully compatible with previous Python code, but because there are many companies currently use Python2.x version of the project and operation and maintenance, so a lot of new features Python3.x It was also later ported to Python2.6 / 2.7 version.

Python's strengths and weaknesses

1. simple and clear

2. Low learning curve is easier to use compared with other languages

3. Open Source, has a strong community and ecosystem

4. interpreted language, inherently portability

5. supports two mainstream programming paradigm (object-oriented programming and functional programming)

6. Scalability and embedded, can be called C / C ++ code can also call Python in C / C ++ are

7. The high specification code, readable, there are codes for obsessive compulsive disorder and crowd

Disadvantages include:

1. Low efficiency, and therefore computationally intensive tasks can be written in C / C ++

2. The code can not be encrypted

3. In developing the framework that can be selected too many (web framework there are more than 100), selectively wrong place

Python applications

The current Python used in cloud infrastructure, DevOps, web crawler development, data analysis, mining, machine learning, etc. have a wide range of applications, and therefore also had a back-end Web development, data interface development, operation and maintenance of automation, automated testing, science computing and visualization, data analysis, quantitative trading, a series of jobs robot development, image recognition and processing.

Installation Python3.7

1. In the official website to download Python3.7, according to their needs, follow the steps to install.

After installation can, python --version Python version view from a terminal program running Python, as shown below:

Alternatively, you can enter into the python interactive interface 1, in the implementation of what version of python code checks.

Other tools introduced

IDLE- comes with integrated development tools

IDLE python installation is carrying on integrated development tools, as shown below:

IPython - better interactive programming tools

  IPython is based Python interactive interpreter. Compared to native Python Shell, IPython provides a more powerful editing and interactive features. Python by the package management tools mounted pip IPython and Jupyter, the specific operation is as follows.

pip install ipython jupyter

or

python -m pip install ipython jupyter

  After a successful installation, you can start IPython ipython by the following command. Of course, we can also operate interactively in a browser window running through the project called notebook Jupyter.

jupyter notebook

Sublime - text editor Artifact

  • First through the official website to install Sublime 2 Sublime 3 or download the installer.

  • Installation package management tools. Ctrl + `shortcut key or selecting Show Console console Open the View menu, enter the following code.

    • Sublime 3

    import  urllib.request,os;pf='Package Control.sublime-package';ipp=sublime.installed_packages_path();urllib.request.install_opener(urllib.request.build_opener(urllib.request.ProxyHandler()));open(os.path.join(ipp,pf),'wb').write(urllib.request.urlopen('http://sublime.wbond.net/'+pf.replace(' ','%20')).read())
    • Sublime 2

    import  urllib2,os;pf='Package Control.sublime-package';ipp=sublime.installed_packages_path();os.makedirs(ipp)ifnotos.path.exists(ipp)elseNone;urllib2.install_opener(urllib2.build_opener(urllib2.ProxyHandler()));open(os.path.join(ipp,pf),'wb').write(urllib2.urlopen('http://sublime.wbond.net/'+pf.replace(' ','%20')).read());print('Please restart Sublime Text to finish installation')
  • Install the plugin. Open a command panel by the Package Control Preference menu or shortcut keys Ctrl + Shift + P, enter the Install Package panel can be found in the tools installed plug-ins, and then look for plug-ins required. We recommend that you install the following plug-ins:

    • SublimeCodeIntel - automatic code completion tool plug.

    • Emmet - front-end development code templates plugins.

    • Git - version control tool plug-ins.

    • Python PEP8 Autoformat - PEP8 automatic formatting specification plug.

    • ConvertToUTF8 - convert a local encoded as UTF-8.

PyCharm - Python development Artifact

Used in this study is Anacond, which includes jupyter.

Exercise

1. Write print out "hello world"

print('hello world')

Python variable types

  • Integer: Python can handle any size integer (Python 2.x, there are two types int and long integer, but this distinction is of little significance for Python, only integer int in Python 3.x in this one kind of), but also support binary (eg 0b100, converted to decimal is 4), octal (eg 0o100, converted to decimal is 64), decimal ( 100) and hexadecimal ( 0x100converted to decimal 256) representation.

  • Floating-point type: decimal floating point is, is called float, because when expressed in terms of scientific notation, decimal point position a floating-point number is variable, floating point math in addition to the wording (such as 123.456addition) also supports scientific notation (such as 1.23456e2).

  • String: string is a single or double quotes any text, such as 'hello'and "hello", as well as the string representation of the original string, byte string representation, Unicode string representation, and can be written in a plurality of in rows (beginning with three single quotes, or three double quotes, three single or double quotation marks the end of three).

  • Boolean: Boolean only True, Falsetwo kinds of values, either True, or is False, in Python, can be directly used True, Falsea Boolean value indicating (note the case), can also be calculated by Boolean operators (e.g., 3 < 5generates a Boolean value True, and 2 == 1It will produce a Boolean value False).

  • Complex type: the form 3+5j, represents a complex mathematical with the same, the only difference is the imaginary part ireplaced j.

Variable naming

  For each variable we need to give it a name, just as each of us has his own famous name the same. In Python, variable naming need to follow these rules must comply with rigid and non-rigid rules is strongly recommended to follow.

  • Hard and fast rule:

    • Variable names consist of letters (generalized Unicode characters, not including special characters), numbers, and the underscore, numbers can not begin.

    • Case sensitive (uppercase aand lowercase Aare two different variables).

    • Do not tell keywords (words that have special meaning, will be mentioned later) and system reserved words (such as the name of the function, module, etc.) conflict.

  • PEP 8 requirements:

    • Lowercase spelling, multiple words connected with underscores.

    • Protected beginning single instance attribute underlined (will be mentioned later).

    • Private instance attribute start with two underscores (will be mentioned later). 

 Use variables

  • int (): converts a string or an integer value, can be specified band.

  • float (): converts a string to float.

  • str (): The specified object is converted into a string, the encoding can be specified.

  • chr (): to convert the integer to a character string (a character) corresponding to the code.

  • ord (): The character string (a character) into corresponding encoded (integer).

 Below with some examples of the variables used.

1. variables to store data and performing arithmetic operations below:

2. Use input () function input, as input () is the input character string, to perform arithmetic, type conversion is required, as shown in FIG.

3. You can use type () to see the type of a variable, as shown below:

4. formatted output% d, {}, as shown below:

5. Use str (), as shown below:

6. chr (), ord () encrypt e-mail address, as shown below:

 

7. Writing Calculator

Operators

 Python is support for multiple operators, as shown below, according to a priority list of all the operators in the end from the high order.

 

Note: [], [:] is a front closed after an interval apart; IS memory address is determined; number other than 0 means True; in particular is determined whether a character string in the string, as in FIG. below:

 

Exercises 

1. The conversion into Centigrade degrees Fahrenheit F = 1.8C + 32

F = input ( 'Enter Fahrenheit:')
C = (a float (F.) -32) * 1.0 / 1.8
Print ( '{:.. 2F = {}} degrees Fahrenheit' .format (C, F))

operation result:

 

2. Enter the radius of the circle and the perimeter area of ​​a circle calculated

Math Import
R & lt = a float (INPUT ( 'Enter radius of the circle:'))
Area ** 2 * = R & lt Math.PI
per Math.PI * = 2 * R & lt
Print ( 'radius% f, area% f, perimeter% f '% (r, area , per))

operation result:

 

3. Enter the year to determine whether a leap year

year = int(input('请输入年份:'))
if (year % 4 == 0 )and (year % 100 != 0 )or(year % 400 == 0):
  print('闰年')
else:
  print('平年')

运行结果:

 

4.判断是否为水仙花数

第一种:

num = input("请输入一个三位数:")
if len(num) > 3:
    print('输入有误')
else:
    num_1=int(num)
    a = int(num_1 % 100)
    b = a % 10 # 百位数
    c = int(a / 10) # 十位数
    d = int(num_1 / 100) # 个位数
    if (d ** 3)+(c ** 3)+(b ** 3) == num_1:
          print('水仙花数')
    else:
          print('不是水仙花数')

运行结果:

第二种:

num = input("请输入一个三位数:")
if len(num) > 3:
    print('输入有误')
else:
    b = int(num[0]) # 百位数
    c = int(num[1]) # 十位数
    d = int(num[2]) # 个位数
    if (d ** 3)+(c ** 3)+(b ** 3) == int(num):
         print('水仙花数')
    else:
         print('不是水仙花数')

运行结果:

 

5.实现一个正方形

for i in range(10):
    print('* ',end='')
print()
for k in range(8):
    print('* ',' '*14,'*',sep=" ")
for j in range(10):
    print('* ',end='')

运行结果:

 

 

 

 

 

 

-

Guess you like

Origin www.cnblogs.com/KAJIA1/p/11269624.html