The role of Python variables and variables, constants, user interaction, annotations

Source: Internet
Author: User
Tags constant definition naming convention

Hello World Proficient in all languages
    • C++
#include <iostream>
int main (void)
{
std::cout<< "Hello World";
}
    • C
#include <stdio.h>
int main (void)
{
printf ("\nhello world!"); \ nthe line break
return 0;
}
    • Java
public class helloworld{
Entrance to the program
public static void Main (String args[]) {
Output information to the console
System.out.println ("Hello world!");
}
}
    • Php
<?php
echo "Hello world!";
?>
    • Ruby
Puts "Hello world."
    • GO
Package Main
Import "FMT"
Func Main () {
Fmt. Printf ("Hello world!\n God Bless you!");
}



variables


one of the main functions of the computer:Calculations, it's easy to do data in Python, as simple as we normally do with computers.


In total consumption, the direct use of the previously calculated intermediate results, so as to avoid the re-calculation of all the data.Incorrect wording:
>>> print (' Eat ', 10+15+7+4+7+3)
Eat 46
>>> print (' cloth ', 20)
Cloth 20
>>> print (' Traffic ', 6+6+6+6+6)
Traffic 30
>>> print (' Spirit ', 300+300+400+200)
Spirit 1200
>>>
>>>
>>> print (' Total consumption ', 46+20+30+1200)
Total consumption 1296
So the reason is not correct, because the final count of total consumption is the human flesh to the previous calculation of the classification results filled in, but we put the program in the script to run, the computer will certainly not know in advance to eat, traffic, buy clothes 3 of the results of the classification, so this result is our own dynamic calculation, Not the computer.

the correct wordingis, directly to each classification results first name to save, and then calculate the total consumption, just need to save a few names before the call on it.
>>> eat = 10+15+7+4+7+3
>>> cloth = 20
>>> traffic = 6+6+6+6+6
>>> spirit=300+300+200+400
>>>
>>> total = eat + cloth + traffic + Spirit
>>> print (' Total consumption ', totals)
Total consumption 1296
    • Variables: Eat, cloth, traffic, Spirit, total this is the function of the program operation of the intermediate results temporarily stored in memory, you can let the following code continue to call, these are called "variables".
    • The role of variables:
Variables is used to store information to is referenced and manipulated in a computer program. They also provide a labeling data with a descriptive name, so we programs can be understood more clearly by the RE Ader and ourselves. It's helpful to think of variables as containers, that's hold information. Their sole purpose is to label and store data in memory. This data can and is used throughout your program. (variables are used to store information that you want to reference and manipulate in your computer programs.) They also provide a way to tag data with descriptive names so that the reader and ourselves can understand our program more clearly. It is helpful to treat variables as containers that contain information. Their sole purpose is to mark and store data in memory. The data can then be used throughout the program).

    • Declaring variables:
Name = "Alex Li"
Variable name (identifier): Name
Variable Value: Alex Li
    • Rules for defining variables:
① variable names can only be any combination of letters, numbers, or underscores.
The first character of a ② 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
    • Variable naming habits:
① Hump Body
Ageofoldboy = 56
Numberofstudents = 80
② Underline
Age_of_oldboy = 56
Number_of_students = 80
The official recommendation is to use the second naming convention, which looks more clear.
    • Define the low mode of the variable:
The ① variable is named Chinese and pinyin.
② variable name is too long.
③ variable nouns are not expressive.
    • Example:
Age_of_oldboy = 56
Oldboy_gf_name = ' Lisa '
    • To modify the value of a variable:
>>>age_of_oldboy = 56 # old boy's age is 56
>>>oldboy_gf_age = #老男孩女朋友的年龄为54
>>>age_of_oldboy + oldboy_gf_age # old boy's age with old boy girlfriend's age is 110
110 results
>>>oldboy_gf_age = #修改老男孩女朋友的年龄为55
>>>age_of_oldboy + oldboy_gf_age #修改后老男孩的年龄与老男孩女朋友的年龄为111
111 Results after the modificationConstants
    • constants:
Constants are constant quantities, for example, pai=3.141592653, or quantities that do not change during a program's operation. In Python, there is no specific syntax for constants, which are used to represent constants in all uppercase names. For example:
Age_of_oldboy = 56
In the C language there is a special constant definition syntax, const int count = 60; Once defined as a constant, the change will be an error.


User Interaction
Name = input ("What is Your Name:")
Print ("Hello" + name)
After executing the script, the Discovery program will wait for the name to go down.

Let the user enter multiple information:

Name = input ("What is Your Name:")
Age = Input ("What old is You:")
Hometown = input ("Where is your Hometown:")
Print ("Hello", Name, "Your is", age, "years old, you came from", hometown)

Execution output:
What is your Name:wu Qianqian
How old is you:21
Where is your Hometown:shijiazhuang
Hello Wu Qianqian Your is years old and you came from Shijiazhuang


Notes
Code comments are divided into single-line and multiline comments. Single-line comments with #, multiline comments can be three pairs of double quotation marks "" "" ".
Cases:
def subclass_exception (name, parents, module, Attached_to=none):
"""
Create exception subclass. Used by modelbase below.

If ' attached_to ' is supplied, the exception would be a created in a-a-
Allows it to be pickled, assuming the returned exception class would be added
As an attribute to the ' Attached_to ' class.
"""
Class_dict = {' __module__ ': module}
If attached_to is not None:
def __reduce__ (self):
# Exceptions is Special-they ' ve got state that isn ' t
# in self.__dict__. We assume it is all in Self.args.
Return (Unpickle_inner_exception, (attached_to, name), Self.args)

def __setstate__ (self, args):
Self.args = args

class_dict[' __reduce__ '] = __reduce__
class_dict[' __setstate__ '] = __setstate__

return type (name, parents, class_dict)
    • Guidelines for code annotations:
① does not need to annotate all, just need to comment on the part that you feel important or not understand.
② comments can be in Chinese or English, but never use pinyin.



The role of Python variables and variables, constants, user interaction, annotations

Related Article

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.