Getting started: Python variables, get values, standard output, loop judgment statements

Source: Internet
Author: User

A variable is made up of letters, numbers, and underscores, and numbers do not begin.

There is no constant in Python, the variable name is generally capitalized when used as a constant, and you should not change its value after that.

Variable command: Use an underscore to separate it.

Character encoding adds a line at the beginning of the code

1 # -*-coding:utf-8-*-
gets the value entered by input:

Normal input:

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

Ciphertext output such as:

1 Import Getpass 2 3 Password = getpass.getpass ("Password:")
several ways of standard output:

The first type:

 1  info2 = "   2  -------------info of {_name}---------  3  name:{_name}  Span style= "COLOR: #008080" > 4  age:{_age}   5  job:{_job}   6  salary:{_salary}   7  . Format (_name=name,   8  _age=age,   9  _job=job,  10  _salary=salary) 

The second type:

1 " " 2 --------------------Info 0f%s--------------- 3 name:%s 4 age:%d 5 job:%s 6 salary:%s 7 "% (name,name,age,job,salary)
If-else Cycle

Guess Age:

1Age_of_oldboy = 562 3guess_age = Int (input ("Guess Age:"))4 ifGuess_age>Age_of_oldboy:5     Print("your guess is older.")6 elifguess_age==Age_of_oldboy:7     Print("You are right!")8 Else:9     Print("your guess is younger.")

While loop

On the basis of guessing the age of control only let guess three times:

1  whileCount<3:2guess_age = Int (input ("Guess Age:"))3     ifGuess_age>Age_of_oldboy:4         Print("your guess is older.")5     elifguess_age==Age_of_oldboy:6         Print("You are right!")7          Break8     Else:9         Print("your guess is younger.")Ten  OneCount = Count + 1 A     Print("You have {_count} times.". Format (_count=3-count)) - #if Count = = 3: - Else: the     Print("sorry,you try too many times")

For loop:
# #基础格式
1 for in range (1,10,2):2 print(" ", i)

Write a guess password with a For loop:

1Age_of_oldboy = 562 3  forIinchRange (3):4guess_age = Int (input ("Guess Age:"))5     ifGuess_age>Age_of_oldboy:6         Print("your guess is older.")7     elifguess_age==Age_of_oldboy:8         Print("You are right!")9          BreakTen     Else: One         Print("your guess is younger.") A  -     Print("You have {_count} times.". Format (_count=2-i)) - #if Count = = 3: the Else: -     Print("Fuck")

Getting started: Python variables, get values, standard output, loop judgment statements

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.