CH1_Introduction_RW
CH1_Introduction_RW
Python
What is python 2
Python works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc).
Python has a simple syntax similar to the English language.
Python has syntax that allows developers to write programs with fewer lines
than some other programming languages.
Python runs on an interpreter system, meaning that code can be executed
as soon as it is written. This means that prototyping can be very quick.
Python can be treated in a procedural way, an object-oriented way or a
functional way.
You should know 5
You can download the latest version for Windows (Python 3.12.2) by following this link:
https://www.python.org/downloads/
https://drive.google.com/drive/folders/18rgT9kGrrAXVWkqLsoIBixjstVN-c2gf?usp=sharing
If you type >>> print("Welcome to Python") and press the Enter key.
The string Welcome to Python appears on the console. Note that
Python requires double or single quotation marks around strings to
delineate them from other code.
Note:
• The number of spaces is up to you as a programmer, but it has to be at least one.
• You have to use the same number of spaces in the same block of code, otherwise
Python will give you an error:
2 or more statement 11
Python does not really have a syntax for multi line comments.
Solution is:
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:
Output
Casting 16
If you want to specify the data type of a variable, this can be done
with casting.
Ex:
Output:????
Variable names 17
A variable can have a short name (like x and y) or a more descriptive name
(age, carname, total_volume). Rules for Python variables' variable name must
start with a letter or the underscore character
A variable name cannot start with a number
A variable name can only contain alpha-numeric characters and underscores
(A-z, 0-9, and _ )
Variable names are case-sensitive (age, Age and AGE are three different
variables)
Legal Illegal
Variables - Assign Multiple Values 18
And you can assign the same value to multiple variables in one line:
Unpacking collection of variables 19
List, tuple(Array)
Mathematical computations 20
The math module provides the mathematical functions listed in the following table. To use these
functions in a program, the math module must be imported (import math). Example of use:
math.sqrt
Interpreter 30
Statements
Another way 35
Ex2: 36
While loop
The syntax for the while loop is:
while condition:
Statements
For loop 39
Syntax:
− for i in range(initialValue, endValue):
# Loop body
Output
Output
44
Output
46
i=2
while i < 20:
j=2
while j <= (i / j):
if i % j == 0:
break
j=j+1
if j > i / j:
print(i, "is prime")
i=i+1
Functions 47
return x + y
return x - y
num1 = 10
num2 = 5
function:
53
program:
Program 1 Program 2
Python allows a function to return 55
multiple values
56
There are four collection data types in the Python programming language:
1. List is a collection which is ordered and changeable. Allows duplicate
members.
2. Tuple is a collection which is ordered and unchangeable. Allows duplicate
members.
3. Set is a collection which is unordered and unindexed. No duplicate
members.
4. Dictionary is a collection which is unordered and changeable. No
duplicate members.
When choosing a collection type, it is useful to understand the properties of that
type. Choosing the right type for a particular data set could mean retention of
meaning, and, it could mean an increase in efficiency or security.
Array(List) 60
Arrays (also called lists) are used to store multiple values in one single
variable.
You can access the values by referring to an index number starting
from 0.
− You can create an array by using the following syntax:
A1 = []
A2 = [2, 3, 4]
A3 = ["red", "green"]
Array(List) 61
You may often need code that reads data from the console into an array. You
can enter one data item per line and append it to an array in a loop.
For example, the following code reads 5 numbers one per line into an array,
then it will display them in reverse order
Array(List) 67
Sometimes it is more convenient to enter the data in one line separated by spaces.
You can use the string’s split method to extract data from a line of input
Note: To copy the data in one array to another array, you have to copy individual elements
from the source array to the target array.
68
arr = input_str.split()
s=0
num = eval(arr[x])
s = s + num
print("\nSum =", s)
Array(List) 69
def read(arr):
for i in range(5):
arr[i] = eval((input("enter a number: ")))
return arr
arr = [None] * 5
arr = read(arr)
write(arr)
String 71
Slicing
You can return a range of characters by using the slice syntax.
Specifythe start index and the end index, separated by a
colon, to return a part of the string.
Ex:
String 78