Python development,

Source: Internet
Author: User

Python development,
I. Variables

  • Variable amount during program execution;
  • Defining a variable is accompanied by three features: Memory ID, data type, and variable value.
  • Before running other languages, you must manually release the program's memory space. However, the python interpreter comes with a memory reclaim mechanism. Once the python program runs, the memory space is automatically released.
age=10print(id(age),type(age),age)
View Code

 

  1. Constant
  • It refers to the amount that is unchangeable during program execution;
  • Constants are generally defined with uppercase letters.
AGE=10print(AGE)
View Code 3. variable naming
  • Camper
AgeOfOldboy=72
View Code
  • Underline
age_of_oldboy=72
View Code 2. Interaction with programs
In ancient times, when we went to the bank to get the money, we needed a bank clerk waiting for us to input his account and password to him, and then he went to verify the result, let's enter and tell him the withdrawal amount. Proud modern people will provide customers with an ATM (that is, a computer), so that the ATM can interact with users and replace manpower. However, machines are dead and we must write programs for them to run. This requires that our programming language can have a mechanism to interact with users and receive user input data.
  • Python3 supports UTF-8 Chinese encoding by default. Python2 needs to be added #-*-coding: UTF-8-*-in the Code header.
  • In python3, no matter what type of input value is saved as the str (string) Type
name=input('please enter the username: ')print(id(name),type(name),name)
View Code

 

  1. Python2
  • Raw_input in python2 is the same as input in python3;
name=raw_input('please enter the username: ')print(id(name),type(name),name)
View Code

 

  • Input in python2. A value must be input. The type of the value will be saved as the type.
name=input('please enter the username: ')print(id(name),type(name),name)
View Code 3. Data Type
  • It is generally used to define the age, ID card number, QQ number, grade, etc.
age=18id=130530198805240011qq=379048558level=99
View Code

 

  1. Float
  • It is generally used to define height, weight, salary, etc.
height=1.81height=float(1.81) 
View Code

 

  1. Str string type
  • It is generally used to define the name, gender, and status of a person;
  • Generally, strings are placed in single, double, and triple quotes.
name='egon'sex='female'age=18 
View Code

 

  • Concatenate strings with "+"
Name = 'egon' sex = 'female 'age = 18 print (name + sex + str (age) Note: The age variable here is 18, if it is an int integer, it cannot be concatenated into strings. You must use str (age) to convert it to the string type.
View Code

 

  • Concatenate strings "*"
name='egon'print(name*10)
View Code

 

  1. Boolean bool
  • Only True and False values are supported;
  • Mostly used for judgment.
age=73AGE=18print(age < AGE)print(age > AGE)
View Code

 

  1. Conversion Between Different Types
  • Integer --> floating point type
a=18print(float(a))
View Code

 

  • Floating Point --> integer
a=1.81print(int(a))
View Code

 

  • Float Type --> string type
a=1.81print(str(a))
View Code

 

  • Integer --> string type
a=18print(str(a))
View Code

 

Iv. array type
  • List in python, defined in [], separated by commas;
info=['egon','alex',18]print(info[2])
View Code

 

  • The element can be any data type or any array type;
  • The character element must be enclosed by quotation marks. quotation marks are not required for integer, floating point, and list.
info=[13,18.1,'alex',['egon','tony']]print(info[3][0])
View Code

 

  1. Dictionary {}
  • The dictionary in python, also called an associated array, is defined in {}. Its elements are represented by the Project name: project content format, and separated by commas;
info={'name':'egon','sex':'male',3:18}print(info[3])
View Code

 

  • The project content can be any data type or any array type;
  • The string type in the project content needs to be enclosed by quotation marks, and no quotation marks are required for integer, floating point, and list types.
Info = {'name': 'aggene', 'Gender ': 'male', 'mymy': ['you ', 'none']} print (info ['mymy'] [1]) info = {'name': 'agents', 'Gender ': 'male', 'mymy ': 123} print (info ['mysql']) info = {'name': 'aggene', 'Gender ': 'male', 'mysql ': 18.1} print (info ['mysql']) info = {'name': 'aggene', 'Gender ': 'male', 'mysql ': 'none'} print (info ['mymy'] [1])
View Code

 

5. format the output
  • My name is xxx, my age is xxx
  • Use placeholder % s
name=input('user_name>>: ')age=input('user_age>>: ')print('my name is %s, my age is %s' %(name,age))
View Code

 

Vi. Operators
  • + -*/
 
Print (5 + 5) #5 plus 5 equals 10 print (5-5) #5 minus 5 equals 0 print (5*5) #5 multiplied by 5 equals 25 print (5/2) #5 divide by 2 equals 2.5
View Code

 

  • Evaluate the quotient integer part // calculate the quotient remainder Part % power **
Print (5 // 2) #5 divided by 2 vendors equals 2 more than 1, only take 2 print (5% 2) #5 divided by 2 vendors equals 2 more than 1, take the remainder 1 print (3 ** 2) # The Power of 3 is 3 multiplied by 3 equals to 9
View Code

 

  1. Comparison Operators
  • ><>==! =
print(30 > 20)print(30 < 20)print(30 >= 30)print(30 <= 30)print(30 == 30)print(30 != 40)
View Code

 

  1. Logical operators
  • Logical and logical or logical non-not bitwise and & bitwise or |
  • Logic and, all conditions must be filled before True is returned;
  • Logical or, the result is True if only one condition is met;
  • If the logic is not, the result is reversed.
name='egon'age=18print(age > 15 and name == 'egon')print(age > 15 or name != 'egon')print(not age > 15)
View Code

 

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.