The path to learning in Python--day1

Source: Internet
Author: User

The variable definition of Python and the print function

 1  print  (  " hello World   " )  2  hello World  3  4  name = "  wei Chao   " 5  print  ("  my name is   " , name)  6  My name is  Wei Chao 

As the code shows, the variable definition in Python does not need to declare the type of the variable beforehand, the variable is directly assigned to the row, and Python automatically determines it. second, the naming of variables in Python is also subject to the following rules :

    1. Variable names can contain only letters, numbers, and underscores. Variable names can begin with a letter or underscore, but not with a number.
    2. Variable names cannot contain spaces, but you can use underscores to separate words in them.
    3. Do not use Python keywords and function names as variable names, that is, do not reserve words for special purposes with Python.
    4. Variable names should be both brief and descriptive.
    5. Use the lowercase l and the capital Letter o sparingly, as they may be mistaken for numbers 1 and 0. For now, you should use lowercase letters to name python variable names best.

Then, theprint function is not the same in Python2 and 3, in 2 The print function does not add (), and 3 must be added (). The specific differences are not discussed in this article, only the application of the print function in 3:

1. Numeric, string, variable direct output

1 Print(1) #数值213   4 Print("Hello World") #字符串5 Hello World6   7x = 128 Print(x) #int变量912Ten    Ones ='Hello'#string变量 A Print(s) - Hello -    theL = [1, 2,'a'] #列表变量 - Print(L) -[1, 2,'a']   -  +t = (1, 2,'a') #元组变量 - Print(t) +(1, 2,'a')  A   atD = {'a': 1,'b': 2} #字典变量 - Print(d) -{'a': 1,'b': 2}

2. Formatted output

Here are the main three ways, the first way is similar to C's printf:

1Name = input ("Name:")2age = Int (input ("Age :"))3Job = input ("Job:")4salary = Int (input ("Salary:"))5 6Info1 =" "7 ----------Info of%s----------8 Name:%s9 Age :%dTen Job:%s One Salary:%d" "%(name, name, age, job, salary) #第一种方式 A Print(INFO1) -  -Info2 =" " the ----------info of {_name}---------- - Name: {_name} - Age : {_age} - Job: {_job} + Salary: {_salary}" ". Format (_name=name, -_age=Age , +_job=Job, A_salary=salary) #第二种方式 at Print(Info2) -  -Info3 =" " - ----------Info of {0}---------- - Name: {0} - Age : {1} in Job: {2} - Salary: {3}" ". Format (name, age, job, salary) #第三种方式 to Print(INFO3)

Second, if statement and Loop statement

1Age_of_rookie = 242Count =03 4  whileCount < 3:5guess = Int (input ("Age :"))6     ifGuess = =Age_of_rookie:7         Print("good, you guessed it!")8          Break9     elifGuess >Age_of_rookie:Ten         Print("Sorry, the age should is younger.") One     Else: A         Print("Sorry, The age should is older.") -Count + = 1 -     ifCount = = 3: theContinue_confirm = input ("want to keep guessing?") -         ifContinue_confirm! ='N' orContinue_confirm! ='N': -Count =0 -  + #For i in range (0,10,2): - #Print ("loop", i) + #Else: A #print ("You have tried too many times ...")

As shown in the code above, the conditional judgment expression of the IF statement in Python is directly written, not as required in C (), and must be added ': ' At the endof the expression. Else statement in the same vein, but the else if statement in C becomes a elif statement in Python, with the same syntax. The while loop format in Python is also like an if statement, which is no longer mentioned. The For loop does not need to be bracketed like C, with initial conditions or conditional judgments or end conditions, as shown in the code, to make a for loop directly with the range function, a variable, and in keywords ( Remember to add a colon!). ).

In addition, the code block in Python is managed by indentation (preferably indenting four spaces), which means the end of the code block when the code block encounters a line of code that does not have the same amount of indentation. So, in Python hollow lattice is very important! If your code doesn't run as you expect, the problem may be indented.

Iii. input function and coercion type conversion

1 name = input ("name:")2 age = Int (input ("Age:  "))3 job = input ("job:")4 salary = Int (input ("Salary:"))

Input function usage as shown in the previous code, it is recommended to add a hint to the input in parentheses to facilitate code debugging. Forcing a type conversion in Python is like calling a function, with the direct type name in parentheses, as shown in the second line of code.

The path to learning in Python--day1

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.