Detailed course on the fundamentals of python first programs
Introduction to Programming Language Python
Overview
- Introduction to Programming and Python
- Setup environment Python in your computer
- Hello World
- Calculator in Python
- Commenting
- Variable in Python
- String in Python
- Taking an input from user
- Selection
- Looping
Introduction to Programming
- Programming is an algorithm(steps) that has been write using programming language, so that it can be executed by a computer.
- Example of popular Programming Language:
- Python
- C++
- C
- Java
- PHP
Introduction to Programming (Cont.)
- To get a simple idea about programming is it is a step by step to solve a problem.
- Basic procedure in programming is:
- Input
- Process
- Output
Process
Input
Output
Introduction to Python
- Python is an easy to learn, powerful programming language.
- It has efficient high-level data structures and a simple but effective approach to objectoriented programming.
- Suitable for beginner to get into programming.
Setup environment Python in your computer
- Download the installer from official Python websites:
- Click at a link named Windows x86 executable installer
- If already have, then double click the installer. Name of the installer is something like this “python-3.6.1.exe”
Setup environment Python in your computer
- This window will pop up after you double click the installer
- Check the radio button “Add Python 3.6 to Path” (in red rectangle shape)
- Then click Install Now
Setup environment Python in your computer
- Wait until the installation finished
- Click close button
Setup environment Python in your computer
- Next open command prompt, by type on your keyboard Start + R
- Then type cmd and click Okay.
p/s: Other method that will work is go to search, and type CMD, then click on the result named „Command Prompt‟
Setup environment Python in your computer
- Command Prompt will popup.
- Type „python‟ and it will display text like in the picture
- Congratulations! You success install Python in your computer
Setup environment Python in your computer
Installing PyCharm
- If you using Windows, download
Setup environment Python in your computer
- Install it as usual. Just click Next and Okay until finish install.
- Open PyCharm, and just click okay if a dialog popup for configuration.
- Then you will see the front page of PyCharm like in the picture]
- Click Create New Project
Setup environment Python in your computer
- Type Introduction as name of your project
- Click Create
Setup environment Python in your computer
- Right click on the Introduction
Project
- Choose New
- Click Python File
Setup environment Python in your computer
- Type „Hello world‟ and make sure Kind textbox is Python file
- Click OK
Setup environment Python in your computer
If you find that the font is too small, then you need to resize it
- Click on the File -> Settings or just simply press Ctrl + Alt + S
- Go to Editor -> General
- Check the radio button „Change font size (zoom) with Ctrl + Mouse Wheel‟
- Click OK
- Then you just need to press Ctrl + Mouse Wheel to resize the font
Setup environment Python in your computer
- So as you can see, the font I much bigger now
- Here is the simple tour before you start learn python:
- Called Editor, where you will write your Python code here
- Called Run toolbar, where you will click the „Play‟ button to run the code
- List directory and file in PyCharm project
Hello World - Simple command in all programming language tutorial is print „Hello world!‟
- In Python, to display output, the syntax is „print‟ followed by open and close bracket.
- Example:
print(„Hello world‟)
Output : Hello world
- Click the play button on the top right hand side to run the code
- The output is at the below window Run code
Calculator in Python
- Create another file named „Calculator‟
- You can do calculation by straight away right the formula
- In most programming language:
- + symbol is plus/add
- - symbol is minus/subtract
- / symbol is divide
- * symbol is multiply
- Example:
2 + 2
Output: 4
50 – 4*6
Output: 26
50 – (40 + 6) * 4 / 10
Output: 31.6
Calculator in Python (Cont.)
- Calculate power, using double asterisk „**‟
- For example we want to calculate 52 5 ** 2
Output: 25 2 ** 3
Output: 8
Calculator in Python (Cont.)
- Calculate remainder of the division using symbol percent „%‟
- For example we want to
calculate remainder of 17 divide by 3 17 % 3
Output: 2
Commenting
- In computer programming, a comment is a programmer readable explanation or annotation in the source code of a computer program. They are added with the purpose of making the source code easier for humans to understand, and are generally ignored by compilers and interpreters.
- In Python, # or hashtag is used to declare it as a comment.
- For example:
# print(2 + 2)
Output: Nothing because it is commented
Variable in Python
- The problem with previous example is the value calculated is not stored in the memory. So if you want to use it again, then you need to type the formula again.
- To overcome this problem, variable is used.
- Assign the value to the variable.
- For example:
Height = 142
Width = 34
- The variable is Height with value 142, second variable is Width with value 34
Variable in Python (Cont.)
- You also can assign a formula to a variable
- For example:
Area = Height * Width
print(Area)
Output:
- Example above shows that, variable Height and Width is used to calculate area. The value is stored in variable Area.
Exercise for Variable Show the calculation of BMI using Python 01
Formula for BMI:
- • BMI = (Weight / (Height x Height)) 02
All values must be assigned to the variables 03
Answer for Exercise
- Assign the value to Height and Weight variable
- Then make a formula and assign it to BMI variable
- There are many ways you can do to calculate BMI as shown in the picture
Strings in Python
- Strings is enclosed in quotes and special character are escaped with backslashes.
- For example: print(„Is it true?‟)
Output: Is it true?
Print(„Isn\‟t it true?‟)
Output: Isn‟t it true?
- There are two examples given above. First example is easy to understand. The second example is different from the first one, where it has backslash before the quotes.
- It means that the quote is a special character, so you need to use backslash in order to display it. If you don‟t put the backslash, then it will show an error.
Strings in Python (Cont.)
- Previous example just show how to print the output in one line. How to about to print an output that has multiple lines?
- Using „\n‟ to print a new line
- For example:
nama = „Muhammad\nAhmad\nBin \nAli‟ print(nama)
- Above example shows how to print multiple line using „\n‟ character.
Strings in Python (Cont.)
- Strings also can do concatenate by using plus symbol
- For example:
First_name = „Muhammad‟
Last_name = „Ariff‟
Full_name = First_name +
Last_name
print(Full_name)
Strings in Python (Cont.)
- Previous example shows that we can combine two variables of strings. But the result does not have space between them.
- To include it, just add space between them.
- Example:
Full_name = First_name + „ „ +
last_name
Strings in Python (Cont.)
- So you want to calculate how many characters in your name?
- Using function named len
- Example:
Length_name =
len(full_name)
- The value length is including space
Exercise for Strings 1
Create a variable first name and last name 2
Then combine it to full name 3
Print the name 4
Calculate the length of characters for full name
Taking input from user
- Sometimes you need to make a program that need to take an input from the user.
- For that purpose, you cannot simply assign the value, since it depends on the user to key in.
- Example of taking an input using python:
Name = input(„Please input your name: ‟)
- We use input function to take the input. You can write any strings between the bracket and quotes.
Taking input from user (Cont.)
- But if you want to take an input integer or float, then we need to casting (transform) it into integer or float.
- By default, input function will take the input as a String.
- Example:
Height = float(input(„Height: „))
Number_of_car =
int(input(„Number of car: „))
Exercise for Input
- Make a program that taking input for:
- Name
- Age
- Height
- Weight
- Display all the information like the
output in the picture
Answer for Input
Exercise
Selection in Python
- Selection is a situation where we need to choose or select something based on circumstances.
- For example, grade A will be given to the student IF the student‟s mark get more than 80%
- Example in Python:
if mark >= 80:
grade = „A‟
elif mark >= 60:
grade = „B‟
elif mark >= 50:
grade = „C‟
else:
grade = „D‟
Looping in
Python
- Selection is a situation where we need to repeat the same task for specific times or infinity
- For example, you want to add numbers from 1 to 10
- Example in Python:
sum = 0
for number in range(1,11):
sum = sum + number
print(sum)
Conclusion
- Python is easier in doing calculation, display output and taking an input
- Use print() to display any output
- Use variable to store any value, it is easier to use that variable again