All Python source code files use the extension . py. When you run a. py file, Python automatically creates the appropriate. pyc file. The. pyc file contains the target code.
Look at one of the following simple programs:
# name.pyname=input ('What'syour first name? ' )print('+name.capitalize () +'! ')
The first line is the source code comment, and thepython comment always begins with the sign # and continues to the end of the line.
The second line invokes function input, which is a standard built-in function for reading strings from the keyboard. When this line of code executes, it will display in the Output window what is your first name and the blinking cursor. The program waits for the user to enter a string and press ENTER. The function input returns the string entered by the user, letting the variable name finally point to the string that the user entered .
The third line shows a greeting.
Python provides a method for converting the casing to a string object : Upper () and lower ().
More than that, Python also gives us the initials, the rest of the lowercase capitalize () method ,
and all words capitalized, the remaining lowercase title () method .
is your first name?wei bo Liuhello wei Bo liu! #capitalize () Hello Wei Bo liu! #upper () Hello Wei Bo liu! #lower () Hello We I Bo liu! #title ()
The function strip () deletes whitespace characters at the beginning and end of the string, as follows:
' Oven '. Strip ()'oven'
reading numbers from the keyboard
function input simply returns a string, so if you need a number, you must use one of the Python numeric conversion functions.
# age.pyage=input ('How old is youtoday? ' ) Age10=int (age) +10print("+str (age10) +' years old. ')
Assuming that the user enters 24, the variable age points to the string ' 24 ' because Python does not automatically convert a string that looks like a number to an integer or floating point, and does not automatically convert integers or floating-point numbers to strings . If you want to use a string for arithmetic operations, you must first convert it to a number.
In the print statement, you must convert the variable AGE10 to a string, or Python will display an error message stating that the number and string cannot be added.
Print a string on the screen
The print statement is a standard built-in function for printing strings to the screen.
You can pass any number of strings to print:
Print ('jack','ate','no',' fat') jack ate no fat
By default, print prints each string in the standard output window and separates them with a space. Modifying the string delimiter is easy and can be done as follows:
Print ('jack','ate','no',' fat',sep= '. ' ) Jack.ate.no.fat
by default, print automatically adds a line break after the specified content is printed: \ n. to print all text on the same line, specify the ending character of the first line as an empty string :
Print (', end= ') Print ('no fat')
>>> Jack ate no fat
2015-06-22 23:37:54
Python Source code interpretation