Home Backend Development Python Tutorial An interpreted language--an introduction to python

An interpreted language--an introduction to python

Jun 27, 2017 am 09:54 AM
python

Python is an interpreted language

Choice of python 2 or 3:

Python 2.7 is the latest and last version of 2, and the update supports Updates will stop in 2020, but companies that are currently using or have developed it continue to use python2, so the transition period for updates still has a way to go, and python 3 will be the long-term development in the future. A better choice (this is only for beginners), of course, when learning python3, you must understand the different features between 2 and 3.

Installation and configuration

windows

##1. Download the installation package

https://www.python.org/ downloads/2
、Install
Default installation path :C:\pythonXX3
、Configure environment variables
[Right-click computer] --》[Properties]--》[Advanced system settings] --》[Advanced]--》[Environment Variables]--》[Find the line with the variable name Path in the second content box, double-click it] --> [The Python installation directory is appended to the variable value, separated by;] ​​
For example: original value;C :\pythonXX, remember to have a semicolon in front ##linux, Mac

No installation required, original Python environment

ps: If you have
2.6
, please update to 2.7 or other versions
First program
1 print 'Hello World!'    #python2的写法2 print ('Hello World!')   #python3的写法
Copy after login

Declare variables
name='Tom'
Copy after login

Rules for variable definition:

Variable names can only be any combination of letters, numbers or underscores
    • The first character of the variable name cannot be a number

    • The following keywords cannot be declared as variable names

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

    • Assignment of variables
name=      =name    (name2)
Copy after login

Character encoding

should be displayed at the beginning of the file to tell the python interpreter what encoding to use to execute the source code, that is:

#!/usr/bin/env python# -*- coding: utf-8 -*-  #告诉python字符编码
  print "你好,世界"
Copy after login

 

注释

  当行注视:# 被注释内容

  多行注释:""" 被注释内容 """

数据类型初识

1、数字

2 是一个整数的例子。
长整数 不过是大一些的整数。
3.23和52.3E-4是浮点数的例子。E标记表示10的幂。在这里,52.3E-4表示52.3 * 10-4。
(-5+4j)和(2.3-4.6j)是复数的例子,其中-5,4为实数,j为虚数,数学中表示复数是什么?。

int(整型)

  在32位机器上,整数的位数为32位,取值范围为-2**31~2**31-1,即-2147483648~2147483647
  在64位系统上,整数的位数为64位,取值范围为-2**63~2**63-1,即-9223372036854775808~9223372036854775807
long(长整型)
  跟C语言不同,Python的长整数没有指定位宽,即:Python没有限制长整数数值的大小,但实际上由于机器内存有限,我们使用的长整数数值不可能无限大。
  注意,自从Python2.2起,如果整数发生溢出,Python会自动将整数数据转换为长整数,所以如今在长整数数据后面不加字母L也不会导致严重后果了。
float(浮点型)
      先扫盲  
  浮点数用来处理实数,即带有小数的数字。类似于C语言中的double类型,占8个字节(64位),其中52位表示底,11位表示指数,剩下的一位表示符号。
complex(复数)
  复数由实数部分和虚数部分组成,一般形式为x+yj,其中的x是复数的实数部分,y是复数的虚数部分,这里的x和y都是实数。
注:Python中存在小数字池:-5 ~ 257
 
2、布尔值
  真或假
  1 或 0
3、字符串
"hello world"

 

接收用户输入

#!/usr/bin/env python#_*_coding:utf-8_*_
 
 #name = raw_input("请输入用户名") #只有 python 2.x有这种写法name=input('请输入用户名:')print('Hello '+name)
Copy after login

 

if else ...

#!/usr/bin/env python# -*- coding: encoding -*-age=int(input('请输入年龄:'))        #接收一个age值,这里因为age是一个int类型,所以需要强制转换一下if age<100:       print('你还小')    #判断age的值小于100的时候输出“你还小”else:print('你已经老了')   #否则输出“你已经老了”
Copy after login

if elif else ...

#!/usr/bin/env python# -*- coding: encoding -*-my_age=22           #定义一个自己的年龄age=int(input('请输入年龄'))  #接收一个年龄if age==my_age:print('猜对了')   #如果输入的值等于my_age的值 输出猜对了elif age<my_ageprint('猜小了')    #输入的值小于my_age的值,输出才小了else:print('猜大了')     #负责显示猜大了
Copy after login

 

 while 循环 ...

#!/usr/bin/env python# -*- coding: encoding -*-count=0         #定义一个值为count的计数器while True:       print(count)  #如果为真就一直循环 这是一个死循环,不停止程序会一直执行count+=1    #循环一次给计数器+1
Copy after login

 

for 循环 ... 

#!/usr/bin/env python# -*- coding: encoding -*-for i in range(10)print(i)                #最简单的循环打印10次
Copy after login

 

 

The above is the detailed content of An interpreted language--an introduction to python. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

Python vs. JavaScript: Community, Libraries, and Resources Python vs. JavaScript: Community, Libraries, and Resources Apr 15, 2025 am 12:16 AM

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

Detailed explanation of docker principle Detailed explanation of docker principle Apr 14, 2025 pm 11:57 PM

Docker uses Linux kernel features to provide an efficient and isolated application running environment. Its working principle is as follows: 1. The mirror is used as a read-only template, which contains everything you need to run the application; 2. The Union File System (UnionFS) stacks multiple file systems, only storing the differences, saving space and speeding up; 3. The daemon manages the mirrors and containers, and the client uses them for interaction; 4. Namespaces and cgroups implement container isolation and resource limitations; 5. Multiple network modes support container interconnection. Only by understanding these core concepts can you better utilize Docker.

How to choose the PyTorch version on CentOS How to choose the PyTorch version on CentOS Apr 14, 2025 pm 06:51 PM

When installing PyTorch on CentOS system, you need to carefully select the appropriate version and consider the following key factors: 1. System environment compatibility: Operating system: It is recommended to use CentOS7 or higher. CUDA and cuDNN:PyTorch version and CUDA version are closely related. For example, PyTorch1.9.0 requires CUDA11.1, while PyTorch2.0.1 requires CUDA11.3. The cuDNN version must also match the CUDA version. Before selecting the PyTorch version, be sure to confirm that compatible CUDA and cuDNN versions have been installed. Python version: PyTorch official branch

How to run programs in terminal vscode How to run programs in terminal vscode Apr 15, 2025 pm 06:42 PM

In VS Code, you can run the program in the terminal through the following steps: Prepare the code and open the integrated terminal to ensure that the code directory is consistent with the terminal working directory. Select the run command according to the programming language (such as Python's python your_file_name.py) to check whether it runs successfully and resolve errors. Use the debugger to improve debugging efficiency.

Python: Automation, Scripting, and Task Management Python: Automation, Scripting, and Task Management Apr 16, 2025 am 12:14 AM

Python excels in automation, scripting, and task management. 1) Automation: File backup is realized through standard libraries such as os and shutil. 2) Script writing: Use the psutil library to monitor system resources. 3) Task management: Use the schedule library to schedule tasks. Python's ease of use and rich library support makes it the preferred tool in these areas.

Is the vscode extension malicious? Is the vscode extension malicious? Apr 15, 2025 pm 07:57 PM

VS Code extensions pose malicious risks, such as hiding malicious code, exploiting vulnerabilities, and masturbating as legitimate extensions. Methods to identify malicious extensions include: checking publishers, reading comments, checking code, and installing with caution. Security measures also include: security awareness, good habits, regular updates and antivirus software.

How to install nginx in centos How to install nginx in centos Apr 14, 2025 pm 08:06 PM

CentOS Installing Nginx requires following the following steps: Installing dependencies such as development tools, pcre-devel, and openssl-devel. Download the Nginx source code package, unzip it and compile and install it, and specify the installation path as /usr/local/nginx. Create Nginx users and user groups and set permissions. Modify the configuration file nginx.conf, and configure the listening port and domain name/IP address. Start the Nginx service. Common errors need to be paid attention to, such as dependency issues, port conflicts, and configuration file errors. Performance optimization needs to be adjusted according to the specific situation, such as turning on cache and adjusting the number of worker processes.

See all articles