Python 1st Day
- Basic Rules
- Always use the extension .py.
- Always use
#!/usr/bin/env python
as shabang line.
- Variable
- Naming rule:
- Names must start with a letter or an underscore.
- Names are case sensitive.
- Can be any (reasonable) length.
- Don't use reserved words.
- Avoid using the lowercase letter ‘l’, uppercase ‘O’, and uppercase ‘I’. Because the l and the I look a lot like each other and the number 1. And O looks a lot like 0.
- When using CamelCase names, capitalize all letters of an abbreviation (e.g. HTTPServer).
- Memory Management
- When you do an variable assignment in Python, it tags the value with the variable name.
- if you change the value of the varaible, it just changes the tag to the new value in memory, you don't need to do the housekeeping job of freeing the memory here. Python's Automatic Garbage Collection does it for you. When a value is without names/tags it is automatically removed from memory.
- Assigning one variable to another makes a new tag bound to the same value.
- A value will have only one copy in memory and all the variables having this value will refer to this memory location, for example when you have variables
a,b,chaving a value 10, it doesn't mean that there will be 3 copy of10s in memory. There will be only one10and all the variablesa,b,cwill point to this value. - Once a variable is updated, say you are doing
a += 1a new value11will be allocated in memory andawill be pointing to this. id()will return an objects memory address (object's identity).
- Multiple Assignment
- You can assign a single value to more than one variables simultaneously, like x = y = z = 1
- The variables assign many values at the same time: x, y, z = 1, 2, "abcd"
- Naming rule:
- Data Type
- Numbers
- Numeric objects are immutable, which means when an object is created its value cannot be changed.
- Python has three distinct numeric types: integers, floating point numbers, and complex numbers.
- Boolean (bool)
- Strings
- Strings start and end with single or double quotes Python strings are immutable.
- Strings are arrays of characters and elements of an array can be accessed using indexing. Indices start with 0 from left side and -1 when start from right side.
- Strings are immutable character sets. Once a string is generated, you can not change any character within the string.
- Numbers
- if statement
- for loop
- range() function
- range(a) : Generates a sequence of numbers from 0 to a, excluding a, incrementing by 1.
- range(a,b) : Generates a sequence of numbers from a to b excluding b, incrementing by 1.
- range(a,b,c) : Generates a sequence of numbers from a to b excluding b, incrementing by c.
- range() function
- break & continue
- The break statement is used to exit a for or a while loop. The purpose of this statement is to end the execution of the loop (for or while) immediately and the program control goes to the statement after the last statement of the loop.
- The continue statement is used in a while or for loop to take the control to the top of the loop without executing the rest statements inside the loop. For instance,
-
>>> for i in range(6): ... if (i == 3 or i == 6): ... continue ... print(i) ... 0 1 2 4 5

浙公网安备 33010602011771号