Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save binarygalwalkin/ffb3aac377b735cc487825e090cc11c8 to your computer and use it in GitHub Desktop.
Save binarygalwalkin/ffb3aac377b735cc487825e090cc11c8 to your computer and use it in GitHub Desktop.
Python 1 : types, variables, operations
Display the source blob
Display the rendered blob
Raw
{
"nbformat_minor": 1,
"cells": [
{
"execution_count": 1,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": "Hello, Python!\n"
}
],
"source": "print ('Hello, Python!')"
},
{
"execution_count": 2,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": "3.5.5 |Anaconda, Inc.| (default, May 13 2018, 21:12:35) \n[GCC 7.2.0]\n"
}
],
"source": "# Check the Python Version\n\nimport sys\nprint(sys.version)"
},
{
"execution_count": 3,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": "Hello, Python!\n"
}
],
"source": "# Practice on writing comments\n\nprint('Hello, Python!') # This line prints a string\n# print('Hi')"
},
{
"execution_count": 4,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"ename": "NameError",
"evalue": "name 'frint' is not defined",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-4-bc81942a2137>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m# Print string as error message\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0mfrint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Hello, Python!\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mNameError\u001b[0m: name 'frint' is not defined"
],
"output_type": "error"
}
],
"source": "# Print string as error message\n\nfrint(\"Hello, Python!\")"
},
{
"execution_count": 5,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"ename": "SyntaxError",
"evalue": "EOL while scanning string literal (<ipython-input-5-5bdb04fa43c5>, line 3)",
"traceback": [
"\u001b[0;36m File \u001b[0;32m\"<ipython-input-5-5bdb04fa43c5>\"\u001b[0;36m, line \u001b[0;32m3\u001b[0m\n\u001b[0;31m print(\"Hello, Python!)\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m EOL while scanning string literal\n"
],
"output_type": "error"
}
],
"source": "# Try to see build in error message\n\nprint(\"Hello, Python!)"
},
{
"execution_count": 6,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": "This will be printed\n"
},
{
"ename": "NameError",
"evalue": "name 'frint' is not defined",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-6-8fb0611ec76e>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"This will be printed\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 4\u001b[0;31m \u001b[0mfrint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"This will cause an error\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 5\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"This will NOT be printed\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mNameError\u001b[0m: name 'frint' is not defined"
],
"output_type": "error"
}
],
"source": "# Print string and error to see the running order\n\nprint(\"This will be printed\")\nfrint(\"This will cause an error\")\nprint(\"This will NOT be printed\")"
},
{
"execution_count": 7,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": "hello world\n"
}
],
"source": "print(\"hello world\")"
},
{
"execution_count": 8,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": "hello world\n"
}
],
"source": "print(\"hello world\") #print the most comment line in the world"
},
{
"execution_count": 9,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"execution_count": 9,
"metadata": {},
"data": {
"text/plain": "int"
},
"output_type": "execute_result"
}
],
"source": "# Type of 12\n\ntype(12)"
},
{
"execution_count": 10,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"execution_count": 10,
"metadata": {},
"data": {
"text/plain": "float"
},
"output_type": "execute_result"
}
],
"source": "# Type of 2.14\n\ntype(2.14)"
},
{
"execution_count": 13,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"execution_count": 13,
"metadata": {},
"data": {
"text/plain": "str"
},
"output_type": "execute_result"
}
],
"source": "# Type of \"Hello, Python 101!\"\ntype(\"Hello, Python 101!\")"
},
{
"execution_count": 14,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"execution_count": 14,
"metadata": {},
"data": {
"text/plain": "float"
},
"output_type": "execute_result"
}
],
"source": "# Check type of 12.0\ntype(12.0)"
},
{
"execution_count": 15,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"execution_count": 15,
"metadata": {},
"data": {
"text/plain": "int"
},
"output_type": "execute_result"
}
],
"source": "# Print the type of -1\n\ntype(-1)"
},
{
"execution_count": 16,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"execution_count": 16,
"metadata": {},
"data": {
"text/plain": "int"
},
"output_type": "execute_result"
}
],
"source": "# Print the type of 4\n\ntype(4)"
},
{
"execution_count": 17,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"execution_count": 17,
"metadata": {},
"data": {
"text/plain": "int"
},
"output_type": "execute_result"
}
],
"source": "# Print the type of 0\n\ntype(0)"
},
{
"execution_count": 18,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": "sys.float_info(max=1.7976931348623157e+308, max_exp=1024, max_10_exp=308, min=2.2250738585072014e-308, min_exp=-1021, min_10_exp=-307, dig=15, mant_dig=53, epsilon=2.220446049250313e-16, radix=2, rounds=1)\n"
}
],
"source": "# information on floats\nprint(sys.float_info)"
},
{
"execution_count": 19,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"execution_count": 19,
"metadata": {},
"data": {
"text/plain": "float"
},
"output_type": "execute_result"
}
],
"source": "# Print the type of 1.0\n\ntype(1.0) # Notice that 1 is an int, and 1.0 is a float"
},
{
"execution_count": 20,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"execution_count": 20,
"metadata": {},
"data": {
"text/plain": "float"
},
"output_type": "execute_result"
}
],
"source": "# Print the type of 0.5\n\ntype(0.5)"
},
{
"execution_count": 21,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"execution_count": 21,
"metadata": {},
"data": {
"text/plain": "float"
},
"output_type": "execute_result"
}
],
"source": "# Print the type of 1/1\n\ntype(1/1)"
},
{
"execution_count": 22,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"execution_count": 22,
"metadata": {},
"data": {
"text/plain": "sys.float_info(max=1.7976931348623157e+308, max_exp=1024, max_10_exp=308, min=2.2250738585072014e-308, min_exp=-1021, min_10_exp=-307, dig=15, mant_dig=53, epsilon=2.220446049250313e-16, radix=2, rounds=1)"
},
"output_type": "execute_result"
}
],
"source": "# System settings about float type\n\nsys.float_info"
},
{
"execution_count": 24,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"execution_count": 24,
"metadata": {},
"data": {
"text/plain": "int"
},
"output_type": "execute_result"
}
],
"source": "# Verify that this is an integer\n\ntype(2)\n"
},
{
"execution_count": 25,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"execution_count": 25,
"metadata": {},
"data": {
"text/plain": "2.0"
},
"output_type": "execute_result"
}
],
"source": "# Convert 2 to a float\n\nfloat(2)\n"
},
{
"execution_count": 26,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"execution_count": 26,
"metadata": {},
"data": {
"text/plain": "float"
},
"output_type": "execute_result"
}
],
"source": "# Convert integer 2 to a float and check its type\n\ntype(float(2))"
},
{
"execution_count": 27,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"execution_count": 27,
"metadata": {},
"data": {
"text/plain": "1"
},
"output_type": "execute_result"
}
],
"source": "# Casting 1.1 to integer will result in loss of information\n\nint(1.1)"
},
{
"execution_count": 28,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"execution_count": 28,
"metadata": {},
"data": {
"text/plain": "1"
},
"output_type": "execute_result"
}
],
"source": "# Convert a string into an integer\n\nint('1')"
},
{
"execution_count": 29,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"ename": "ValueError",
"evalue": "invalid literal for int() with base 10: '1 or 2 people'",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-29-24ead4bd246e>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m# Convert a string into an integer with error\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0mint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'1 or 2 people'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mValueError\u001b[0m: invalid literal for int() with base 10: '1 or 2 people'"
],
"output_type": "error"
}
],
"source": "# Convert a string into an integer with error\n\nint('1 or 2 people')"
},
{
"execution_count": 30,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"execution_count": 30,
"metadata": {},
"data": {
"text/plain": "1.2"
},
"output_type": "execute_result"
}
],
"source": "# Convert the string \"1.2\" into a float\n\nfloat('1.2')"
},
{
"execution_count": 31,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"execution_count": 31,
"metadata": {},
"data": {
"text/plain": "'1'"
},
"output_type": "execute_result"
}
],
"source": "# Convert an integer to a string\n\nstr(1)"
},
{
"execution_count": 32,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"execution_count": 32,
"metadata": {},
"data": {
"text/plain": "'1.2'"
},
"output_type": "execute_result"
}
],
"source": "# Convert a float to a string\n\nstr(1.2)"
},
{
"execution_count": 33,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"execution_count": 33,
"metadata": {},
"data": {
"text/plain": "True"
},
"output_type": "execute_result"
}
],
"source": "# Value true\n\nTrue"
},
{
"execution_count": 34,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"execution_count": 34,
"metadata": {},
"data": {
"text/plain": "False"
},
"output_type": "execute_result"
}
],
"source": "# Value false\n\nFalse"
},
{
"execution_count": 35,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"execution_count": 35,
"metadata": {},
"data": {
"text/plain": "bool"
},
"output_type": "execute_result"
}
],
"source": "# Type of True\n\ntype(True)"
},
{
"execution_count": 36,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"execution_count": 36,
"metadata": {},
"data": {
"text/plain": "bool"
},
"output_type": "execute_result"
}
],
"source": "# Type of False\n\ntype(False)"
},
{
"execution_count": 1,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"execution_count": 1,
"metadata": {},
"data": {
"text/plain": "1"
},
"output_type": "execute_result"
}
],
"source": "# Convert True to int\n\nint(True)"
},
{
"execution_count": 2,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"execution_count": 2,
"metadata": {},
"data": {
"text/plain": "True"
},
"output_type": "execute_result"
}
],
"source": "# Convert 1 to boolean\n\nbool(1)"
},
{
"execution_count": 3,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"execution_count": 3,
"metadata": {},
"data": {
"text/plain": "False"
},
"output_type": "execute_result"
}
],
"source": "# Convert 0 to boolean\n\nbool(0)"
},
{
"execution_count": 4,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"execution_count": 4,
"metadata": {},
"data": {
"text/plain": "1.0"
},
"output_type": "execute_result"
}
],
"source": "# Convert True to float\n\nfloat(True)"
},
{
"execution_count": 5,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"execution_count": 5,
"metadata": {},
"data": {
"text/plain": "float"
},
"output_type": "execute_result"
}
],
"source": "type(6/2)"
},
{
"execution_count": 6,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"execution_count": 6,
"metadata": {},
"data": {
"text/plain": "int"
},
"output_type": "execute_result"
}
],
"source": "type (6//2)"
},
{
"execution_count": 8,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": "how many hours in 160 min ?\n"
}
],
"source": "print(\"how many hours in 160 min ?\")"
},
{
"execution_count": 10,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": "2\n"
}
],
"source": "print(160//60)"
},
{
"execution_count": 11,
"cell_type": "code",
"metadata": {},
"outputs": [],
"source": "# Store value into variable\n\nx = 43 + 60 + 16 + 41"
},
{
"execution_count": 12,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"execution_count": 12,
"metadata": {},
"data": {
"text/plain": "160"
},
"output_type": "execute_result"
}
],
"source": "x"
},
{
"execution_count": 13,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"execution_count": 13,
"metadata": {},
"data": {
"text/plain": "2.6666666666666665"
},
"output_type": "execute_result"
}
],
"source": "# Use another variable to store the result of the operation between variable and value\n\ny = x / 60\ny"
},
{
"execution_count": 14,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"execution_count": 14,
"metadata": {},
"data": {
"text/plain": "2.6666666666666665"
},
"output_type": "execute_result"
}
],
"source": "# Overwrite variable with new value\n\nx = x / 60\nx"
},
{
"execution_count": 15,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"execution_count": 15,
"metadata": {},
"data": {
"text/plain": "142"
},
"output_type": "execute_result"
}
],
"source": "# Name the variables meaningfully\n\ntotal_min = 43 + 42 + 57 # Total length of albums in minutes\ntotal_min"
},
{
"execution_count": 16,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"execution_count": 16,
"metadata": {},
"data": {
"text/plain": "2.3666666666666667"
},
"output_type": "execute_result"
}
],
"source": "# Name the variables meaningfully\n\ntotal_hours = total_min / 60 # Total length of albums in hours \ntotal_hours"
},
{
"execution_count": null,
"cell_type": "code",
"metadata": {},
"outputs": [],
"source": ""
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3.5",
"name": "python3",
"language": "python"
},
"language_info": {
"mimetype": "text/x-python",
"nbconvert_exporter": "python",
"version": "3.5.5",
"name": "python",
"file_extension": ".py",
"pygments_lexer": "ipython3",
"codemirror_mode": {
"version": 3,
"name": "ipython"
}
}
},
"nbformat": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment