Python _ basic syntax,

Source: Internet
Author: User

Python _ basic syntax,
I. Variables

Variables are used to store intermediate results during program operation, and to facilitate future calls, operations, or changes to programs. It also provides a way to mark descriptive data for a clearer understanding. It is easier to understand variables as a container for saving information. Variables are stored in the memory and will apply to the entire program.

 

Declare Variables

 

#_*_coding:utf-8_*_name = "Imomy Mu"

 

Naming rules for variables:
  • To be descriptive
  • Variable names can contain only _, numbers, and letters. They cannot contain spaces or special characters (#? <., ¥ $ *!~)
  • Cannot use Chinese as the variable name
  • Cannot start with a number
  • Reserved characters cannot be used
Variable assignment
name = "Imomy Mu" name2 = nameprint(name,name2) name = "Jack"print("What is the value of name2 now?")

 

Ii. user input

Use the input command.

 

name = input("What is your name?")print("Hello " + name )

 

 

All the data accepted by input is a string. Even if you input a number, it is still processed as a string. However, sometimes we need to input the data information, or an integer. In this case, we need to use data type conversion to convert the input data.

Int integer = integer converts a string to int and uses int (converted data)
Str string = convert data to a string using str (converted data)

Assume that a person can live for 80 years and determine how many years the user can live.

death_age = 80 name = input("your name:")age = input("your age:") print("Your name:",name)#print("You can still live for ",  death_age - int(age)," years ....")print("You can still live for " +  str(death_age - int(age)) +" years ....")

 

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.