Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 17

Lecture 1: Introduction to Python

What is Python?
Python is a popular programming language. It was created by Guido van Rossum, and released in 1991.

It is used for:

 web development (server-side),


 software development,
 mathematics,
 system scripting.

What can Python do?


 Python can be used on a server to create web applications.
 Python can be used alongside software to create workflows.
 Python can connect to database systems. It can also read and modify files.
 Python can be used to handle big data and perform complex mathematics.
 Python can be used for rapid prototyping, or for production-ready software development.

Why Python?
 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-orientated way or a functional way.

“Hello World” programs in different languages:

C# C++ JAVA Python

1
Installing Python on Windows 10:
1) Go to www.python.org
2) Click “Downloads” Link at the top of the page

3) Click “Download Python 3.5.2” (or whatever the 3.X version currently is:

2
4) When the
installation window comes up, click “Install Now”
a. You can choose to check the “Install launcher for all users (recommended) or not – either way should be
OK
b. You can choose to “Add Python 3.8 to PATH” or not – either way should be OK
c. Note: Depending on how Windows is set up, you might need to provide an administrator password to
install on your system at this point.
d. You can choose to “Customize Installation” if you want, especially if you want to install to a location
other than the default one shown. Generally I recommend installing to the default location unless you
have a problem doing so.
e. In any case, you might want to note the location of the installation in case you have difficulty later. If
you are specifying the location yourself, put it in a location you are likely to easily find/remember.

5) You should see Python installing at this point. When it finishes, you should see a screen that says the installation
was successful. You can click “Close”
Installing PyCharm
1) Go to www.jetbrains.com/pycharm
2) Click the “Download” button at top left (you can also scroll down the page and click the download link for the
community version directly)

3) Click the “DOWNLOAD” link under the Community edition


4) When the download box pops up, click “Save” and then “Run”.
a. Depending on your computer’s setup, you might need to enter an administrator password at this point.
5) The setup wizard should have started. Click “Next”

6) Choose an installation location. I recommend letting it install in the suggested location. Click Browse if you
want to enter a new location, and when you are done selecting the location, click “Next”.
7) You can choose to select two options, after which you click “Next”
a. You can create a desktop shortcut (I suggest doing so). This will put a link to PyCharm on your desktop.
b. You can create associations (I suggest doing so). This means that when you open a python file (one with
a .py extension), it will open in PyCharm

8) Choose a start menu folder (I suggest leaving the default “Jetbrains” selected, but you can change it if you wish)
and click “Install”
9) Wait for installation to finish. At the end, you should receive a message screen that PyCharm is installed. You
can click “Finish”, and if you want to go ahead and run it, click the “Run PyCharm Community Edition” box
first. You have now installed PyCharm
Running PyCharm
1) The first time you run PyCharm, you will get a message box asking about importing settings. You can select
“I do not have a previous version of PyCharm or I do not want to import my settings” and click “OK”

2) The first time you run PyCharm, you will need to accept the privacy policy.

3) The first time you run PyCharm, you will have some “Initial Configuration” options. Just hit “OK”
4) You will now be at the intro screen for PyCharm. This will also be the intro screen when you start PyCharm
in the future without a recently opened project (if you do have a recently opened one you did not close
before exiting, it will automatically reopen to that one). Click on “Create New Project”

5) You will need to select a location.


a. When it asks for the location, you can select where you want the project to be created. I suggest at
least changing the name from “untitled” to something more meaningful, like “FirstProject”
b. PyCharm should have found the Python interpreter you installed earlier (when you installed Python
itself). It will be selected if so. See TROUBLESHOOTING below for what to do if you do NOT
have an Interpreter listed. If you do not have an interpreter listed, you need to resolve this
before you can continue.
c. Click the “Create” Button when you have the project set up
6) This will bring up the PyCharm environment

7) Try creating and running a program. Go up to the “File” menu and select “New” python file
Python Keywords and Identifiers
Keywords are the reserved words in Python.
We cannot use a keyword as a variable name, function name or any other identifier. They are used to
define the syntax and structure of the Python language.
In Python, keywords are case sensitive.
All the keywords except True, False and None are in lowercase and they must be written as they are.
The list of all the keywords is given below.

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

Python Identifiers
An identifier is a name given to entities like class, functions, variables, etc. It helps to differentiate
one entity from another.

Rules for writing identifiers

1. Identifiers can be a combination of letters in lowercase (a to z) or uppercase (A to Z) or


digits (0 to 9) or an underscore  _ . Names like  myClass ,  var_1  and  print_this_to_screen , all are
valid example.
2. An identifier cannot start with a digit.  1variable  is invalid, but  variable1  is a valid name.
3. Keywords cannot be used as identifiers.

global = 1

Output

File "<interactive input>", line 1


global = 1
^
SyntaxError: invalid syntax

4. We cannot use special symbols like !, @, #, $, % etc. in our identifier.

a@ = 0

Output

File "<interactive input>", line 1


a@ = 0
^
SyntaxError: invalid syntax

5. An identifier can be of any length.

Python Data Types


Data types are the classification or categorization of data items. It represents the kind of value that tells
what operations can be performed on a particular data. Since everything is an object in Python
programming, data types are actually classes and variables are instance (object) of these classes.
Program 1: Hello World

print ("Hello world")

output:
Hello world

Program 2: Changing the value of a variable

website = "apple.com"
print(website)
website = "programiz.com"
print(website)

output:
apple.com
programiz.com

Program 3: Assigning multiple values to multiple variables

a, b, c = 5, 3.2, "Hello"
print (a)
print (b)
print (c)

output:
5
3.2
Hello

Program 4: Combine two number variables

num1=9
num2=4
result=num1+num2
print(result)

output:
13

Program 5: Combine two string variables


firstname="sara"
lastname="adnan"
fullname=firstname+" "+lastname
print(fullname)

output:
sara adnan

Program 6: Combine two strings of the keyboard and print the result

firstname=input("enter first name: ")


lastname=input("enter last name: ")
fullname=firstname+" "+lastname
print(fullname)

output:
enter first name: ahmed
enter last name: zaid
ahmed zaid

Program 7: Combine two number of the keyboard and print the result

num1=int(input("enter first number: "))


num2=int(input("enter second number: "))
result=num1+num2
print("the result:",result)

output:
enter first number: 4
enter second number: 5
the result: 9

Program 8: Square of the number

number1=int(input("enter the number:"))


sqr=number1**2
print("the square=",sqr)

output:
enter the number: 5
the square= 25

Program 8: Find the average three numbers of the keyboard and print the result
number1=int(input("Enter the first number: "))
number2=int(input("Enter the second number: "))
number3=int(input("Enter the third number: "))
total = number1 + number2 + number3
average = total / 3
print ("The average is: ",average)

output:
Enter the first number: 5
Enter the second number: 4
Enter the third number: 6
The average is: 5.0

Program 9: Create simple calculater

num1=int(input('please enter first number:'))


num2=int(input('please enter second number:'))
summation_result=num1+num2
subtraction_result=num1-num2
multiplication_result=num1*num2
division_result=num1/num2
remainder_result=num1%num2
print('the summation result is :' , summation_result)
print('the subtraction result is :',subtraction_result)
print('the multiplication result is :',multiplication_result)
print('the division result is :',division_result)
print('the remainder is :',remainder_result)

output:
enter first number: 4
enter second number: 5
the result: 9

You might also like