|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594 |
- {
- "cells": [
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "All the IPython Notebooks in this lecture series are available at https://github.com/rajathkumarmp/Python-Lectures"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "# Print Statement"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "The **print** statement can be used in the following different ways :\n",
- "\n",
- " - print(\"Hello World\")\n",
- " - print(\"Hello\", <Variable Containing the String>)\n",
- " - print(\"Hello\" + <Variable Containing the String>)\n",
- " - print(\"Hello %s\" % <variable containing the string>)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 1,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "Hello World\n"
- ]
- }
- ],
- "source": [
- "print(\"Hello World\")"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "In Python, single, double and triple quotes are used to denote a string.\n",
- "Most use single quotes when declaring a single character. \n",
- "Double quotes when declaring a line and triple quotes when declaring a paragraph/multiple lines."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 2,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "Hey\n"
- ]
- }
- ],
- "source": [
- "print('Hey')"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 3,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "My name is Rajath Kumar M.P.\n",
- "\n",
- "I love Python.\n"
- ]
- }
- ],
- "source": [
- "print(\"\"\"My name is Rajath Kumar M.P.\n",
- "\n",
- "I love Python.\"\"\")"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Strings can be assigned to variable say _string1_ and _string2_ which can called when using the print statement."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 1,
- "metadata": {
- "scrolled": true
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "Hello World\n",
- "Hello World !\n"
- ]
- }
- ],
- "source": [
- "string1 = 'World'\n",
- "print('Hello', string1)\n",
- "\n",
- "string2 = '!'\n",
- "print('Hello', string1, string2)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "String concatenation is the \"addition\" of two strings. Observe that while concatenating there will be no space between the strings."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 2,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "HelloWorld!\n"
- ]
- }
- ],
- "source": [
- "print('Hello' + string1 + string2)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "**%s** is used to refer to a variable which contains a string."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 6,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "Hello World\n"
- ]
- }
- ],
- "source": [
- "print(\"Hello %s\" % string1)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Similarly, when using other data types\n",
- "\n",
- " - %s -> string\n",
- " - %d -> Integer\n",
- " - %f -> Float\n",
- " - %o -> Octal\n",
- " - %x -> Hexadecimal\n",
- " - %e -> exponential\n",
- " \n",
- "This can be used for conversions inside the print statement itself."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 3,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "Actual Number = 18\n",
- "Float of the number = 18.000000\n",
- "Octal equivalent of the number = 22\n",
- "Hexadecimal equivalent of the number = 12\n",
- "Exponential equivalent of the number = 1.800000e+01\n"
- ]
- }
- ],
- "source": [
- "print(\"Actual Number = %d\" % 18)\n",
- "print(\"Float of the number = %f\" % 18)\n",
- "print(\"Octal equivalent of the number = %o\" % 18)\n",
- "print(\"Hexadecimal equivalent of the number = %x\" % 18)\n",
- "print(\"Exponential equivalent of the number = %e\" % 18)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "When referring to multiple variables parenthesis is used."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 4,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "Hello World !\n"
- ]
- }
- ],
- "source": [
- "print(\"Hello %s %s\" % (string1,string2))"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "## Other Examples"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "The following are other different ways the print statement can be put to use."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 9,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "I want %d to be printed here\n"
- ]
- }
- ],
- "source": [
- "print(\"I want %%d to be printed %s\" %'here')"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 5,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "_A_A_A_A_A_A_A_A_A_A\n"
- ]
- }
- ],
- "source": [
- "print('_A'*10)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 6,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "Jan\n",
- "Feb\n",
- "Mar\n",
- "Apr\n",
- "May\n",
- "Jun\n",
- "Jul\n",
- "Aug\n"
- ]
- }
- ],
- "source": [
- "print(\"Jan\\nFeb\\nMar\\nApr\\nMay\\nJun\\nJul\\nAug\")"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 12,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "Jan\n",
- "Feb\n",
- "Mar\n",
- "Apr\n",
- "May\n",
- "Jun\n",
- "Jul\n",
- "Aug\n"
- ]
- }
- ],
- "source": [
- "print(\"\\n\".join(\"Jan Feb Mar Apr May Jun Jul Aug\".split(\" \")))"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 12,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "I want \\n to be printed.\n"
- ]
- }
- ],
- "source": [
- "print(\"I want \\\\n to be printed.\")"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 13,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "\n",
- "Routine:\n",
- "\t- Eat\n",
- "\t- Sleep\n",
- "\t- Repeat\n",
- "\n"
- ]
- }
- ],
- "source": [
- "print \"\"\"\n",
- "Routine:\n",
- "\\t- Eat\n",
- "\\t- Sleep\\n\\t- Repeat\n",
- "\"\"\""
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "# PrecisionWidth and FieldWidth"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Fieldwidth is the width of the entire number and precision is the width towards the right. One can alter these widths based on the requirements.\n",
- "\n",
- "The default Precision Width is set to 6."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 14,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "'3.121312'"
- ]
- },
- "execution_count": 14,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "\"%f\" % 3.121312312312"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Notice upto 6 decimal points are returned. To specify the number of decimal points, '%(fieldwidth).(precisionwidth)f' is used."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 15,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "'3.12131'"
- ]
- },
- "execution_count": 15,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "\"%.5f\" % 3.121312312312"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "If the field width is set more than the necessary than the data right aligns itself to adjust to the specified values."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 16,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "' 3.12131'"
- ]
- },
- "execution_count": 16,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "\"%9.5f\" % 3.121312312312"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Zero padding is done by adding a 0 at the start of fieldwidth."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 17,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "'00000000000003.12131'"
- ]
- },
- "execution_count": 17,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "\"%020.5f\" % 3.121312312312"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "For proper alignment, a space can be left blank in the field width so that when a negative number is used, proper alignment is maintained."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 18,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- " 3.121312\n",
- "-3.121312\n"
- ]
- }
- ],
- "source": [
- "print \"% 9f\" % 3.121312312312\n",
- "print \"% 9f\" % -3.121312312312"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "'+' sign can be returned at the beginning of a positive number by adding a + sign at the beginning of the field width."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 19,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "+3.121312\n",
- "-3.121312\n"
- ]
- }
- ],
- "source": [
- "print \"%+9f\" % 3.121312312312\n",
- "print \"% 9f\" % -3.121312312312"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "As mentioned above, the data right aligns itself when the field width mentioned is larger than the actualy field width. But left alignment can be done by specifying a negative symbol in the field width."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 20,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "'3.121 '"
- ]
- },
- "execution_count": 20,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "\"%-9.3f\" % 3.121312312312"
- ]
- }
- ],
- "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.5.2"
- }
- },
- "nbformat": 4,
- "nbformat_minor": 1
- }
|