{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "variables = ['a', 'b', 'c', 'd']\n", "values = [10, 37, 15, 4]\n", "expression = 'a*b-c*d'" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for index in range(len(variables)):\n", " # exec takes a string of python code and executes it\n", " exec(variables[index] + '=' + str(values[index]))\n", " # here it executes something like 'a = 1' or 'b = 2'\n", "print(a)\n", "print(b)\n", "print(c)\n", "print(d)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# eval takes a string containing a python expression\n", "# and returns the result of evaluating it\n", "print(eval(expression))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.1" } }, "nbformat": 4, "nbformat_minor": 2 }