Python first day, python first day

Source: Internet
Author: User

Python first day, python first day

I. python's first line of code:

1 name = "Hello, world" 2 print (name)

2. variables:

1 name = "SunDM12"2 name2 = name3 print("my name : ",name, name2)4 5 name = "wangba"6 print(name, name2)

Name changes before and after, while name2 = name has assigned "SunDM12" to name2. After the name changes, name2 will not change.

3. Interaction:

1 username = input("username : ")2 print(username)

Input Function: You can display input characters on the interface and assign values to username.

 1 name = input("name :") 2 age = input("age :") 3 job = input("job :") 4 salary = input("salary :") 5  6 info = ''' 7 ------ info of %s ------ 8 name : %s 9 age : %s10 job : %s11 salary : %s12 '''%(name, name, age, job, salary)

The first format to print on the screen.

% S is the string; % d is the double precision; % f is the floating point type

1 info2 = '''2 ------- info of {_name} -------3 name : {_name}4 age : {_age}5 job : {_job}6 salary : {_salary}7 '''.format(_name=name,8                _name=name,_age=age,_job=job,_salary=salary)        

The second format for printing on the screen.

1 info3 = '''2 ------ info of {0} ------3 name : {0}4 age : {1}5 job : {2}6 salary : {3}7 '''.format(name,age,job,salary)

The third format for printing on the screen.

Iv. logon password:

 1 import getpass 2  3 _username = 'SunDM12' 4 _password = '123456' 5  6 username = input('username :') 7 password = input('password :') 8 print(username,password) 9 10 if _username == username and _password == password11     print("welcome user {name} login...".format(name = username))12 else:13     print("Invalid username or password!")

Getpass is a package that provides portable mask input.

1. getpass. getpass ()

2. getpass. getuser ()

5. Password-based game

1 correct_number = 122 guess_number = int(input("guess number:"))3 4 if correct_number == guess_number:5     print("yes,you got it...")6 elif guess_number >correct_number:7     print("think smaller...")8 else:9     print("think bigeer...")

The input function is a character, which must be forcibly converted to an integer.

5.1 while LOOP

1 count = 02 while True:3     print("count :",count)4     count = count +15     if count == 1000:6         break

Break indicates that the entire loop exists.

correct_number = 12count = 0while count<3:    guess_number = int(input("guess number:"))    if correct_number == guess_number:        print("yes,you got it...")    elif guess_number >correct_number:        print("think smaller...")    else:        print("think bigeer...")    count + = 1else:    print("you have tried too many times.")

Use the while loop to design a guess digital game

5.2 for Loop

1 for i in range(10):2     print("loop ",i)

Show 1 to 10

1 for i in range(0,10,2):2     print("loop :",i)

2 is the step size

correct_number = 12for i in range(3):    guess_number = int(input("guess number:"))    if correct_number == guess_number:        print("yes,you got it...")    elif guess_number >correct_number:        print("think smaller...")    else:        print("think bigeer...")else:    print("you have tried too many times.")

Use the for loop to guess digital games

5.3 difference between continue and break

 1 for i in range(10): 2     if i<5: 3         print("loop",i) 4     else: 5         continue 6     print("....") 7  8 for i in range(10): 9     print('-------',i)10     for j in range(10):11         print(j)12         if j>5:13             break

Continue ignores the current statement and continues executing the next line

Break jumps out of the current execution cycle

 

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.