Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {
- "cells": [
- {
- "cell_type": "code",
- "execution_count": 10,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "Hello, Python\n"
- ]
- }
- ],
- "source": [
- "# My first program in python \n",
- "print (\"Hello, Python\")"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 2,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "3.6.7 | packaged by conda-forge | (default, Jul 2 2019, 02:18:42) \n",
- "[GCC 7.3.0]\n"
- ]
- }
- ],
- "source": [
- "# Checking what version of python this is \n",
- "\n",
- "import sys\n",
- "print (sys.version)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 3,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "Anna\n"
- ]
- }
- ],
- "source": [
- "# Practising writing comments \n",
- "\n",
- "print(\"Anna\") # this line of code will write \"Anna\"\n",
- "# print (\"Sophie\") # this line of code has been commented out "
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 7,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "anna\n",
- "anna\n"
- ]
- }
- ],
- "source": [
- "# creating errors\n",
- "\n",
- "print (\"anna\")\n",
- "\n",
- "#if this said frind then an error stating that name \"frint\" is not defined would be shown\n",
- "\n",
- "print (\"anna\")\n",
- " \n",
- "# if I missed the second \" in this statement then an error stating EOL while scanning string literal would be shown"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 23,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "This will be printed\n",
- "This will cause an error\n",
- "This will NOT be printed if there is an error caused by the statement above\n"
- ]
- }
- ],
- "source": [
- "#if there is an error in the middle of the code then only the code up to that point will be executed, and an error message will be provided\n",
- "\n",
- "print(\"This will be printed\")\n",
- "print(\"This will cause an error\")\n",
- "print(\"This will NOT be printed if there is an error caused by the statement above\")"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 13,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "Hello World!\n"
- ]
- }
- ],
- "source": [
- "# Hello World\n",
- "\n",
- "print(\"Hello, World!\") #print the traditional hello world"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 14,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "'hello'"
- ]
- },
- "execution_count": 14,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "#Types of objects\n",
- "\n",
- "#integer\n",
- "\n",
- "11\n",
- "\n",
- "#float\n",
- "\n",
- "12.235\n",
- "\n",
- "#string \n",
- "\n",
- "\"hello\""
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 24,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "<class 'int'>\n",
- "<class 'float'>\n",
- "<class 'str'>\n",
- "<class 'int'>\n",
- "<class 'float'>\n"
- ]
- },
- {
- "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)"
- ]
- },
- "execution_count": 24,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "#finding the type of an object \n",
- "\n",
- "print(type(12))\n",
- "\n",
- "print(type(17.223))\n",
- "\n",
- "print(type(\"anna\"))\n",
- "\n",
- "print(type(-1))\n",
- "\n",
- "print(type(1.0))\n",
- "\n",
- "# learn more about the specifics of floats for your runtime environment\n",
- "\n",
- "sys.float_info\n",
- "\n"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 28,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "<class 'int'>\n",
- "2.0\n",
- "<class 'float'>\n"
- ]
- }
- ],
- "source": [
- "# Verify 2 is an integer\n",
- "\n",
- "print(type(2))\n",
- "\n",
- "#converting 2 to a float\n",
- "\n",
- "print(float(2))\n",
- "\n",
- "# verify 2.0 is a float\n",
- "\n",
- "print(type(float(2)))\n",
- "\n"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 34,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "<class 'float'>\n",
- "1\n",
- "<class 'int'>\n"
- ]
- }
- ],
- "source": [
- "# verify 1.1 is a float\n",
- "\n",
- "print(type(1.1))\n",
- "\n",
- "#convert 1.1 to an integer\n",
- "\n",
- "print(int(1.1)) # doing this can cause you to lose information the 0.1\n",
- "\n",
- "# verify 1 is an integer\n",
- "\n",
- "print(type(int(1.1)))"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 42,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "<class 'str'>\n",
- "1\n",
- "<class 'int'>\n",
- "1.0\n"
- ]
- }
- ],
- "source": [
- "#converting from string to integer\n",
- "\n",
- "print(type(\"1\"))\n",
- "\n",
- "print(int(\"1\"))\n",
- "\n",
- "print(type(int(\"1\")))\n",
- "\n",
- "#this only works when the string is a number, it will produce an error if you try to do this with a word e.g\n",
- "\n",
- "#int(\"person\")\n",
- "\n",
- "#converting string to float \n",
- "\n",
- "print(float(\"1\"))"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 44,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "'1.27654'"
- ]
- },
- "execution_count": 44,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "#converting numbers to strings \n",
- "\n",
- "str(1.27654)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 51,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "<class 'bool'>\n",
- "<class 'bool'>\n",
- "True\n",
- "False\n",
- "1\n",
- "0\n",
- "1.0\n",
- "0.0\n"
- ]
- }
- ],
- "source": [
- "#Boolean data \n",
- "\n",
- "print(type(True))\n",
- "print(type(False))\n",
- "\n",
- "#converting 1 and 0 into bool\n",
- "\n",
- "print(bool(1))\n",
- "print(bool(0))\n",
- "\n",
- "#converting True and False into int\n",
- "\n",
- "print(int(True))\n",
- "print(int(False))\n",
- "\n",
- "#converting True and False into floats\n",
- "\n",
- "print(float(True))\n",
- "print(float(False))"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 54,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "3.0\n",
- "<class 'float'>\n",
- "3\n",
- "<class 'int'>\n"
- ]
- }
- ],
- "source": [
- "#operations \n",
- "\n",
- "#division \n",
- "\n",
- "#float division \n",
- "\n",
- "print(6/2)\n",
- "print(type(6/2))\n",
- "\n",
- "#integer division \n",
- "\n",
- "print(6//2)\n",
- "print(type(6//2))\n",
- "\n",
- "#"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 17,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "x = \n",
- "15\n",
- "y = \n",
- "-13\n",
- "2.6666666666666665\n"
- ]
- }
- ],
- "source": [
- "#addition \n",
- "\n",
- "x=1+2+3+4+5\n",
- "\n",
- "print(\"x = \") \n",
- "print(x)\n",
- "\n",
- "#subtraction \n",
- "\n",
- "y=1-2-3-4-5\n",
- "\n",
- "print(\"y = \") \n",
- "print(y)\n",
- "\n",
- "#can do the same with multiplication and division (integer and float)\n",
- "\n",
- "#how many hours in 160 minutes\n",
- "\n",
- "mins=160\n",
- "number_of_hours=mins/60\n",
- "print(number_of_hours)\n",
- "\n"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 20,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "50.0"
- ]
- },
- "execution_count": 20,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "#assigning variables\n",
- "\n",
- "x=2*100*3\n",
- "y=x/12\n",
- "y"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 22,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "4.0"
- ]
- },
- "execution_count": 22,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "#overwriting variable \n",
- "\n",
- "x=12\n",
- "x=x/3\n",
- "x"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 25,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "21"
- ]
- },
- "execution_count": 25,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "# defining variables \n",
- "\n",
- "x=12\n",
- "y=7\n",
- "z=1*2\n",
- "m=x+y+z\n",
- "m"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {},
- "outputs": [],
- "source": []
- }
- ],
- "metadata": {
- "kernelspec": {
- "display_name": "Python",
- "language": "python",
- "name": "conda-env-python-py"
- },
- "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.7"
- }
- },
- "nbformat": 4,
- "nbformat_minor": 4
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement