Python Programming Essentials
1. Operators in Python
Operators in Python are special symbols that perform operations on values and variables, called operands. There are seven types of operators in Python:
- Assignment Operator
- Arithmetic Operator
- Logical Operator
- Comparison Operator
- Bitwise Operator
- Membership Operator
- Identity Operator
2. Data Types in Python
Python supports various data types. Type casting, or converting between these types, is done using functions like int(), float(), complex(), bool(), and str().
int(): Converts a value to an integer. For example,int(123.987)returns123.float(): Converts a value to a floating-point number. For example,float(10)returns10.0.complex(): Converts a value to a complex number. It can take one argument (real part) or two (real and imaginary parts). For example,complex(10)returns10+0jandcomplex(10,-2)returns10-2j.bool(): Converts a value to a boolean (TrueorFalse). For example,bool(0)returnsFalseandbool(10)returnsTrue.str(): Converts a value to a string. For example,str(10)returns'10'.
3. Python Identifier Naming Best Practices
Identifiers are user-defined names for variables, functions, classes, modules, etc. Follow these guidelines:
- Use lowercase (a-z), uppercase (A-Z), digits (0-9), or underscores (_). Examples:
shapeClass,shape_1,upload_shape_to_db. - Don’t start with a digit.
0Shapeis invalid, butshape1is valid. - Avoid reserved keywords.
- Don’t use special characters like ‘.’, ‘!’, ‘@’, ‘#’, ‘$’, ‘%’.
- Capitalize class names; use lowercase for others.
- A leading underscore signifies a private identifier.
- Two leading underscores indicate a strongly private identifier.
- Two leading and trailing underscores denote special names.
- Use meaningful names (e.g.,
iter,index) instead of single characters (e.g.,i). - Use underscores for readability:
count_no_of_letters. - Avoid leading and trailing underscores.
4. Python Data Structures
Strings
- Sequences of Unicode characters declared using single, double, or triple quotes.
- Immutable (cannot be changed after creation).
- Accessed using square brackets.
- Can be multiplied by integers for repetition.
Lists
- Mutable, ordered collections of items in square brackets, separated by commas.
- Can store various data types.
- Elements are accessed and modified by index.
- Support slicing, appending, extending, inserting, removing, popping, clearing, sorting, and reversing.
Tuples
- Similar to lists, but immutable.
- Suitable for fixed data.
- Allow duplicates and heterogeneous objects.
- Support positive and negative indexing.
Dictionaries
- Use curly braces
{}(empty braces create an empty dictionary, not a set; useset()for an empty set).
Sets
- Unordered collections of unique elements (no duplicates).
- Mutable (elements can be added or removed).
- Represented using curly braces
{}. - Support set operations (union, intersection, difference).
5. Type Casting in Python
Type casting converts a value from one data type to another. Python provides built-in functions for this:
int(): Converts to integer.float(): Converts to floating-point.complex(): Converts to complex number.bool(): Converts to boolean.str(): Converts to string.
6. Python Features, Advantages, and Applications
Features/Characteristics
- Interpreted: Executed line by line.
- Dynamically Typed: Data types are inferred.
- High-Level: Easier to write complex programs.
- General-Purpose: Wide range of applications.
- Object-Oriented: Supports OOP concepts.
- Portable: Runs on different operating systems.
Advantages
- Simple and Easy to Learn: Concise and readable syntax.
- Extensive Libraries: Pre-built modules for various tasks.
- Open Source: Large and active community.
- Versatile: Web development, scripting, data analysis, ML, AI, etc.
- Efficient: Text processing, integration capabilities, unit testing.
Applications
- Desktop Applications
- Web Applications
- Database Applications
- Network Programming
- Game Development
- Data Analysis
- Machine Learning
- Artificial Intelligence
- Internet of Things (IoT)
Examples: Google, YouTube, Dropbox, Raspberry Pi, EVE Online, BitTorrent, Industrial Light & Magic, Pixar, ESRI, IronPort email server, Maya, NSA, iRobot.
IDEs
- PyCharm
- Sublime Text
- Wing
- Atom
7. Keywords and Identifiers
Keywords
- Reserved words with specific meanings.
- Cannot be used as identifiers.
- Case-sensitive.
View keywords using:
import keyword
print(keyword.kwlist)
Identifiers
- User-defined names for variables, functions, classes, etc.
- Used to reference entities in code.
Example:
test = 10
test is the identifier, 10 is the value.
