python entry (1) - the first py program, comments, variable, keyword

1. Use Pycharm the first to write a python program

  • 1. Open Pycharm, select Create New Project, create a new project
    Here Insert Picture Description

  • 2. Select the Pure Python represents the creation of a pure Python program projects, Location represent the path to save the project, Interpreter expressed using a version of the Python interpreter, and finally click Create to create the project.
    Here Insert Picture Description

  • 3. Right-click the project, select New, then select Python File
    Here Insert Picture Description

  • File name HelloPython 4. In the dialog box, enter, click on OK, means creating a Python program text files, text files default extension .py
    Here Insert Picture Description

  • 5. Enter the following code, and right space, select Run to run, the print a character string "Hello World!".

print(“Hello World!”)

Here Insert Picture Description

After the successful operation, Pycharm Console window will display our output.
Here Insert Picture Description

2. Comment

1. The role comment

  • Note: for text explanation of the program code in the program code.

  • Role: Comments are not program, can not be executed, but the program code explanation, so that others can understand the role of program code, can greatly enhance the readability of the program.

2. Classification comments

<1> single line comment

Beginning with #, # right of all text as a description, not a real program to be executed, play a supporting role in explanation

# Is a comment I can write some functional description like oh in
print ( 'hello world')

<2> multi-line comments

'''
    我是多行注释,可以写很多很多行的功能说明

    下面的代码完成 ,打印一首诗
    名字叫做:春江花月夜
'''


print('╔═╤═╤═╤═╤═╤═╤═╤═╤═╤═╤═╤═╤═╤═╤═╤═╤═╤═╤═╤═╗')
print('║ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │  ║')
print('║ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ║')
print('║ │春│滟│江│空│江│江│人│不│白│谁│可│玉│此│鸿│昨│江│斜│不│ ║')
print('║ │江│滟│流│里│天│畔│生│知│云│家│怜│户│时│雁│夜│水│月│知│ ║')
print('║ │潮│随│宛│流│一│何│代│江│一│今│楼│帘│相│长│闲│流│沉│乘│ ║')
print('║ │水│波│转│霜│色│人│代│月│片│夜│上│中│望│飞│潭│春│沉│月│ ║')
print('║ │连│千│绕│不│无│初│无│待│去│扁│月│卷│不│光│梦│去│藏│几│ ║')
print('║春│海│万│芳│觉│纤│见│穷│何│悠│舟│徘│不│相│不│落│欲│海│人│ ║')
print('║江│平│里│甸│飞│尘│月│已│人│悠│子│徊│去│闻│度│花│尽│雾│归│ ║')
print('║花│,│,│,│,│,│,│,│,│,│,│,│,│,│,│,│,│,│,│ ║')
print('║月│海│何│月│汀│皎│江│江│但│青│何│应│捣│愿│鱼│可│江│碣│落│ ║')
print('║夜│上│处│照│上│皎│月│月│见│枫│处│照│衣│逐│龙│怜│潭│石│月│ ║')
print('║ │明│春│花│白│空│何│年│长│浦│相│离│砧│月│潜│春│落│潇│摇│ ║')
print('║ │月│江│林│沙│中│年│年│江│上│思│人│上│华│跃│半│月│湘│情│ ║')
print('║ │共│无│皆│看│孤│初│望│送│不│明│妆│拂│流│水│不│复│无│满│ ║')
print('║ │潮│月│似│不│月│照│相│流│胜│月│镜│还│照│成│还│西│限│江│ ║')
print('║ │生│明│霰│见│轮│人│似│水│愁│楼│台│来│君│文│家│斜│路│树│ ║')
print('║ │。│。│。│。│。│?│。│。│。│?│。│。│。│。│。│。│。│。│ ║')
print('║ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ║')
print('║ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ║')
print('╚═╧═╧═╧═╧═╧═╧═╧═╧═╧═╧═╧═╧═╧═╧═╧═╧═╧═╧═╧═╝')

<3> python program, Chinese support

python3, the default is to support Chinese, but in the python2 need some additional operations can

python2, if used directly on the Chinese in the program, such as

print ( 'Hello')

If you run directly output, the program will go wrong:

Chinese error

The solution is: write the following code at the beginning of the program, which is the Chinese comments

# coding = utf-8
after revision procedure:

Coding. 8 = UTF-#
Print ( 'Hello')

operation result:

Hello there

Note: The
recommended way to use the python syntax specification of:

#-- coding:utf-8 --

3. Variables

In Python, a data storage, requires a thing called variable, the following example:

num1 = 100 # num1 is a variable, like a small basket
num2 = 87 # num2 is a variable
result = num1 + num2 # and the data accumulated num1 "basket" in the two num2, then put the result variable

  • Description:
    • The so-called variable, can be understood as basket, if you need to store more data, the easiest way is to have a number of variables, of course you can also use a
    • Procedure is used to process the data, and the variable is used to store data

3.1 Types of variables

In order to more fully utilize the memory space, and more efficient memory management, there are different types of variables, as follows:
Here Insert Picture Description

The 4.python keywords

4.1 What is the key

python some of the special features of the identifier, which is called keyword
keyword, python is already in use, so own definition of the same name as a keyword does not allow developers identifier

4.2 View Keywords:

and     as      assert     break     class      continue    def     del
elif    else    except     exec      finally    for         from    global
if      in      import     is        lambda     not         or      pass
print   raise   return     try       while      with        yield

It can be viewed in the Python Shell python current system by the following command keywords
Here Insert Picture Description

import keyword
keyword.kwlist

5. Output

5.1 normal output

For example Printing tips:

print('hello world')
print('萨瓦迪卡---泰语,你好的意思')

5.2 formatted output

Objective <1> formatting operation

For example, we have the following code:

pirnt("我今年10岁")
pirnt("我今年11岁")
pirnt("我今年12岁")
    ...

Think about it:

When the output of age, with a number of "I am xx years old," about the program can simplify it? ? ?

answer:

String formatting

<2> What is Formatting

Look at the following code:

age = 10
print("我今年%d岁" % age)

age += 1
print("我今年%d岁" % age)

age += 1
print("我今年%d岁" % age)

...

In the program, such as% saw operator, which is in Python formatted output.

age = 18
name = "xiaohua"
print("我的姓名是%s, 年龄是%d" % (name, age))

<3> common format symbols

Here is the complete, it can use the% symbol list:
Here Insert Picture Description

5.3 Wrap output

On output, if there \ n So, at this time \ n after the content is displayed in another row

print("1234567890-------") # 会在一行显示

print("1234567890\n-------") # 一行显示1234567890,另外一行显示-------

6. Enter (input)

input () function and raw_input () (python2 version, not the 3) is similar, but it must be accepted input expression.

>>> a = input() 
123
>>> a
123
>>> type(a)
<type 'int'>
>>> a = input()
abc
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 1, in <module>
NameError: name 'abc' is not defined
>>> a = input()
"abc"
>>> a
'abc'
>>> type(a)
<type 'str'>
>>> a = input()
1+3
>>> a
4
>>> a = input()
"abc"+"def"
>>> a
'abcdef'
>>> value = 100
>>> a = input()
value
>>> a
100

input () accepts input expression and the result of the expression is assigned to the left operand

8. The data type conversion

8.1 common data type conversion

Here Insert Picture Description

8.2 Examples

>>> # int(): 将数据转换为 int 类型
... 
>>> str1 = "10"
>>> # int() 默认按10进制转换后显示
... num1 = int(str1)
>>> 
>>> # int() 处理浮点数,只留下整数部分,舍弃小数部分(并不是四舍五入操作)
... num2 = int(3.74)
>>> print(num2)
3
>>> 
>>> """
... num1 = int(str1, 8) # 第二个参数为8,表示按8进制转换后显示,结果为 8
... num1 = int(str1, 16) # # 第二个参数为16,表示按16进制转换后显示,结果为 16
... #01 02 03 04 05 06 07 10
... #01 02 ... 0B 0C 0D 0E 0F 10
... print(num1)
... """
>>> 
>>> # float() 将数据转化为浮点数
... str2 = "3.14"
>>> f1 = float(str2)
>>> print(type(f1))
<class 'float'>
>>> 
>>> f2 = float(10)
>>> print(f2)
10.0
>>> 
>>> # complex() 创建复数: 第一个参数是复数的实部,第二个参数是复数的虚部
... c1 = 10 + 4j
>>> c2 = complex(10, 4)
>>> 
>>> print(c1)
(10+4j)
>>> print(c2) # 等同与c1
(10+4j)
>>> 
>>> # str() : 转换为 字符串类型
... num1 = 10
>>> f1 = 3.14
>>> 
>>> print(type(str(num1)))
<class 'str'>
>>> print(type(str(f1)))
<class 'str'>
>>> 
>>> # repr(): 转换为表达式字符串
... num1 = 10
>>> print(type(repr(num1)))
<class 'str'>
>>> 
>>> 
>>> # eval(): 将字符串形式的数据,转换为原本的类型
... str1 = "3.14"
>>> print(type(eval(str1)))
<class 'float'>
>>> 
>>> str2 = "[10, 20, 30]"
>>> l = eval(str2)
>>> print(type(l))
<class 'list'>
>>> 
>>> 
>>> # chr: 将一个整数转换为对应的 Unicode 字符
... s = chr(1065)
>>> print(s)
Щ
>>> 
>>> # ord :将一个字符转换为对应的字符编码数字
... n = ord("A")
>>> print(n)
65
>>> 
>>> # bin: 将一个整数转换为二进制
... print(bin(1024)) # 0b 开头表示二进制数
0b10000000000
>>> 
>>> # oct:将一个整数转换为八进制
... print(oct(1024)) # 0o 开头表示八进制数
0o2000
>>> 
>>> # hex: 将一个整数转换为十六进制
... print(hex(1024)) # 0x 开头表示十六进制
0x400
>>>

8.3 commonly used characters with ASCII code table

Here Insert Picture Description

9. summary

Here Insert Picture Description

Published 122 original articles · won praise 1 · views 7306

Guess you like

Origin blog.csdn.net/qq_36079912/article/details/105038999