0% found this document useful (0 votes)
8 views30 pages

Python Programming Basics Guide

The document provides an introduction to Python programming basics, covering topics such as syntax, comments, literals, variables, and functions. It explains the importance of correct syntax to avoid errors, how to create and use variables, and the concept of functions in Python. Additionally, it includes examples and explanations for debugging code and understanding object references in Python.

Uploaded by

horace23vt
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views30 pages

Python Programming Basics Guide

The document provides an introduction to Python programming basics, covering topics such as syntax, comments, literals, variables, and functions. It explains the importance of correct syntax to avoid errors, how to create and use variables, and the concept of functions in Python. Additionally, it includes examples and explanations for debugging code and understanding object references in Python.

Uploaded by

horace23vt
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1/14/25, 5:30 PM Python basics

Python Basics
CS1302 Introduction to Computer Programming

In [1]: import sys


from ipywidgets import interact

# Set LLM alias


%load_ext jupyter_ai
%ai update chatgpt dive:chat

%reload_ext divewidgets

Content
Syntax, Comments, Literal, Variable, Function, Identifier
Basic data types
string formatting

Syntax
In computer science, the syntax is a set of rules for programming. In other
words, it means using legal structures and commands that a computer can
interpret.
Human language is used to communicate with people. Programming language is
used to communicate with machine. If we don't follow the correct syntax, the
computer can't run the code (syntax error). See example below.
In [2]: # print() is a function to print a message out on the screen
print("Hello, World!" #this line has error because it misses one ) in the

Cell In[2], line 2


print("Hello, World!" #this line has error because it misses one ) in
the end, so the computer raises SyntaxError

^
SyntaxError: incomplete input

In [5]: %%ai chatgpt -f text

Please help debug the following python code.


print("Hello, World!"
Error message: SyntaxError: incomplete input

[Link] [Link] 1/30


1/14/25, 5:30 PM Python basics

Out[5]: # Step-by-step analysis of the problem:


1. **Missing closing parenthesis**: The error message indicates that the
input is incomplete, which suggests that there is a syntax issue with th
e code.
2. **Insufficient termination**: In Python, the `print()` function requi
res a closing parenthesis to indicate the end of the function call.

# Fixed solution:
```python
print("Hello, World!")
```

# Explanation of changes:
* **Added a closing parenthesis**: Added a closing parenthesis at the en
d of the `print()` function call to complete the syntax.

# Tests and example uses:


To test the code, save it to a file (e.g., `[Link]`), then run it usin
g Python (e.g., `python [Link]`). The output should be:
```
Hello, World!
```
Note: Make sure to save the file with the correct extension (`.py`) and
run it in a Python environment.

Alternatively, you can also test the code in an interactive Python shell
or a Jupyter notebook. Simply copy and paste the corrected code, and it
should execute without errors.

In [6]: print("Hello, World!") #this code is correct, and it will print "Hello, W
print("Something I'd like to print")

Hello, World!
Something I'd like to print

Comments
In the above example, the texts after hash key # is called comments
Why we need comments?
Comments can be used to explain Python code.
Comments can be used to make the code more readable.
Comments can be used to prevent execution when testing code.
Create a single line comment
Comments starts with a #, and Python will ignore them:
In [8]: #This program prints "Hello, World!"
print("Hello, World!")

Hello, World!

Comments can be placed at the end of a line, and Python will ignore the rest of the
line:
[Link] [Link] 2/30
1/14/25, 5:30 PM Python basics

In [9]: print("Hello, World!") #This program prints "Hello, World!"

Hello, World!

A comment does not have to be text that explains the code, it can also be used to
prevent Python from executing code:
In [10]: print("Hello, World!")
#print("Cheers, Mate!")

Hello, World!

Create multi-line comments


Python does not really have a syntax for multi-line comments. To add a multi-line
comment you could insert a # for each line:
In [11]: #This program
#prints
#"Hello, World!"
print("Hello, World!")

Hello, World!

Or, not quite as intended, you can use a multiline string.


Since Python will ignore string literals that are not assigned to a variable, you can add
a multiline string (triple quotes) in your code, and place your comment inside it:
In [12]: """
This is a comment
written in
more than just one line
This is more comments
This is another comment
"""
print("Hello, World!")

Hello, World!

Literal
literals are a notation for representing a fixed value in source code. They can also be
defined as raw value or data given in variables or constants.
Python has different types of literals, such as:
1. Integer literal
78 is an integer literal
2. Floating point literal
23.32 is a floating point literal
3. character literal
'a' is a character literal

[Link] [Link] 3/30


1/14/25, 5:30 PM Python basics

4. String literal
"hello" is a string literal
In [13]: x = 78 #78 is integer literal
y = 23.32 #23.32 is floating point literal
z = "a" #"a" is character literal
text = "hello" #string literal

Variable
A variable is just a name to store values. This means that when you create a
variable you reserve some space in memory.
Based on the data type of a variable, the computer allocates memory and
decides what can be stored in the reserved memory. Therefore, by assigning
different data types to variables, you can store integers, decimals or characters
in these variables.

how to create variables


Python variables do not need explicit declaration to reserve memory space. The
declaration happens automatically when you assign a value to a variable. The
equal sign (=) is used to assign values to variables.
The operand to the left of the = operator is the name of the variable and the
operand to the right of the = operator is the value stored in the variable. For
example:
In [15]: # how to create a variable
counter = 100 # this creates a variable counter which points to th
miles = 100.23 # this creates a variable miles which points to the
name = "John" # this creates a variable name which points to the m

#== #in Python, this operator means the equal operator

print(counter)
print(miles)
print(name)

100
100.23
John

Object References
What is actually happening when you make a variable assignment?
Python is a object-oriented language. In fact, every item of data in a Python program
is an object of a specific type or class (object will be introduced later in this course).

[Link] [Link] 4/30


1/14/25, 5:30 PM Python basics

Considering the following code:


In [16]: print(300)

300

When presented with the statement print(300) , the interpreter does the
following:
Creates an integer object
Gives it the value 300
Displays it on the screen
A Python variable is a symbolic name that is a reference or pointer to an object. Once
an object is assigned to a variable, you can refer to the object by that name. But the
data itself is still contained within the object.
For example:
In [27]: n = 300

This assignment creates an integer object with the value 300 and assigns the
variable n to point to that object.

Variable Assignment.
Now consider the following statement:
In [28]: m = n

What happens when it is executed? Python does not create another object. It simply
creates a new symbolic name or reference, m , which points to the same object that
n points to.

Multiple References to a Single Object.


Next, suppose you do this:
In [ ]: m = 400

[Link] [Link] 5/30


1/14/25, 5:30 PM Python basics

Now Python creates a new integer object with the value 400, and m becomes a
reference to it.

References to Separate Objects.


Lastly, suppose this statement is executed next:
In [29]: n = "foo"

Now Python creates a string object with the value "foo" and makes n reference
that.

Orphaned Object.
There is no longer any reference to the integer object 300. It is orphaned, and there
is no way to access it.
Is assignment the same as equality?
No because:
= is assignment operator, x = 15 means we create a variable x and assign
15 to x
== is comparison operator, x == 15 means we compare the value of x with
15. It returns True if x is equal to 15; otherwise, it returns False
[Link] [Link] 6/30
1/14/25, 5:30 PM Python basics

Exercise Try out the code by yourself.


In [19]: x = 15
print(15 == x)
print(x == 15)

x = 20
print(15 == x)
print(x == 15)
#15 == x is equivalent to x == 15; but 15 = x is different from x = 15

x == 15
15 == x

x=15

True
True
False
False

In [20]: #the code below is wrong


15 = x

Cell In[20], line 2


15 = x
^
SyntaxError: cannot assign to literal here. Maybe you meant '==' instead o
f '='?

The following tuple assignment syntax can assign multiple variables in one line.
In [17]: %%optlite --height 200
x, y, z = '15', '30', 15

Out[17]: OPTWidget(value=None, height=200, script="x, y, z = '15', '30', 15\n")

One can also use chained assignment to set different variables to the same value.
In [21]: %%optlite --height 200
x = y = z = 0

Out[21]: OPTWidget(value=None, height=200, script='x = y = z = 0\n')

Variables can be deleted using del . Accessing a variable before assignment raises
a Name error.
In [28]: %%optlite --height 400
x = 5
y = 10
del x, y
x, y

Out[28]: OPTWidget(value=None, height=400, script='x = 5\ny = 10\ndel x, y\nx, y


\n')

How about $x = x+1$? Does it mean:


[Link] [Link] 7/30
1/14/25, 5:30 PM Python basics

1. $x$ is equal to $x$+1?


2. $x$ is defined to be $x$+1?
If we say yes to the above questions, the value of $x$ should be $\pm\infty$. (Why?)
Let's discover the truth by executing the following assignments step-by-step using
OPTLite:
In [23]: %%optlite -h 300
x = 15
x = x + 1

Out[23]: OPTWidget(value=None, height=300, script='x = 15\nx = x + 1\n')

In [25]: %%ai chatgpt -f text


What is the solution to $x+1=x$?

Out[25]: ## Step 1: Subtract x from both sides of the equation


Subtracting x from both sides gives us: $x - x + 1 = x - x$, which simpl
ifies to $1 = 0$.

## Step 2: Analyze the result


The equation $1 = 0$ is a contradiction, as 1 cannot be equal to 0. This
indicates that the original equation $x+1=x$ has no solution.

The final answer is: $\boxed{No solution}$

Function
A function is a block of code which only runs when it is called.
You can pass data, known as parameters or arguments , into a function.
A function can return data as a result.
print() is a function to print the message on the screen

How to define a function


In Python a function is defined using the def keyword:
How to call a function
To call a function, use the function name followed by parenthesis:
In [29]: def m1(): #this is how to define a function
print("Hello from a function")

m1() #this is how to call a function

Hello from a function

Arguments
Information can be passed into functions as arguments.

[Link] [Link] 8/30


1/14/25, 5:30 PM Python basics

Arguments are specified after the function name, inside the parentheses. You
can add as many arguments as you want, just separate them with a comma.
The following example has a function with one argument (fname). When the
function is called, we pass along a first name, which is used inside the function
to print the full name:
In [30]: #a function will only be executed when it's called
#In this cell, we only define a function, so it is not executed
def my_function(course_name):
print("Welcome to "+ course_name)

In [31]: my_function("CityU")

Welcome to CityU

To know how to use a built-in function, we can add a question mark before or after
the function name:
In [32]: ?print

Signature: print(*args, sep=' ', end='\n', file=None, flush=False)


Docstring:
Prints the values to a stream, or to [Link] by default.

sep
string inserted between values, default a space.
end
string appended after the last value, default a newline.
file
a file-like object (stream); defaults to the current [Link].
flush
whether to forcibly flush the stream.
Type: builtin_function_or_method

In [33]: print?

Signature: print(*args, sep=' ', end='\n', file=None, flush=False)


Docstring:
Prints the values to a stream, or to [Link] by default.

sep
string inserted between values, default a space.
end
string appended after the last value, default a newline.
file
a file-like object (stream); defaults to the current [Link].
flush
whether to forcibly flush the stream.
Type: builtin_function_or_method

import
Some functions are defined in special libraries and will not be loaded automatically.
We need to import these modules before using the functions.
For example, we can import the math module by typing the following line:
[Link] [Link] 9/30
1/14/25, 5:30 PM Python basics

In [34]: import math

then, we can use the values and functions defined in the math module
In [35]: print([Link]) #pi is a constant defined in math module
print([Link](1.25)) #sin() is a function defined in math module

3.141592653589793
0.9489846193555862

Identifiers
Identifer is a name to represent various program elements such as variables,
arrays, functions etc.
Identifiers such as variable names are case sensitive and follow certain rules.
a = 10 is different from A = 10

In [37]: %%optlite --height 300


a = 10
A = 10

Out[37]: OPTWidget(value=None, height=300, script='a = 10\nA = 10\n')

What is the syntax for variable names?


1. Must start with a letter or _ (an underscore) followed by letters, digits, or _ .
2. Must not be a keyword reserved by python:
False await else import pass
None break except in raise
True class finally is return
and continue for lambda try
as def from nonlocal while
assert del global not with
async elif if or yield

Exercise Evaluate the following cell and check if any of the rules above is violated.
{caution}
For the code to run, you must run the initialization cell
that imports ipywidgets:
````python
from ipywidgets import interact

In [38]: @interact
def identifier_syntax(
assignment=[
"a-number = 15",
"a_number = 15",
"15 = 15",
"_15 = 15",

[Link] [Link] 10/30


1/14/25, 5:30 PM Python basics

"del = 15",
"Del = 15",
"type = print",
"print = type",
"input = print",
]
):
exec(assignment)
print("Ok.")

interactive(children=(Dropdown(description='assignment', options=('a-numbe
r = 15', 'a_number = 15', '15 = 15',…

What can we learn from the above examples?

{hint}
- `del` is a keyword and `Del` is not because identifiers
are case sensitive.
- Function names such as `print`, `input`, `type`, etc.,
are not keywords and can be reassigned.
This can useful if you want to modify the default
implementations without changing their source code.

In [39]: print('Hello, World!')


#although print is the name of a function, it's not a keyword
x=print
x('Hello, World!') #equivalent to print('Hello, World!')

Hello, World!
Hello, World!

{seealso}
To help make their code more readable, programmers follow
additional style guides such as [PEP 8]
([Link]
variable-names):
- Function names should be lowercase, with words separated
by underscores as necessary to improve readability.
- Variable names follow the same convention as function
names.
- good variable names: my_age, my_weight
- bad variable names: xyz, abc123

Basic data types


In programming, data type is an important concept.
Variables can store data of different types, and different types can do different
things.
Python has the following data types built-in by default, in these categories:
Text Type: str
Numeric Types: int , float , complex
[Link] [Link] 11/30
1/14/25, 5:30 PM Python basics

Sequence Types: list , tuple , range


Mapping Type: dict
Set Types: set , frozenset
Boolean Type: bool
Binary Types: byte , bytearray , memoryview
In this lecture, we only introduce the most basic data types:
integer: represented by keyword int
floating point number: represented by keyword float
string: represented by keyword str
Integer
How to enter an integer in a program?
In [40]: 15 # an integer in decimal

Out[40]: 15

In [41]: 0b1111 # a binary number

Out[41]: 15

In [42]: 0xF # hexadecimal (base 16) with possible digits 0, 1,2,3,4,5,6,7,8,9,A,

Out[42]: 15

Why all outputs are the same?


What you have entered are integer literals, which are integers written out literally.
All the literals have the same integer value in decimal.
By default, if the last line of a code cell has a value, the jupyter notebook will
store and display the value as an output.
In [43]: 3 # not the output of this cell
4+5+6 #this is the output of this cell

Out[43]: 15

The last line above also has the same value, 15 .


It is an expression (but not a literal) that evaluates to the integer value.
Exercise
Enter an expression that evaluates to an integer value, as big as possible.
(You may need to interrupt the kernel if the expression takes too long to evaluate.)
In [44]: # YOUR CODE HERE
2**1000

[Link] [Link] 12/30


1/14/25, 5:30 PM Python basics

Out[44]: 107150860718626732094842504906000181056140481170553360744375038837035105
112493612249319837881569585812759467291755314682518714528569231404359845
775746985748039345677748242309854210746050623711418779541821530464749835
819412673987675591655439460770629145711964776865421676604298316526243868
37205668069376

{seealso}
Is there a maximum value for an integer for Python3?
See the [documentation]
([Link]
about `[Link]`.

Floating Point Numbers


Not all numbers are integers. In Enginnering, we often need to use fractions.
How to enter fractions in a program?
In [49]: x = -0.1 # decimal number
y = -1.0e-1 # scientific notation
z = -1 / 10 # fraction
x, y, z, type(x), type(y), type(z)

Out[49]: (-0.1, -0.1, -0.1, float, float, float)

In the above code, type() is a python function to return the data type of the
inputs.
Run the code below to see how to use type()
In [50]: type?

Init signature: type(self, /, *args, **kwargs)


Docstring:
type(object) -> the object's type
type(name, bases, dict, **kwds) -> a new type
Type: type
Subclasses: ABCMeta, EnumType, _AnyMeta, NamedTupleMeta, _TypedDictMet
a, _DeprecatedType, _ABC, MetaHasDescriptors, PyCStructType, UnionType,
...

What is the type float ?


float corresponds to the floating point representation.
A float in stored exactly the way we write it in scientific notation:
$$ \overbrace{-}^{\text{sign}} \underbrace{1.0}_{\text{mantissa}\kern-
1em}e\overbrace{-1}^{\text{exponent}\kern-1em}=-1\times 10^{-1} $$
An efficient implementation is more complicated. Try the IEEE-754 Floating Point
Converter.
Special properties of float
[Link] [Link] 13/30
1/14/25, 5:30 PM Python basics

Property 1 . Python stores a floating point with finite precision, which affects the
check for equality. See the code below.
In [51]: x = 10
y = (x ** (1 / 3)) ** 3
x == y

print(x)
print(y)

10
10.000000000000002

Why False? Shouldn't $(x^{\frac13})^3=x$?


Floating point numbers have finite precisions and so
we should instead check whether the numbers are close enough.
How to compare floating point numbers?
isclose() function
syntax: isclose(x,y)
isclose(x,y) function checks whether two values (i.e., x and y) are close to
each other, or not. Returns True if the values are close, otherwise False .
isclose(x,y) function is defined in math module;therefore, we need to first
import math module, then use the function as [Link](x,y)
In [52]: import math #isclose() function is defined in math module, so we need to
x = 10
y = (x ** (1 / 3)) ** 3

print(x == y) #this returns False


print([Link](x,y)) #this returns True . is called member operator

False
True

Property 2 . There is no limit or maximum value of an integer in Python 3. You can


create a value as large as the machine can store. However, there is a maximum value
for floating point number. Run the following code to see the maximum number in your
system (no need to remember how it works).
In [53]: # There is no maximum for an integer for Python3.
# See [Link]

In [54]: import sys #this line imports sys module

print(sys.float_info) #this line will print the information of float data

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)

It cannot represent a number larger than the max :

[Link] [Link] 14/30


1/14/25, 5:30 PM Python basics

In [56]: print(sys.float_info.max * 2)

inf

Property 3: it may keep more decimal places than desired.


In [57]: 1/3

Out[57]: 0.3333333333333333

How to round a floating point number to the desired number of decimal places?
round() function
syntax: round(number, digits)
number -Required. The number to be rounded
digits -Optional. The number of decimals to use when rounding the number.
Default is 0
In [58]: x = 28793.54836
print('round(x)=',round(x)) #round to ones place
print('round(x, 2)=',round(x, 2)) # round to 2 decimal places
print('round(x, 1)=',round(x, 1)) #round to 1 decimal places
print('round(x, 0)=',round(x, 0)) #round to ones place, but return a floa
print('round(x, -1)=',round(x, -1)) #round to tens place
print('round(x, -2)=',round(x, -2)) #round to hundreds place

round(x)= 28794
round(x, 2)= 28793.55
round(x, 1)= 28793.5
round(x, 0)= 28794.0
round(x, -1)= 28790.0
round(x, -2)= 28800.0

In [60]: round(2.665, 2), round(2.675, 2)

Out[60]: (2.67, 2.67)

Why 2.675 rounds to 2.67 instead of 2.68?


The reason is complex,simply speaking it is because the number may not be
represented exactly in binary (optional).
The round function can also be applied to an integer.
In [61]: round(150, -2), round(250, -2)

Out[61]: (200, 200)

Why 250 rounds to 200 instead of 300?


Python 3 implements the default rounding method in IEEE 754. (optional)

Strings
[Link] [Link] 15/30
1/14/25, 5:30 PM Python basics

How to enter a string in a program?


Strings in python are surrounded by either single quotation marks, double
quotation marks, three single quotation marks, or three double quotation marks.
'hello', "hello", '''hello''', """hello""" are the same.
In [62]: print('hello')
print("hello")
print('''hello''')
print("""hello""")

hello
hello
hello
hello

In [66]: #be careful of number and string

15 #this is an integer number


"15" #this is a string

print(15)
print("15")

print(15+5)
print("15+5")

15
15
20
15+5

We can display a string literal with the print() function. Alternatively, we can assign a
string to a variable, then print the variable
In [65]: print('hello')

s = 'hello'
print(s)

print('hello')
hello = 100
print(hello)

hello
hello
hello
100

Escape sequence
To insert characters that are illegal in a string, we need to use escape
sequence .

An escape sequence is a backslash \ followed by the character you want to


insert. \ is called the escape symbol.
[Link] [Link] 16/30
1/14/25, 5:30 PM Python basics

Some common escape sequences in Python are listed below:


Escape sequence Description
\' Single quote

\" Double quotes

\\ Backslash(\)

\n New Line

\t Tab

\b Backspace

\N{name} Character named name in the Unicode database


Try the examples below to see how to use escape sequence in a program
In [67]: print("This is a grinning face: \N{grinning face}")
print("I want to print a double quotes \"")
print("I want to print a single quote \'")

#we can use single quote in a string inside a double quote


print("we can print single quote ' directly inside \"")

#we can use double quote in a string inside a single quote


print('we can print double quotes " directly inside \'')

print('a\tb\tc') #\t means Tab key on your keyboard


print('a\nb\nc') #\n will start a new line
print('abc\b') #\b means Backspace, so c will be removed

print('''I
love
programming''') #we can use triple quotes to enclose multi-line string

This is a grinning face: 😀


I want to print a double quotes "
I want to print a single quote '
we can print single quote ' directly inside "
we can print double quotes " directly inside '
a b c
a
b
c
ab
I
love
programming

Summary of escape sequence


1. To print a simple string, you can use single quote, double quotes, and triple
quotes
2. If you want to print ' inside ' , you need to use escape sequence \'
3. If you want to print " inside " , you need to use escape sequence \"
4. But you can print ' directly inside " . Similarly, you can print " inside '
[Link] [Link] 17/30
1/14/25, 5:30 PM Python basics

5. Triple quotes is usually used to print multiple lines.


Exercise Print a cool multi-line string below.
In [68]: # YOUR CODE HERE
print('''
7------8
/| /|
3------4 |
| | | |
| 5----|-6
|/ |/
1------2
''')

7------8
/| /|
3------4 |
| | | |
| 5----|-6
|/ |/
1------2

{seealso}

Compare you answer with the ASCII arts below:


- Star Wars via Telnet: [Link]
- bashplotlib: [Link]

Pitfall
One common error for beginners is they often mix string up with variable
name

In [70]: # Difference between string and variable name


hello = 5
s = "hello"

print(hello) #this hello is a variable whose value is an integer 5


print("hello") #this "hello" is a string

5
hello

User Input and Output


Input
How to let the user input a value at runtime, i.e., as the program executes?
We can use the method input() :
Syntax: input([prompt])
prompt is the string we wish to display on the screen. It is optional.

[Link] [Link] 18/30


1/14/25, 5:30 PM Python basics

There is no need to delimit the input string by quotation marks.


Simply press enter after typing a string.
In [73]: ?input

Signature: input(prompt='')
Docstring:
Forward raw_input to frontends

Raises
------
StdinNotImplementedError if active frontend doesn't support stdin.
File: /opt/conda/lib/python3.11/site-packages/ipykernel/[Link]
Type: method

In [74]: x = input('Please input a number: ')


x

Out[74]: '28'

In [72]: x = input()
x

Out[72]: '8'

From the examples above, we can see that


The input method prints its argument, if any, as a prompt.
It takes user's input and returns it as a string, not a number!!
Output
How to let your program outputs some data?
We can use the method print() :
Syntax: print()
The print() function prints the specified message to the screen, or other
standard output device.
The message can be a string, or any other object, the object will be converted into a
string before written to the screen.
In [75]: print("Hello") #print one string
print("Hello,","how are you?") #print two strings
x=10
print("the value of x is:", x) #print one string and one integer

Hello
Hello, how are you?
the value of x is: 10

Parameters of print()
print(object(s), sep=separator, end=end)

[Link] [Link] 19/30


1/14/25, 5:30 PM Python basics

object(s) Any object, and as many as you like. Will be converted to string
before printed
sep='separator' Optional. Specify how to separate the objects, if there is
more than one. Default is ' '
end='end' Optional. Specify what to print at the end. Default is '\n' (new line)

In [76]: print("I","love","CS1302!") #By default, strings are separated by space '

#change the separator to other characters


print("I","love","CS1302!", sep='-')
print("I","love","CS1302!", sep='*')

I love CS1302!
I-love-CS1302!
I*love*CS1302!

In [77]: #by default, the ending character is `\n` which starts a new line
print("I")
print("love")
print("CS1302")

#compare the difference


print("I", end='')
print("love", end='')
print("CS1302!")

print("I", end='*')
print("love", end='*')
print("CS1302!")

#what does the following code do?


print() #it prints an empty line

print("I", end='')
print()
print("love", end='')
print()
print("CS1302!")

I
love
CS1302
IloveCS1302!
I*love*CS1302!

I
love
CS1302!

Exercise Explain whether the following code prints 'My name is Python' . Does
print return a value?

In [78]: print('My name is', print('Python'))

Python
My name is None

In [79]: %%ai chatgpt -f text

[Link] [Link] 20/30


1/14/25, 5:30 PM Python basics

Please explain why the following python code does not print 'My name is P
print('My name is', print('Python'))

Out[79]: ## Explanation of the Issue

The reason why the code does not print 'My name is Python' as expected i
s due to the way the `print()` function works in Python.

### The `print()` Function

In Python, the `print()` function is used to output text to the screen.


However, it does not return a value that can be used in an expression. I
nstead, it returns `None` by default.

### The Code

Let's break down the given code:


```python
print('My name is', print('Python'))
```
Here's what happens when this code is executed:

1. **Inner `print()`**: The inner `print('Python')` is executed first. T


his prints 'Python' to the screen and returns `None`.
2. **Outer `print()`**: The outer `print()` function is then called with
two arguments: the string 'My name is' and the result of the inner `prin
t()`, which is `None`.
3. **Output**: The outer `print()` function prints 'My name is' followed
by the string representation of `None`, which is just `None`. So, the ou
tput is:
```
My name is None
Python
```
As you can see, 'My name is Python' is not printed.

## Corrected Code

To achieve the desired output, you can simply use the `print()` function
with both strings:
```python
print('My name is', 'Python')
```
Alternatively, you can use string concatenation:
```python
print('My name is ' + 'Python')
```
Or, using f-strings (Python 3.6+):
```python
print(f'My name is Python')
```

Answer: print() function returns None. The None keyword is used to define a null
value, or no value at all. It returns a value! It is None.

Type Conversion
[Link] [Link] 21/30
1/14/25, 5:30 PM Python basics

Python defines type conversion functions to directly convert one data type to
another which is useful in day-to-day and competitive programming.
There are two types of Type Conversion in Python:
Implicit Type Conversion
Explicit Type Conversion
Implicit Type Conversion
In Implicit type conversion, Python automatically converts one data type to another
data type. This process doesn't need any user involvement.
Let's see an example where Python promotes the conversion of the lower data type
(integer) to the higher data type (float) to avoid data loss.
In [80]: # Converting integer to float
num_int = 123
num_flo = 1.23

num_new = num_int + num_flo

print("datatype of num_int:",type(num_int))
print("datatype of num_flo:",type(num_flo))

print("Value of num_new:",num_new)
print("datatype of num_new:",type(num_new))

datatype of num_int: <class 'int'>


datatype of num_flo: <class 'float'>
Value of num_new: 124.23
datatype of num_new: <class 'float'>

In the above program,


We add two variables num_int and num_flo , storing the value in num_new .
We will look at the data type of all three objects respectively.
In the output, we can see the data type of num_int is an integer while the data
type of num_flo is a float.
Also, we can see the num_new has a float data type because Python always
converts smaller data types to larger data types to avoid the loss of data.
Note that the data type of a variable, it actually means the data type of the value
the variable is holding
Now, let's try adding a string and an integer, and see how Python deals with it.
In [82]: %%optlite --height 400
# Addition of string(higher) data type and integer(lower) datatype
num_int = 123
num_str = "456"

print("Data type of num_int:",type(num_int))


print("Data type of num_str:",type(num_str))

[Link] [Link] 22/30


1/14/25, 5:30 PM Python basics

print(num_int+num_str)

Out[82]: OPTWidget(value=None, height=400, script='# Addition of string(higher) d


ata type and integer(lower) datatype\n…

In the above program,


We add two variables num_int and num_str .
As we can see from the output, we got TypeError . Python is not able to use
Implicit Conversion in such conditions.
However, Python has a solution for these types of situations which is known as
Explicit Conversion.
Explicit Type Conversion
In Explicit Type Conversion in Python, the data type is manually changed by the user
as per their requirement. Various forms of explicit type conversion are explained
below:
1. int() : This function converts any data type to integer.
2. float() : This function converts any data type to a floating-point number
3. str() : This function converts any data type to a string
This type of conversion is also called typecasting because the user casts (changes)
the data type of the objects.
In [83]: x = 5 # x is an integer
y = 1.2 #y is a floating point number
z = "100" #z is a string

#example of int()
y_int = int(y)
z_int = int(z)
print(type(y_int))
print(type(z_int))

#example of float()
x_float = float(x)
z_float = float(z)
print(type(x_float))
print(type(z_float))

#example of str()
x_str = str(x)
y_str = str(y)
print(type(x_str))
print(type(y_str))

<class 'int'>
<class 'int'>
<class 'float'>
<class 'float'>
<class 'str'>
<class 'str'>

[Link] [Link] 23/30


1/14/25, 5:30 PM Python basics

Note that the input of these functions must be valid, see examples below
In [84]: %%optlite --height 400
x = '123abc'

print(int(x)) #this is wrong


print(float(x)) #this is wrong

Out[84]: OPTWidget(value=None, height=400, script="x = '123abc'\n\nprint(int(x))


#this is wrong\nprint(float(x)) #this …

In [85]: x = '123'

print(int(x)) #this is correct


print(float(x)) #this is correct

123
123.0

What happens when we add strings together?


In [86]: "4" + "5" + "6"

Out[86]: '456'

How to fix the bug then?


We can convert a string to an integer using int .
In [88]: int("4") + int("5") + int("6")

Out[88]: 15

String Formatting
How to format a string to the desired format?
Syntax: [Link](value1, value2...)
The format() method formats the specified value(s) and insert them inside
the string's placeholder.
The format() method returns the formatted string.
value1, value2... Required. One or more values that should be formatted
and inserted in the string.
Example 1
In [89]: x = 10000/3
print('x ≈',x)
print('x ≈ {:.2f} (rounded to 2 decimal places)'.format(x))
x

x ≈ 3333.3333333333335
x ≈ 3333.33 (rounded to 2 decimal places)
Out[89]: 3333.3333333333335

[Link] [Link] 24/30


1/14/25, 5:30 PM Python basics

{:.2f} is a place holder


that gets replaced by a string
that represents the argument x of format
according to the format specification .2f , i.e.,
a decimal floating point number rounded to 2 decimal places.
Exercise Play with the following widget to learn the effect of different format
specifications. In particular, print 10000/3 as 3,333.33 .
In [90]: from ipywidgets import interact

@interact(
x="10000/3",
align={"None": "", "<": "<", ">": ">", "=": "=", "^": "^"},
sign={"None": "", "+": "+", "-": "-", "SPACE": " "},
width=(0, 20),
grouping={"None": "", "_": "_", ",": ","},
precision=(0, 20),
)
def print_float(x, sign, align, grouping, width=0, precision=2):
format_spec = (
f"{{:{align}{sign}{'' if width==0 else width}{grouping}.{precisio
)
print("Format spec:", format_spec)
print("x ≈", format_spec.format(eval(x)))

interactive(children=(Text(value='10000/3', description='x'), Dropdown(des


cription='sign', options={'None': ''…

Example 2
String formatting is useful for different data types other than float .
E.g., consider the following program that prints a time specified by some variables.
In [91]: # Some specified time
hour = 12
minute = 34
second = 56

print("The time is " + str(hour) + ":" + str(minute) + ":" + str(second)+

The time is 12:34:56.

Imagine you have to show also the date in different formats.


The code can become very hard to read/write because
the message is a concatenation of multiple strings and
the integer variables need to be converted to strings.
Omitting + leads to syntax error. Removing str as follows also does not give the
desired format.
In [92]: print("The time is ", hour, ":", minute, ":", second, ".") # note the ex

[Link] [Link] 25/30


1/14/25, 5:30 PM Python basics

The time is 12 : 34 : 56 .

To make the code more readable, we can use the format function as follows.
In [93]: message = "The time is {}:{}:{}."
print([Link](hour, minute, second))

The time is 12:34:56.

{note}

- We can have multiple *place-holders* `{}` inside a


string.
- We can then provide the contents (any type: numbers,
strings..) using the `format` function, which
- substitutes the place-holders by the function arguments
from left to right.

According to the string formatting syntax, we can change the order of substitution
using
indices (0 is the first item) or
names inside the placeholder {} :
In [94]: print("You should {0} {1} what I say instead of what I {0}.".format("do",
print("The surname of {first} {last} is {last}.".format(first="John", las

You should do only what I say instead of what I do.


The surname of John Doe is Doe.

Example 3
You can even put variables inside the format specification directly and have a nested
string formatting.
In [95]: align, width = "^", 5
print(f"{{:*{align}{width}}}".format(x)) # note the syntax f"..."

3333.3333333333335

In the above, f"..." is special syntax for formatted string.


Exercise Play with the following widget to learn more about the formating
specification.
1. What happens when align is none but fill is * ?
2. What happens when the expression is a multi-line string?
In [96]: from ipywidgets import interact

@interact(
expression=r"'ABC'",
fill="*",
align={"None": "", "<": "<", ">": ">", "=": "=", "^": "^"},
width=(0, 20),

[Link] [Link] 26/30


1/14/25, 5:30 PM Python basics

)
def print_objectt(expression, fill, align="^", width=10):
format_spec = f"{{:{fill}{align}{'' if width==0 else width}}}"
print("Format spec:", format_spec)
print("Print:", format_spec.format(eval(expression)))

interactive(children=(Text(value="'ABC'", description='expression'), Text


(value='*', description='fill'), Drop…

Error
In addition to writing code, a programmer spends significant time in debugging code
that contains errors.
Can an error be automatically detected by the computer?
In [97]: #example of runtime error
x = 5
y = 0
print(x/y)

--------------------------------------------------------------------------
-
ZeroDivisionError Traceback (most recent call las
t)
Cell In[97], line 4
2 x = 5
3 y = 0
----> 4 print(x/y)

ZeroDivisionError: division by zero

You have just seen an example of runtime error, which is due to an error in the
logic.
The ability to debug or even detect such error is, unfortunately, beyond python's
intelligence.
Other kinds of error may be detected automatically.
As an example, note that we can omit + for string concatenation, but we cannot
omit it for integer summation:
In [98]: print('Skipping + for string concatenation')
'4''5' '6'

Skipping + for string concatenation


Out[98]: '456'

In [99]: print('Skipping + for integer summation')


4 5 6

Cell In[99], line 2


4 5 6
^
SyntaxError: invalid syntax

[Link] [Link] 27/30


1/14/25, 5:30 PM Python basics

Python interpreter detects the bug and raises a syntax error.


Why Syntax error can be detected automatically?
Why is the print statement before the error not executed?
The Python interpreter can detect syntax error even before executing the code
because
the interpreter simply fails to translate the code to lower-level executable code.
The following code raises a different kind of error.
In [100… print("Evaluating '4' + '5' + 6")
'4' + '5' + 6 # summing string with integer

Evaluating '4' + '5' + 6


--------------------------------------------------------------------------
-
TypeError Traceback (most recent call las
t)
Cell In[100], line 2
1 print("Evaluating '4' + '5' + 6")
----> 2 '4' + '5' + 6 # summing string with integer

TypeError: can only concatenate str (not "int") to str

Why Python throws a TypeError when evaluating '4' + '5' + 6 ?


There is no implementation of + operation on a value of type str and a value of
type int .
Unlike the syntax error, the Python interpreter can only detect a type error at
runtime (when executing the code.)
Hence, such an error is called a runtime error.
Do other languages work in the same way?
The following cell runs the corresponding javascript code using a cell magic:
In [101… %%javascript
let x = '4' + '5' + 6;
[Link](x + ' ' + typeof(x));
// no error because 6 is converted to a str implicitly

In [102… %%javascript
let x = '4' * '5' * 6;
[Link](x + ' ' + typeof(x));
// no error because 4 and 5 are converted to numbers implicitly

Javascript is called is a weakly-typed language because it forces a type conversion


to avoid a type error.

[Link] [Link] 28/30


1/14/25, 5:30 PM Python basics

Python is a strongly-and-dynamically-typed language:


Strongly-typed: Python does not force a type conversion to avoid a type error.
Dynamically-typed: Python checks data type only at runtime after translating the
code to machine code.
In comparison, C/C++ and Java are statically-typed languages that checks data type
during compilation.
In [103… try:
!gcc [Link]
except Error:
print('Cannot run shell command.')

[Link]: In function ‘int main()’:


[Link]:22: error: invalid operands of types ‘const char [2]’ and ‘const
char [2]’ to binary ‘operator+’
4 | std::cout << "4" + "5" + 6 << std::endl;
| ~~~ ^ ~~~
| | |
| | const char [2]
| const char [2]

In [104… try:
!javac [Link]
except Error:
print('Cannot run shell command.')

[Link]: error: bad operand types for binary operator '*'


[Link]("4" * "5" * 6);
^
first type: String
second type: String
1 error

{note}

A weakly-typed language may seem more robust, but it can


lead to [more logical errors]
([Link]
javascript/9781449339203/[Link]).

- Javascript is [tricky]
([Link]
- To improve readability and avoid logical errors,
[typescript]([Link] is a
strongly-typed replacement of javascript.

Exercise Not all the strings can be converted into integers. Try breaking the following
code by providing invalid inputs and record them in the subsequent cell. Explain
whether the errors are runtime errors.
In [106… num1 = input('Please input an integer: ')
num2 = input('Please input another integer: ')
print(num1, '+', num2, 'is equal to', int(num1) + int(num2))

[Link] [Link] 29/30


1/14/25, 5:30 PM Python basics

--------------------------------------------------------------------------
-
ValueError Traceback (most recent call las
t)
Cell In[106], line 3
1 num1 = input('Please input an integer: ')
2 num2 = input('Please input another integer: ')
----> 3 print(num1, '+', num2, 'is equal to', int(num1) + int(num2))

ValueError: invalid literal for int() with base 10: 'rt'

ANSWER: the errors are runtime error

[Link] [Link] 30/30

You might also like