Python Programming Introduction Class
Course Overview
This four-lesson series provides a structured introduction to Python programming, starting from absolute basics and progressing to practical scientific computing applications. The course is designed for beginners who want to build a solid foundation in Python programming with a focus on applications in data analysis and visualization.
Each lesson builds on previous concepts, offering hands-on examples and practical exercises that reinforce learning. By completing this course, you will gain the skills needed to write Python scripts, organize code effectively, and create visualizations of scientific data. This course emphasizes hands-on learning with practical examples drawn from scientific computing. Each lesson includes:
- Clear explanations of core concepts
- Executable code examples you can run and modify
- Practical exercises to reinforce learning
- Real-world applications of programming concepts
- References to official documentation for further exploration
The course structure follows a logical progression from language fundamentals to applied scientific computing, ensuring you build a comprehensive understanding of Python programming.
Prerequisites and Tools
This course assumes no prior programming experience. The only requirements are:
- A computer with internet access
- Basic computer literacy (file management, web browsing)
- Ideally a small software project
Recommended tools: Miniconda or Mambaforge for environment management, a text editor (VS Code, Sublime Text, or similar), and a modern web browser for accessing Jupyter notebooks.
All Lesson Links
Access each lesson directly using the links below:
- Lesson 1: What is Python - Language fundamentals, modules, and environment setup
- Lesson 2: Python Structures - Core data types, objects, sequences, and mutability
- Lesson 3: Programming is Managing Information Flow - Functions, conditionals, loops, and control structures
- Lesson 4: NumPy and Plotting - Introduction to NumPy arrays and data visualization
- Exercise with solution
All lessons open in a new tab for convenient reference while working through the course material.
Data for exercise
Click here to download XRD_T.raw (5.24 MB)Click here to download TEM.7z (6.98 MB)
Lesson Content
Lesson 1: What is Python
Core Topic: Language fundamentals, modules, and environment setup
This foundational lesson establishes what makes Python a powerful programming language for rapid development and scientific computing. You'll learn about Python's interpreted, object-oriented nature with dynamic semantics, and how to set up your development environment using Miniconda or Mambaforge for effective package management.
Key concepts covered include:
- Understanding Python as an interpreted, high-level language
- Working with modules and packages (with hands-on
Torino.pyexample) - Setting up Python environments with Conda/Mamba
- Introduction to Jupyter Notebook and essential keyboard shortcuts
- Basic package installation and management
Lesson 2: Python Structures
Core Topic: Core data types, objects, sequences, and mutability
This lesson explores Python's core philosophy that "everything is an object." You'll learn about different data types, understand the concept of mutability, and work with Python's built-in data structures for effective data manipulation.
Key concepts covered include:
- Understanding mutable vs. immutable objects
- Working with standard data types: NoneType, Booleans, numbers, strings
- Sequence types: lists, tuples, and their operations
- Common operations like slicing, concatenation, and iteration
- Object identity vs. value equality
Lesson 3: Programming is Managing Information Flow
Core Topic: Functions, conditionals, loops, and control structures
Learn how to make your code dynamic and powerful with programming logic structures. This lesson covers the essential building blocks that allow you to create responsive, efficient programs that can make decisions and repeat operations.
Key concepts covered include:
- Defining functions with
defand lambda expressions - Using conditional statements:
if,elif,else - Implementing loops:
forandwhile - Control flow with
break,continue, andelseclauses - Iterating with
range()andenumerate() - Recursive problem-solving approaches
Lesson 4: NumPy and Plotting
Core Topic: Introduction to NumPy arrays and data visualization with Matplotlib
Apply your Python skills to scientific computing with this lesson on numerical operations and data visualization. You'll learn to work efficiently with numerical data and create informative visualizations to analyze and present your results.
Key concepts covered include:
- Working with NumPy's efficient
ndarrayobjects - Array attributes: shape, dtype, size, and dimensions
- Vectorized operations and broadcasting
- Creating plots with Matplotlib:
plot(),scatter(),bar() - Customizing plots with colors, markers, and styles
- Creating multi-plot figures with
subplot() - Introduction to curve fitting with
lmfit
📚 Exercises & Practical Labs
This section provides practical exercises specifically designed for the CRISMAT laboratory. The exercises cover topics ranging from basic physics and chemistry to synthesis analysis and crystallographic database queries. Each exercise includes downloadable materials and commented solutions.
📓 Exercise 1: Python Introduction for Physics and Chemistry
Objective: Become familiar with Python through 15 progressive exercises spanning basic calculations to real scientific applications.
Notebook Structure:
- Level 1 (Exercises 1-2): Basic Calculations - Moles to grams converter, density calculator
- Level 2 (Exercises 4-6): Temperature & States of Matter - Water state checker, Celsius to Kelvin, Boyle's Law
- Level 3 (Exercises 7-9): Energy & Motion - Kinetic energy, gravitational potential energy, free fall time
- Level 4 (Exercises 10-12): Chemistry & Solutions - pH checker, molarity calculator, dilution calculator
- Level 5 (Exercises 13-15): Loops & Real Applications - Radioactive decay, periodic table lookup, projectile motion
- Mini Project: Stoichiometry calculator (2H₂ + O₂ → 2H₂O)
- Challenge Exercises: Ideal gas law, weight on different planets
Skills practiced: variables, user input, arithmetic operations, conditionals (if/elif/else), math module, dictionaries, loops, and formula implementation.
⬇️ Download Notebook (Exercises) | 🔍 Download Solutions
Replace # links with actual .ipynb file paths
⚖️ Exercise 2: Stoichiometric Calculations for Synthesis
Objective: Develop Python functions to calculate precise quantities of elements needed for solid-state synthesis.
Tool Features:
Formula_parser()/Formula_parser_s()- Convert chemical formula strings to dictionaries with stoichiometryFormula_parser()- Handles brackets/parentheses in formulas (e.g., "Cu(OH)₂")WT_dic()- Calculate weight percent of each element in a compoundMW_dic()- Calculate molecular weight from a formula dictionaryfind_NEle()- Parse next element and its stoichiometric coefficientDict2Formula()- Convert dictionary back to formula string
Example usage - Synthesis of 5g Cu₂₂Fe₈Ge₄S₃₂:
from pyXRD.pt_tables import pt_p
# Parse formula to dictionary
finalD = Formula_parser_s('Cu22Fe8Ge4S32')
# Returns: {'Cu': 22.0, 'Fe': 8.0, 'Ge': 4.0, 'S': 32.0}
# Calculate weight percentages
finalwt = WT_dic(finalD)
# Returns: {'Cu': 0.442, 'Fe': 0.141, 'Ge': 0.092, 'S': 0.325}
# Calculate grams needed for 5g synthesis
final_ele_w = {atom: val * 5 for atom, val in finalwt.items()}
# Returns: {'Cu': 2.211g, 'Fe': 0.707g, 'Ge': 0.460g, 'S': 1.623g}
Skills practiced: string parsing, dictionary manipulation, periodic table data integration, function design, and practical synthesis planning.
🌐 Synthesis Helper Web App (interactive)
Replace # link with your actual web application URL
💎 Exercise 3: Querying the Crystallographic Open Database (COD)
Objective: Learn how to query crystallographic databases to obtain structures, cell parameters, and symmetry information using the COD RESTful API.
Notebook Features:
- Section 1: Basic CIF file retrieval by COD ID
- Section 2: Advanced searching with multiple criteria (elements, space group, unit cell parameters, volume, Z, publication data, etc.)
- Section 3: Detailed entry information and CIF metadata parsing
- Section 4: Bulk operations - download multiple CIFs and search-and-download workflows
- Section 5: Data analysis functions - convert results to pandas DataFrame, analyze cell parameters, get statistics over time
Example queries demonstrated:
- Download CIF file for COD ID 9000137
- Search for structures containing Sn, Cr, and S
- Search for NaCl structures with year range filtering
- Search by space group (F d -3 m) with volume range (500-1500 ų)
- Parse CIF metadata (cell parameters, space group, formula, journal, year)
- Bulk download multiple CIF files
Skills practiced: RESTful API interaction, JSON parsing, pandas DataFrames, crystallographic data analysis, batch processing, and working with scientific databases.
⬇️ Download Notebook (COD API Examples)
Replace # link with your actual COD notebook path
🔬 Additional Exercises (in development)
Coming soon:
- XRD data analysis
- EXAFS spectrum simulation
- 3D visualization of crystal structures
Practical Skills You Will Gain
By completing this four-lesson course, you will be able to:
- Set up a robust Python development environment using Conda/Mamba
- Write and organize Python code in scripts, modules, and Jupyter notebooks
- Work effectively with Python's core data structures and understand object mutability
- Design programs using functions, conditional logic, and loops to manage information flow
- Perform basic numerical computations with NumPy arrays
- Create clear, customized static plots to visualize your data
- Apply Python programming to scientific data analysis tasks
Next Steps After Completion
After completing this introductory course, you'll be well-prepared to:
- Tackle more advanced Python topics like object-oriented programming
- Explore specialized libraries for your field (Pandas for data analysis, SciPy for scientific computing, scikit-learn for machine learning)
- Begin working on your own Python projects
- Participate in online Python communities and forums
- Consider more advanced courses in specific Python application areas
Python's versatility means the skills you learn in this course can be applied to web development, data science, automation, scientific research, and many other domains.
Course References
This course is based on the Python Introduction lessons by Prestipino, available on GitHub. For additional learning resources: