2 hours for python BASICS (1) and 2 hours for python Basics

Source: Internet
Author: User

2 hours for python BASICS (1) and 2 hours for python Basics

2 hours of learning python BASICS (1)

Python is a dynamic language with no type identifiers. All types are runtime identifiers.


Let's take a look at the basic data types of python.
There are integers, same as java int such as 5
Floating Point number, such as 5.54523
There are strings, and there are minor differences with java strings. You can use single quotes such as 'abc' "abc ".
There is a Boolean value, which must be either True or False for java, but uppercase is required.
Boolean values can be involved in logical operations and are the same as java & or and java | not is the same as java!
The null value is None.
Features are the same as short-circuit operations


Variable assignment is very simple. It does not need to be declared. You can directly use the variable after it is written.
Eg: a = 1 a = 'abc'
Students who have studied java may ask if a is not an integer or a value-based string, and an error is reported. But as mentioned above, this is a dynamic language.
Type runtime identification, so this is very correct


All annotations in java are //
Python annotation is #


In java, if the character string needs to be escaped, add \
Python also supports this method, but provides a simpler method.
Add the r before the string
Eg:

a=r'"fjkkfkfk\fmer'
If you print a, it will display "fjkkfkfk \ fmer
By the way, let's talk about how to print python.
No parentheses
Print the stuff you want to print.
Separate multiple contents. A space is displayed during printing.
Back to the just-mentioned escape, r''escape can only escape a single row, while multiple-row escape requires r''''', which means multiple rows in python.
''''' Will be used frequently in the future.


Java often encounters garbled errors. The common modification method is to change to UTF-8.
In python, you can add u before the string.
Eg: u''' hello
Hi '''
If the Chinese character string encounters UnicodeDecodeError In the Python environment, this is because the format of the. py file is incorrect. You can add comments in the first line.
# -*- coding: utf-8 -*-

Python's simple integer and floating-point operations are the same as java's.
The result of two integer operations is an integer.
The integer and floating-point operations first Replace the integer with a floating-point number, and then perform operations


In python, Boolean values and other types are allowed for logical operations.
In python, 0, none, ''is regarded as false.
Other values and non-null strings are considered as true.
Eg: a = 'python'
Print 'hello', a or 'World'
The output is 'hello' python.
World short-circuited


In the java Collection framework, there is a type called list
Python also has a list, and the list is the same.
List ordered set, allowing you to delete and modify elements at any time
Usage example
Eg: a = ["fhjhef", 2222, True]
OK. We can see different elements in a list.
Because python is a dynamic Language
In fact, java can also (without generics), but Java is prone to errors because it is a static language.
The dynamic use of python demonstrates its flexibility.
Query:
We can access data by index, for example, a [0]
Remember, as long as the computer language index is almost 0
Python has more Nb and can be accessed in reverse order. For example, a [-1] is the first last element.
Add:
Two methods
1. a. append ('fhjjj ')
This method can only be added to the end of the list.
2. a. insert (1, 'krkfk ')
This method inserts an element to any position. The first parameter is the location index.
Delete:
Delete two reloads of a method.
A. pop (): Delete the last element and print it.
A. pop (-2), delete the last and second elements, and print
Change:
Super simple
A [-1] = 5423
The above are the basic operations for adding, deleting, modifying, and querying the list.


Another Data Structure in python is tuple.
It is also an ordered list
This is similar to an array, but the elements in the array can be modified.
T = ('fjkjkfjk ', True, 5523)
What can we do? We can only search through indexes.
The search method is the same as the list method.
Eg:
T [-2]
Nothing else can be done
Note: When creating a single element, a = (5)
Does it feel like a = 5? parentheses are processed as priorities?
So to avoid the problem, when creating a single element
A = (5,) Add at the end, so there is no ambiguity
Tuple definition is immutable, but cannot be changed?
If you add a list to the tuple, write a post on the list.
Then everything is possible, because the reference list has not changed.
Eg:
t=[5,6]a=(4,5,t)t[0]=1a(4,5,[1,6])


Let's talk about some simple conditional loop statements.
For more information, see eg:
age = 20if age >= 18:    print 'your age is', age    print 'adult'print 'END'
We can see that the if statement does not have any parentheses.
Mode:
If condition:
Execution statement
Other statements
In python, empty spaces and tabs are especially important. They are equivalent to braces in java.


Let's look at if-else.
if age >= 18:    print 'adult'else:    print 'teenager'


The mode is
If condition:
Execution statement
Else:
Execution statement


What if there are multiple if else instances?
if score>=90:    print 'excellent'elif score>=80:    print 'good'elif score>=60:    print 'passed'else:    print 'failed'


OK, simple and clear
The mode is
If condition:
Execution statement
Elif condition:
Execution statement
Elif condition:
Execution statement
Else:
Execution statement
Note the General indent. press TAB.


Then there is a loop. first look at the for loop.
Eg:
L = ['Adam', 'Lisa', 'Bart']for name in L:    print name
The mode is
For variable name (assign the value of this position to this variable) in SET (list, tuple, others ):
Execution statement
The basic situation is similar to that of foreach in java.


While Loop
Eg:
while x<100:    sum+=x    x=x+2
Mode
While execution conditions:
Execution statement
If you want to jump out of the loop, you can use break. This is the same as java.
Both while and for can be used.
Eg:
sum = 0x = 1while True:    sum = sum + x    x = x + 1    if x > 100:        breakprint sum

If you call a loop, continue is also the same as java
Nested loop:
for x in ['A', 'B', 'C']:    for y in ['1', '2', '3']:        print x + y


Note the indentation, and emphasize again that the indentation here is the same as {} in java {}

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.