Python learning --- chapter 1, python learning --- Section

Source: Internet
Author: User

Python learning --- chapter 1, python learning --- Section

Run the first program hello_world.py

#_*_coding:utf-8_*_print("Hello world!")

Output result:

Hello world!

Variable

#_*_coding:utf-8_*_name = "beyoungt"

 

#_*_coding:utf-8_*_name = "beyoungt"print(name)name = “abby"print(name)

Output result:

beyoungtabby

You can change the value of a variable in a program at any time. python always records the latest value of the variable.

String:

In python, all enclosed in quotation marks are strings. It can be either a single quotation mark or a double quotation mark.

"This is a string."'This is also a string.'

How to modify the case sensitivity of a string:

name = "beyoungt"print(name.title())  

Output:

Beyoungt

Title (): change the first letter of each word to uppercase.

Upper (): converts all strings to uppercase.

Lower (): change all strings to lowercase.

String concatenation:

first_name = "tian"last_name = "beyoungt"full_name = first_name + " " + last_nameprint("Hello," + full_name.title+" ! " )

Output:

Hello,Tian Beyoungt!

 

Formatted output of the string:

Name = "beyoungt" print ("my name is % s" % name) # output: my name is beyoungt

% S: String % d: integer % f: Floating Point

name = input("name:")age = int(input("age:") ) #integerjob = input("job:")salary  = input("salary:")info = '''-------- info of  %s  -----Name:%sAge:%dJob:%sSalary:%s''' % (name,name,age,job,salary)info2 = '''-------- info of {_name}  -----Name:{_name}Age:{_age}Job:{_job}Salary:{_salary}'''.format(_name=name,           _age=age,           _job=job,           _salary=salary)info3 =  '''-------- info of {0} -----Name:{0}Age:{1}Job:{2}Salary:{3}'''.format(name,age,job,salary)print(info3)

Add a tab: \ t

Add line break: \ n

Print ("ages: \ n \ tPython \ nC \ n \ tJavaScript") Output: Ages: PythonC JavaScript

Delete blank:

Favorite_language = "python" favorite_language.rstrip () # Delete blank spaces at the end of favorite_language.lstrip () # Delete blank spaces at the beginning of favorite_language.strip () # Delete blank spaces

 

Module:

#!usr/bin/env python#-*- coding:utf-8-*-# Author burnywenimport getpass_username = 'burnywen'_password = '123456'username = input("username:")#password = getpass.getpass("password:")  #fails to work well in pycharm;password = input("password:")if _username == username and _password == password:    print("Welcome user {name} login...".format(name=username))else:    print("Invalid username or password!")

The getpass module hides the password.

Loop:

#!usr/bin/env python#-*- coding:utf-8-*-# Author burnywencount = 0while True:    print("count:",count)    count +=1    if count == 100:        break
#!usr/bin/env python#-*- coding:utf-8-*-# Author burnywenfor i in range(0,10):    if i <3:        print("loop ",i)    else :        continue    print("oh....")

 

#!usr/bin/env python#-*- coding:utf-8-*-# Author burnywenfor i in range(10):    print('----------',i)    for j in range(10):        print(j)        if j >5:            break

Guess games and improvements:

#!usr/bin/env python
#-*- coding:utf-8-*-
# Author burnywen 
age_of_burnywen = 23for i in range(3):    guess_age = int(input("guess age:") )    if guess_age == age_of_burnywen :        print("congratulations, you are right. ")        break    elif guess_age > age_of_burnywen:        print("think smaller...")    else:        print("think bigger...")else:    print("you have tried too many times.")
 

 

#!usr/bin/env python#-*- coding:utf-8-*-# Author burnywen
age_of_burnywen = 23count = 0while count <3:    guess_age = int(input("guess age:") )    if guess_age == age_of_burnywen :        print("congratulations, you are ringht. ")        break    elif guess_age > age_of_burnywen:        print("think smaller...")    else:        print("think bigger!")    count +=1else:    print("you have tried too many times.")
 

 

#!usr/bin/env python#-*- coding:utf-8-*-# Author burnywenage_of_buruywen =23count = 0while count <3:    guess_age = int(input("guess age:") )    if guess_age == age_of_burnywen :        print("congratulations, you are right. ")        break    elif guess_age > age_of_burnywen:        print("think smaller...")    else:        print("think bigger!")    count +=1    if count == 3:        countine_confirm = input("do you want to keep guessing..?")        if countine_confirm != 'n':              count =0else:    print("you have tried too many times.")

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.