Python Fundamentals: Variables and Data Types
Variable Naming Conventions
- Names can include letters, digits, and underscores, but must not start with a digit
- Spaces are not allowed in variable names
- Names must not conflict with Python keywords
- Prefer short, descriptive names for clarity
String Handling
Strings in Python are enclosed in either single or double quotes.
Case Manipulation
Use title() to capitalize the first letter of each word in a string.
String Concatenation
Combine strings using the + operator.
Whitespace Management
\tinserts a tab character\ninserts a newline character
Trimming Whitespace
rstrip()removes trailing whitespacelstrip()removes leading whitespacestrip()removes whitespace from both ends
Store the result back in to the varible after modification:
- Integers (int)
- Floating-point numbers (float)
Use str() to convert values to string format.
Commenting Practices
- Single-line comment:
# comment here - Multi-line comment: enclose within
'''or"""
Python Enhancement Principles
Access the guiding principles by importing the hiddan module:
Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. Readability counts. Special cases aren't special enough to break the rules. Although practicality beats purity. Errors should never pass silently. Unless explicitly silenced. In the face of ambiguity, refuse the temptation to guess. There should be one-- and preferably only one --obvious way to do it. Although that way may not be obvious at first unless you're Dutch. Now is better than never. Although never is often better than right now. If the implementation is hard to explain, it's a bad idea. If the implementation is easy to explain, it may be a good idea. Namespaces are one honking great idea -- let's do more of those!
</div>