Python Intro CN
Python Intro CN
Introduction to Python
Python is an easy-to-learn and a powerful Object-Oriented Programming language. It is a
very high-level programming language.
Why Python?
1. Easy to Use: Python is comparatively an easier-to-use language as compared to
other programming languages.
2. Expressive Language: The syntax of Python is closer to how you would write
pseudocode. Which makes it capable of expressing the code’s purpose better than
many other languages.
Python Download
The very first step towards Python Programming would be to download the tools required
to run the Python language. We will be using Python 3 for the course. You can download
the latest version of Python 3 from h
ttps://www.python.org/downloads/
Note:- If you are using Windows OS, then while installing Python make sure that “Add
Python to PATH“ is checked.
Getting an IDE for writing programs:
You can use any IDE of your choice, however, you are recommended to use Jupyter
Notebook. You can download it from https://jupyter.org/install
1
Working in Python
Once you have Python installed on your system, you are ready to work on it. You can work
in Python in two different modes:-
a) Interactive Mode: In this mode, you type one command at a time and Python
executes the same. Python’s interactive interpreter is also called Python Shell.
b) Script Mode: In this mode, we save all our commands in the form of a program file
and later run the entire script. After running the script, the whole program gets
compiled and you’ll see the overall output.
As we are just getting started with Python, we will start with the most fundamental
program which would involve printing a standard output to the console. The print()
function is a way to print to the standard output. The syntax to use print() function is as
follows:-
In[] : print(<Objects>)
Example: If we want to print “Hello, World!” in our code, we will write it in the following
way:-
2
Note:- T
he quotes that are on either side of Hello, World! were not printed to the screen
because they are used to tell Python that they contain a string. The quotation marks delineate
where the string begins and ends.
Variables in Python
What are Variables?
A variable in Python represents a named location that refers to value and whose values can
be used and processed during the program run. In other words, variables are labels/names
to which we can assign value and use them as a reference to that value throughout the
code.
3
operator and one to the right, and adds them together. Likewise, the “=” operator takes a
value to the right of the operator and assigns it to the name/label/variable on the left of the
operator.
For Example: Now let us create a variable namely Student to hold a student’s name and a
variable A
ge to hold a student’s age.
tudent = "Jacob"
>>> S
>>> A ge = 19
Python will internally create labels referring to these values as shown below:
Now, let us modify the first program we wrote.
Here, the Python program assigned the value of the string to a variable g
reeting, and
then when we call p
rint(greeting), it prints the value that the variable, greeting, points
to i.e. "Hello, World!"
We get the output as:-
Hello, World!
Naming a Variable
You must keep the following points in your mind while naming a variable:-
4
● Variable names are case sensitive. For example:- The variable names Temp and
temp are different.
● While writing a program, creating self-explanatory variable names help a lot in
increasing the readability of the code. However, too long names can clutter up the
program and make it difficult to read.
Age = 20
Age = 3 0 # Re-assigning a different value to the same variable
5
Python preloads some commonly used values in an area of memory. This memory space
has values/literals at defined memory locations and all these locations have different
addresses.
6
Data Types
Introduction
Data types are the classification or categorization of data items. Data types
represent a kind of value that determines what operations can be performed on
that data. Numeric, non-numeric, and Boolean (true/false) data are the most used
data types. However, each programming language has its classification largely
reflecting its programming philosophy. Python offers the following built-in data
types:
● Numbers
○ Integers
○ Floating Point Numbers
○ Complex Numbers
● Strings
● Boolean Values
● List, Tuple, and Dictionary ( To be covered later in the course )
7
int Integers 4
Note:- If a variable has been assigned a value of some data type. It can be reassigned a
value belonging to some other Data Type in the future.
Python Numbers
Introduction
Number data types store numerical values. Python supports Integers, floating-point
numbers, and complex numbers. They are defined as int, float, and complex classes.
● Integers can be of any length (Only limited by the memory available). They do not
have a decimal point and can be positive or negative.
● A floating-point number is a number having a fractional part. The presence of a
decimal point indicates a floating-point number. They have a precision of up to 15
digits.
● 1 is an integer, 1.0 is a floating-point number.
● Complex numbers are of the form, x + yj, where x is the real part and y is the
imaginary part.
We can use the type() function to know which class a variable or a value belongs to.
Similarly, the i
sinstance() function is used to check if an object belongs to a particular
class.
8
b = 5
print(b, " is of type", type(b))
b = 2.0
print(b, " is of type", type(b))
b = 1+2j
print(b, " is complex number?", isinstance(b,complex))
The Arithmetic Operators are used in Python in the same way as they are used in
Mathematics.
OPERATOR DESCRIPTION
** Exponent Operator- The first operand raised to the power of the second
operand
9
Let us see how these operators work:-
To get the input from the user interactively, we can use the built-in function, input(). This
function is used in the following manner:
For example:
10
We will get the following interactive output:
In[] : name = input("Enter your name: ")
Enter your name: Rishabh #User Input
In[] : age = input("Enter your age: ")
Enter your age: 20 #User Input
In[] : name
Out[] : 'Rishabh'
In[] : age
Out[] : '19'
Note:- input() function always returns a value of the String type. Notice that in the
above script the output for both name and age, Python has enclosed the output in quotes,
like 'Rishabh' and '
19', which implies that it is of S
tring type. This is just because,
whatever the user inputs in the input() function, it is treated as a S
tring. This would
mean that even if we input an integer value like 20, it will be treated like a string '
19' and
not an integer. Now, we will see how to read Numbers in the next section.
Reading Numbers
Here, variableRead is a String type that was read from the user. This string value will then
be converted to Integer using the i
nt() function and assigned to updatedVariable.
This can even be shortened to a single line of code as shown below:-
11
12