Python Learning notes-(ii) Getting started with Python

Source: Internet
Author: User

1. First Python program

1.1 Direct Print out
    • Open cmd and enter python into the Python interactive environment: (see >>> in the Python Interactive environment:)

    • Enter the following code in the Python Interactive environment: print ("hello,world!")

1.2 Running the file output
    • Create a new TXT text document and change the suffix to. py. Open Edit document Contents with notepad++ and Save:

#!/usr/bin/env python-----Declaring the type of interpreter to use

Print "hello,world!"

    • Open cmd, enter into the Python interactive environment, run the. py file (as shown):

2. Data types and Variables2.1 Data Types

2.1.1 Integer

Python can handle integers of any size, and the representation in the program is exactly the same as the mathematical notation.

2.1.2 Floating point

Floating-point numbers, which are decimals, are called floating-point numbers because, when represented by scientific notation, the decimal position of a floating-point number is variable, for example, 1.23x109 and 12.3x108 are equal. Floating-point numbers can be written in mathematical notation, such as 1.23,3.14,-9.01, and so on. But for very large or very small floating-point numbers, it must be expressed in scientific notation, the 10 is replaced with E, 1.23x109 is 1.23e9, or 12.3e8,0.000012 can be written 1.2e-5, and so on.

Integers and floating-point numbers are stored inside the computer in different ways, and integer operations are always accurate (is division accurate?). Yes! ), and the floating-point operation may have rounding errors.

2.1.3 String

The string is any text enclosed in "or";

1) Inside the string contains ‘ and contains",可用转义字符\来标识;如:I‘m “OK”! 在python中应输入print("I\‘m \“OK\”!")

2) The escape character \ itself can also be escaped; print ("\ \")

2.1.4 Boolean value

A Boolean value is exactly the same as a Boolean algebra, with a Boolean value of only True False two values, either, True or, False in Python, you can directly use True and False represent a Boolean value (note case). It can also be computed by Boolean operations. Boolean values can be used and , or and not operations.

2.1.5 Null value

A null value is a special value in Python, None denoted by. Nonecannot be understood as 0 , because 0 it is meaningful, and None is a special null value.

2.2 Variables2.2.1 What is a variable

Variables are used to store all kinds of identifiers that need to be temporarily saved while the program is running, a variable should have a name, and a storage unit in memory that holds the value of the variable.

2.2.2 Variable naming conventions
    • Consists of numbers, letters and underscores;
    • Cannot be the beginning of a number;
    • The variable name cannot be the same as the Python keyword, system function name, and class name;
2.2.3 variable Command habits
    • cannot be Chinese or pinyin;
    • Concise and clear, such as: Ageofuser, age_of_user, etc.;
The representation of the 2.2.4 variable in computer memory

When we wrote: Name= "CC", the Python interpreter did two things:

1) Create a ' CC ' string in memory;

2) Create a variable named name in memory and point it to ' cc '.

This time the variable name is assigned to another variable name1, this operation is actually the variable name1 pointer to the variable name point to the data, and the variable name is not any relationship; If the name value changes, the value of name1 remains unchanged;

Cause: string attribute, once modified, re-created

The value of name changes, which is equivalent to re-opening a piece of memory space, while the name1 still points to the original memory space.

2.3 Constants

Quantities are immutable variables, such as the usual mathematical constants π is a constant.

In Python, constants are typically represented in all uppercase variable names: such as ABC

3. String Formatting
    • String%s
Print ("hello,%s"% "world")
    • Integer%d
Print ("My age is%d"% 20)
    • Floating point%f
Print ("My age is%f"% 20)

3.1 Format output

1 name = input ("What's your Name?") 2 age = Int (input ("Tell me to Age:")) 3 job = input ("Job?") 4 salary = input ("salary!") 5  6 info = "7----info of%s------8 name:%s 9 age:%d10 job:%s11 salary:%s12" "% (NA me,age,job,salary) print (info) Info2 = "----info of {_name}------name:{_name}19 age:{_age}20 job:{_job }21 salary:{_salary}22 ". Format (_name=name,23            _age=age,24            _job=job,25            _salary=salary) print ( Info2)  : Info3 = "----info of {0}------name:{0}36 age:{1}37 job:{2}38 salary:{3}39". Fo Rmat (name,age,job,salary) max print (INFO3)

  

4 cipher Ciphertext input

Import getpass  password = getpass.getpass (' Enter password: ')  

PS: The file cannot be run directly in the interpreter, it needs to run a. py file to see the effect

Python Learning notes-(ii) Getting started with Python

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.