summaryrefslogtreecommitdiff
path: root/The_Spirit_of_C/chapter8.ipynb
diff options
context:
space:
mode:
authorThomas Stephen Lee2015-09-04 22:04:10 +0530
committerThomas Stephen Lee2015-09-04 22:04:10 +0530
commit41f1f72e9502f5c3de6ca16b303803dfcf1df594 (patch)
treef4bf726a3e3ce5d7d9ee3781cbacfe3116115a2c /The_Spirit_of_C/chapter8.ipynb
parent9c9779ba21b9bedde88e1e8216f9e3b4f8650b0e (diff)
downloadPython-Textbook-Companions-41f1f72e9502f5c3de6ca16b303803dfcf1df594.tar.gz
Python-Textbook-Companions-41f1f72e9502f5c3de6ca16b303803dfcf1df594.tar.bz2
Python-Textbook-Companions-41f1f72e9502f5c3de6ca16b303803dfcf1df594.zip
add/remove/update books
Diffstat (limited to 'The_Spirit_of_C/chapter8.ipynb')
-rwxr-xr-xThe_Spirit_of_C/chapter8.ipynb567
1 files changed, 0 insertions, 567 deletions
diff --git a/The_Spirit_of_C/chapter8.ipynb b/The_Spirit_of_C/chapter8.ipynb
deleted file mode 100755
index baae6f9a..00000000
--- a/The_Spirit_of_C/chapter8.ipynb
+++ /dev/null
@@ -1,567 +0,0 @@
-{
- "metadata": {
- "name": "",
- "signature": "sha256:db9d52743fdd42516fb59e7618f0fc71d76d4ba3cb21fd098bf4fd004c40683b"
- },
- "nbformat": 3,
- "nbformat_minor": 0,
- "worksheets": [
- {
- "cells": [
- {
- "cell_type": "heading",
- "level": 1,
- "metadata": {},
- "source": [
- "Chapter 8, A Detailed Look at the printf and scanf Functions"
- ]
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "Program 8-1 , Page number: 149"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "# A print demo\n",
- "# The conversion specification for numbers.\n",
- "\n",
- "print \"Case 1 :%d:\"%123\n",
- "\n",
- "print \"Case 2 :%0d:\"%123\n",
- "\n",
- "print \"Case 3 :%2d:\"%123\n",
- "\n",
- "print \"Case 4 :%8d:\"%123\n",
- "\n",
- "print \"Case 5 :%-8d:\"%123\n",
- "\n",
- "print \"Case 6 :%8d:\"%-123\n",
- "\n",
- "print \"Case 7 :%f:\"%123.456\n",
- "\n",
- "print \"Case 8 :%f:\"%123.4567896\n",
- "\n",
- "print \"Case 9 :%e:\"%123.456\n",
- "\n",
- "print \"Case 10 :%e:\"%1.23456e2\n",
- "\n",
- "print \"Case 11 :%e:\"%123.456e+0\n",
- "\n",
- "print \"Case 12 :%f:\"%1.23456E+02\n",
- "\n",
- "print \"Case 13 :%4.2f:\"%123.456\n",
- "\n",
- "print \"Case 14 :%8.2f:\"%123.456\n",
- "\n",
- "print \"Case 15 :%.3e:\"%123.456\n",
- "\n",
- "print \"Case 16 :%12.3e:\"%123.456\n"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Case 1 :123:\n",
- "Case 2 :123:\n",
- "Case 3 :123:\n",
- "Case 4 : 123:\n",
- "Case 5 :123 :\n",
- "Case 6 : -123:\n",
- "Case 7 :123.456000:\n",
- "Case 8 :123.456790:\n",
- "Case 9 :1.234560e+02:\n",
- "Case 10 :1.234560e+02:\n",
- "Case 11 :1.234560e+02:\n",
- "Case 12 :123.456000:\n",
- "Case 13 :123.46:\n",
- "Case 14 : 123.46:\n",
- "Case 15 :1.235e+02:\n",
- "Case 16 : 1.235e+02:\n"
- ]
- }
- ],
- "prompt_number": 1
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "Program 8-2 , Page number: 152"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "# A print demo:\n",
- "# Interpretation of values\n",
- "\n",
- "# python is dynamically-typed language\n",
- "# which is why declaration of variables is omitted\n",
- "n = -123\n",
- "c = \"V\"\n",
- "\n",
- "print \"%d\"%n\n",
- "print \"%u\"%n\t\t# python correctly interprets the negative(unsigned) integer\n",
- "\n",
- "print \"%c\"%c\n",
- "# print \"%d\"%c \t# python doesn't support implicit type-casting from integer to character or character to integer\n",
- "print \"%c\"%86"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "-123\n",
- "-123\n",
- "V\n",
- "V\n"
- ]
- }
- ],
- "prompt_number": 2
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "Program 8-3 , Page number: 153"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "# Printing out the ASCII value of a character\n",
- "\n",
- "# There is no do..while loop in python\n",
- "# Hence, we will be using while loop \n",
- "\n",
- "\n",
- "answer = \"y\"\n",
- "while answer == \"y\" :\n",
- "\tprint \"Enter a character:\",\n",
- "\tin_char = \"a\"\n",
- "\tprint in_char\n",
- "\n",
- "\tprint \"The ASCII value of %c is %d\"%(in_char,ord(in_char)) \t# ord() function returns the ascii(int) value of a character\n",
- "\n",
- "\tprint \"\\nAnother? (y/n)\",\n",
- "\tanswer = \"n\"\t\t\t# To terminate the loop\n",
- "\tprint answer\n",
- "\n",
- "\n"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter a character: a\n",
- "The ASCII value of a is 97\n",
- "\n",
- "Another? (y/n) n\n"
- ]
- }
- ],
- "prompt_number": 3
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "Program 8-4 , Page number: 154"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "# Converting a digit to a number by using its ASCII value\n",
- "\n",
- "print \"Enter a digit:\",\n",
- "digit = \"A\"\n",
- "print digit\n",
- "\n",
- "if digit < \"0\" or digit > \"9\" :\n",
- "\tprint \"That's not a digit\"\n",
- "else :\n",
- "\tprint \"The digit you typed is %c\"%digit"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter a digit: A\n",
- "That's not a digit\n"
- ]
- }
- ],
- "prompt_number": 4
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "Program 8-5 , Page number: 156"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "# Converting a letter from lower to upper case or vice-versa\n",
- "\n",
- "answer = \"y\"\n",
- "while answer == \"y\" :\n",
- "\tprint \"\\n\\n\\nEnter a letter:\",\n",
- "\tletter = \"A\"\n",
- "\tprint letter\n",
- "\n",
- "\tif letter >= \"A\" and letter < \"Z\" :\t\t# Letter is uppercase\n",
- "\t\tprint \"Lower case is: %c\"%chr(ord(letter) + (ord(\"a\")-ord(\"A\")))\n",
- "\telif letter >= \"a\" and letter <= \"z\" :\t# Letter is lowercase\n",
- "\t\tprint \"Upper case is: %c\"%chr(ord(letter) - (ord(\"a\")-ord(\"A\")))\n",
- "\telse :\n",
- "\t\tprint \"That's not a letter\"\n",
- "\n",
- "\t# Ask if the user wants to go again\n",
- "\tprint \"\\nAnother? (y/n)\",\n",
- "\tanswer = \"n\"\t\t# To terminate the loop\n",
- "\tprint answer"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "\n",
- "\n",
- "\n",
- "Enter a letter: A\n",
- "Lower case is: a\n",
- "\n",
- "Another? (y/n) n\n"
- ]
- }
- ],
- "prompt_number": 5
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "Program 8-6 , Page number: 157"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "# Example of using control characters\n",
- "\n",
- "print \"The word apple\\b\\b\\b\\b\\b_____ is underlined.\\n\"\n",
- "print \"The last word in this sentence is way over\\t\\there.\""
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "The word apple\b\b\b\b\b_____ is underlined.\n",
- "\n",
- "The last word in this sentence is way over\t\there.\n"
- ]
- }
- ],
- "prompt_number": 6
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "Program 8-7 , Page number: 158"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "# How to print special characters\n",
- "\n",
- "print \"This is how to print a double-quote: \\\"\"\n",
- "print \"This is how to print a backslash: \\\\\"\n",
- "# this is how to write an apostrophe character\n",
- "c = '\\''\n",
- "print \"c now contains: %c\"%c\n",
- "print \"This is how to print a percent-sign: %\"\t\t# no escape sequence required for % in python"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "This is how to print a double-quote: \"\n",
- "This is how to print a backslash: \\\n",
- "c now contains: '\n",
- "This is how to print a percent-sign: %\n"
- ]
- }
- ],
- "prompt_number": 7
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "Program 8-8 , Page number: 160"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "# Octal and hexadecimal notation\n",
- "\n",
- "a = 27\n",
- "b = 033\n",
- "c = 0x1b\n",
- "\n",
- "print \"a = %d, b = %d, c = %d\"%(a,b,c)\n",
- "print \"a = %o, b = %o, c = %o\"%(a,b,c)\n",
- "print \"a = %x, b = %x, c = %x\"%(a,b,c)"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "a = 27, b = 27, c = 27\n",
- "a = 33, b = 33, c = 33\n",
- "a = 1b, b = 1b, c = 1b\n"
- ]
- }
- ],
- "prompt_number": 8
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "Program 8-9 , Page number: 162"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "# Illustration of some ways to print a string\n",
- "\n",
- "print \"This is simply a control string.\"\n",
- "\n",
- "print \"%s\"%(\"The control string just tells how to print this.\")\n",
- "\n",
- "print \"%s\"%(\"Newline may be put here, instead!.\\n\"),\n",
- "\n",
- "print \"%s %s %s %s %s\"%(\"This\",\"is\",\"valid\",\"in\",\"C.\")\n",
- "\n",
- "print \"%s %s also %s %s %s\"%(\"This\",\"is\",\"valid\",\"in\",\"C.\")\n",
- "\n",
- "print \"%s %s %s %s %s %s %s\"%(\"This\",\"is\",\"valid\",\"in\",\"C\",\"also.\",\"\\n\"),"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "This is simply a control string.\n",
- "The control string just tells how to print this.\n",
- "Newline may be put here, instead!.\n",
- "This is valid in C.\n",
- "This is also valid in C.\n",
- "This is valid in C also. \n"
- ]
- }
- ],
- "prompt_number": 9
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "Program 8-10 , Page number: 163"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "# %s conversion specification demonstrated\n",
- "\n",
- "print \"Case 1 :%s:\"%(\"A string\")\n",
- "\n",
- "print \"Case 2 :%3s:\"%(\"A string\")\n",
- "\n",
- "print \"Case 3 :%12s:\"%(\"A string\")\n",
- "\n",
- "print \"Case 4 :%-12s:\"%(\"A string\")\n",
- "\n",
- "print \"Case 5 :%12.6s:\"%(\"A string\")\n",
- "\n",
- "print \"Case 6 :%12.12s:\"%(\"A string\")\n",
- "\n",
- "print \"Case 7 :%.6s:\"%(\"A string\")\n",
- "\n",
- "print \"Case 8 :%-12.6s:\"%(\"A string\")"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Case 1 :A string:\n",
- "Case 2 :A string:\n",
- "Case 3 : A string:\n",
- "Case 4 :A string :\n",
- "Case 5 : A stri:\n",
- "Case 6 : A string:\n",
- "Case 7 :A stri:\n",
- "Case 8 :A stri :\n"
- ]
- }
- ],
- "prompt_number": 10
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "Program 8-11 , Page number: 164 "
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "# Some miscellaneous conversion sppecifications\n",
- "\n",
- "a = 123456789L\n",
- "\n",
- "print \"%d\"%a\t\t\t# in python both int and long are unified..so %d specifier valid unlike in C\n",
- "print \"%ld\"%a\n",
- "print \":%c:\"%(\"V\")\n",
- "print \":%0c:\"%(\"V\")\n",
- "print \":%5c:\"%(\"V\")\n",
- "print \":%-5c:\"%(\"V\")"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "123456789\n",
- "123456789\n",
- ":V:\n",
- ":V:\n",
- ": V:\n",
- ":V :\n"
- ]
- }
- ],
- "prompt_number": 11
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "Program 8-13 , Page number: 171"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "# Reading decimal,octal and hex numbers\n",
- "\n",
- "print \"Enter a number in decimal:\",\n",
- "a = 27\n",
- "print a\n",
- "print \"You entered %d\"%a\n",
- "\n",
- "print \"\\nEnter a number in octal:\",\n",
- "a = 033\t\t\t# O is added in the front to specify octal value\n",
- "print a\n",
- "print \"You entered %o, or %d in decimal\"%(a,a)\n",
- "\n",
- "print \"\\nEnter a number in hex:\",\n",
- "a = 0x1B\n",
- "print a\n",
- "print \"You entered %x, or %d in decimal\"%(a,a)"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter a number in decimal: 27\n",
- "You entered 27\n",
- "\n",
- "Enter a number in octal: 27\n",
- "You entered 33, or 27 in decimal\n",
- "\n",
- "Enter a number in hex: 27\n",
- "You entered 1b, or 27 in decimal\n"
- ]
- }
- ],
- "prompt_number": 12
- }
- ],
- "metadata": {}
- }
- ]
-} \ No newline at end of file