Concise python tutorial Swaroop, C. H. Chapter 4th Basic Concepts

Source: Internet
Author: User
Tags natural string

Is it enough to just print "Hello world"? You should want to do more-you want to get some input, and then do the operation,
And then get some output from it. In Python, we can use constants and variables to do these things.
Constants in literal sense
An example of a literal constant is a number like 5, 1.23, 9.25e-3, or a
String ', ' It's a string! '. They are called literal, because they have a literal meaning
Meaning-you use their values in their literal sense. The number 2 always stands for itself, and not something else--it is a
Constant, because its value cannot be changed. Therefore, all of these are called literal constants.

Number
There are 4 types of numbers in Python-integers, long integers, floating-point numbers, and complex numbers.
2 is an example of an integer.
Long integers are just larger integers.
3.23 and 52.3E-4 are examples of floating-point numbers. The e tag represents a power of 10. Here, 52.3E-4 means 52.3 * 10-4.
( -5+4j) and (2.3-4.6j) are examples of complex numbers.


string
A string is a sequence of characters. A string is basically a group of words.
I can almost guarantee that you will use the string in every Python program, so please pay special attention to the following section. Let me tell you now.
How to use strings in Python.
Use single quotation marks (')
You can use single quotation marks to indicate a string, just like ' Quote me on this '. All whitespace, that is, spaces and tabs
Are kept as they are.
Use double quotation marks (")
The string in double quotes is exactly the same as the string used in single quotes, such as "What's your name?".
Use three quotation marks ("' or" ")
Using the three quotes, you can indicate a multiline string. You can use single and double quotation marks freely in three quotes. For example:
"This is a multi-line string. This is the first
Line.
The second line.
"What ' s your name?," I asked.
He said "Bond, James bond."
‘‘‘
Escape Character
Suppose you want to include a single quotation mark (') in a string, how do you indicate the string? For example, this string
What ' s your name? You're definitely not going to use ' what ' your name? ' to indicate it, because Python will
I can't figure out where this string begins and where it ends. So, you need to specify the single quotation mark instead of the end of the string. Can pass
The escape character to complete this task. You use \ ' to indicate single quotes--notice the backslash. Now you can represent the string
For ' what\ ' your name? '.
Another way to represent this particular string is "What's your name?", that is, double quotes. Similarly, in a double
You can also use the escape character when using the double quotation mark itself in a quoted string. Alternatively, you can use the escape character \ \ to indicate the backslash
Itself.
One thing to note is that in a string, a single backslash at the end of the line indicates that the string continues on the next line, not the
Start a new row. For example:
"This is the first sentence.\
This is the second sentence. "
Equivalent to "This is the first sentence." This is the second sentence. "
Natural String
If you want to indicate certain strings that do not require special handling such as escape characters, then you need to specify a natural string. Self -
string is specified by prefixing the string with R or R. For example R "Newlines is indicated by
\ n ".
Unicode string
Unicode is the standard method of writing international texts. If you want to write text in your native language, such as Hindi or Arabic, then you
A Unicode-enabled editor is required. Similarly, Python allows you to work with Unicode text-you just need to use the character
Precede the string with the prefix u or U. For example, U "This is a Unicode string.".
Remember to use Unicode strings when you are working with text files, especially if you know that the file contains a language other than English
Write the text.
The string is immutable
This means that once you create a string, you can no longer change it. Although this looks like a bad thing, it actually
No. We will see later in the program why we say it is not a disadvantage.
String literals by literal meaning
If you put two strings next to each other literally, they will be automatically connected by Python. For example, ' what\ '
' Your name ' will be automatically converted to ' What ' your name? '.
comments to C/A + + programmers
There is no dedicated char data type in Python. There really is no need to have this type and I'm sure you won't do it
and trouble.
comments to the perl/php programmer
Remember, single and double quote strings are exactly the same-they are not different in any way.
Comments to the regular expression user
Be sure to use a natural string to process the regular expression. Otherwise, you will need to use a lot of back slashes. For example, a back reference
can be written as ' \\1 ' or R ' \1 '.


Variable
Just using literal constants can quickly cause trouble-we need a way to store information and manipulate it.
Method. This is why you introduce variables. Variables are what we want-their values can change, i.e. you can use variables to store
Any thing. Variables are just a portion of your computer's memory that stores information. Unlike literal constants, you need to be able to visit
Ask these variables for the method, so you give the variable a name.

Name of the identifier
Variables are examples of identifiers. An identifier is a name used to identify something. When naming identifiers, you should follow these rules:
The first character of an identifier must be a letter in the alphabet (uppercase or lowercase) or an underscore (' _ ').
Other parts of the identifier name can consist of a letter (uppercase or lowercase), an underscore (' _ '), or a number (0-9).
The identifier name is case-sensitive. For example, MyName and myname are not an identifier. Note that the former lowercase n and
The latter in uppercase N.
Examples of valid identifier names are I, __my_name, name_23, and A1B2_C3.
Examples of invalid identifier names are 2things, this was spaced out, and my-name.

Data type
Variables can handle different types of values, called data types. The basic types are numbers and strings, and we've talked about them. In the back
Chapters, we'll look at how to create our own types using classes.

Object
Remember that Python calls anything that is used in the program as an object. This is broadly speaking. So we're not going to say, "xxx.
", we say" an object ".
Annotations to object-oriented programming users
For everything, including numbers, strings, and even functions, Python is extremely completely oriented to
of the object.
We'll look at how to use variables and literal constants. Save the following example, and then run the program.


How to write a python program
The following are standard procedures for saving and running Python programs.
1. Open your favorite editor.
2. Enter the program code in the example.
3. Save it as a file using the file name given in the note. I routinely use all the Python programs to
extension. py save.
4. Run the interpreter command Python program.py or use the idle run program. You can also use the previous interface
of the executable method.
Example 4.1 using variables and literal constants
# Filename:var.py
i = 5
Print I
i = i + 1
Print I
s = ' ' This is a multi-line string.
The second line.
Print S
(source file: code/var.py)
Output
$ python var.py
56
This is a multi-line string.
The second line.


How it works
Let's explain how this program works. First we use the assignment operator (=) to assign a literal constant of 5 to the variable
Volume I. This line is called a statement. Statement declarations need to do something, in this place we connect the variable name I with the value 5. Pick up
Down, we print the value of I with the print statement, that is, the value of the variable is printed on the screen.
Then we add 1 to the value stored in I and then save it back to I. When we print it, we get the expected value of 6.
Similarly, we assign a literal string to the variable s and print it.
Comments to C/A + + programmers
You only need to assign a value to a variable when you use it. You do not need to declare or define a data type.

logical lines and physical rows
Physical lines are what you see when you write your program. The logical line is a single statement that Python sees. Python assumes that each physical row corresponds to a
logical line. An example of a
logical line is a statement like print ' Hello world '-if it is a line (as you would see in the editor
), it is also a physical line.
By default, Python wants to use only one statement per line, which makes the code easier to read.
If you want to use more than one logical line in a physical row, you need to use a semicolon (;) to specifically indicate this usage. A semicolon
represents the end of a logical line/statement. For example:
i = 5
Print I
is the same as the following:
i = 5;
Print I; The
can also be written as:
i = 5; print i;
Can even be written as:
i = 5; Print I
However, I strongly recommend that you persist in writing only one logical line in each physical line. Write
A logical line in more than one physical line only when the logical line is too long. This is to avoid using semicolons as much as possible, making the code easier to read. In fact, I have never used or seen an excessive number in a Python
program.
Here is an example of writing a logical line in multiple physical rows. It is known as a clear row connection.
S = ' This was a string. \
This continues the string. '
Print S
Its output:
This is a string. This continues the string.
Similarly,
print \
I
has the same effect as the following:
Print I
Sometimes, there is a hint of the hypothesis that you do not need to use backslashes. This situation occurs when parentheses, Fang Kuo
, or curly braces are used in logical lines. This is referred to as the implied line connection. You'll see this usage in the sections that explain how to use the list later.

indent
Whitespace is important in python. In fact the gap at the beginning of the line is important. It is called indentation. Whitespace (Spaces and tab
characters) at the beginning of a logical line is used to determine the indentation level of logical lines, which is used to determine the grouping of statements.
This means that statements at the same level must have the same indentation. Each set of such statements is called a block. We'll see in a later chapter the
example of the use of blocks.
One thing you need to remember is that the wrong indentation throws an error. For example:
i = 5
print ' Value is ', I # error! Notice a single in
The start of the line
print ' I repeat, the value is ', I
When you run this program, you get the following error:
File "Whitespace.py", line 4
print ' Value is ', I # error! Notice A single space
at the start of the line
^
Syntaxerror:invalid syntax
Note that there is a space at the beginning of the second row. This error, indicated by Python, tells us that the syntax of the program is invalid, that the program is not being
correctly written. It tells you that you cannot arbitrarily start a new block of statements (except, of course, the main block you have been using). When you can use the
new block, it will be described in detail in later chapters, such as control flow.
How to Indent
Do not mix tabs and spaces to indent because this does not work when crossing different platforms.
I strongly recommend that you use a single tab or two or four spaces at each level of indentation.
Select one of these three indentation styles. More importantly, choose a style, and then consistently use it, that is, just make
in this style.

Summarized
Now that we have learned a lot of detail, we can begin to learn something more interesting to you, such as control flow statements. In the following
Before continuing your study, make sure you are clear about the content of this chapter.

Concise python tutorial Swaroop, C. H. Chapter 4th Basic Concepts

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.