Writing our first Python Program
In order to write python code we need a text editor.In this blog i am going to use IDLE(Python), for installing IDLE you can refer my previous post on installing python.
Example 1---Working with print statement
Save the below code as ex1.py
Note: We will execute the code from the windows command prompt.
Save python code in a .py extension
I have saved ex1.py at "E:\python_blog\basics1" location.
now if we open the command prompt by default the location is C: drive
In order to execute our code found at E: driver ,we have to change the location from C: to E:
In order to execute the python code we first have to navigate to the program location and type "python ex1.py"
Lets us execute another example from python IDLE
Save the below code in ex2.py
Example 2-----Multi-line print statement
Now let execute ex2.py using IDLE
Step 1: Open the file using IDLE
Step 2: Once the file is opened RUN it.
Step 3: Output in the IDLE console
Lessons Learnt
- Comments are meant for readability of a program a single line comment start with a # symbol and a multi-line comment starts and ends with a """ symbol.
- In order to print statement we use a keyword "print" .
- We can print symbols ,numbers and characters.
Variables
Variables are used to store data ,the data can be numbers,special characters or just plain text.
%s is used for strings and %d is used for numbers.
Save the below code in ex3.py
Example 3 --- working with variables
%s is used for strings and %d is used for numbers.
Save the below code in ex3.py
Example 3 --- working with variables
Output
Arithmetic Operators
Operator | Operation | Expression | English description | Result |
+
|
addition | 11 + 56
|
11 plus 56 | 67 |
-
|
subtraction | 23 - 52
|
23 minus 52 | -29 |
*
|
multiplication | 4 * 5
|
4 multiplied by 5 | 20 |
**
|
exponentiation | 2 ** 5
|
2 to the power of 5 | 32 |
/
|
division | 9 / 2
|
9 divided by 2 | 4.5 |
//
|
integer division | 9 // 2
|
9 divided by 2 | 4 |
%
|
modulo (remainder) | 9 % 2
|
9 mod 2 | 1 |
Now when multiple operators are found in an expression, they are evaluated in the order of precedence.
Operator | Precedence |
** | highest |
- (negation) | |
*, /, //, % | |
+ (addition), - (subtraction) | lowest |
Example 4 --- Working with operators
Now in the above example the values ie...variables value are fixed. In order to change the variables values based on the user input we need to accept value from the user.
-------------*********___________________^^^^^^^^---------!!@@@----------------
Accepting Inputs
Now in the above example the value ie...variables value are fixed. In order to change the variables values based on the user input we need to accept value from the user.
We can receive input from the user using the input() --function
We can receive input from the user using the input() --function
Example 5 --- Working with input() --- function
Output
Functions
It is block of code that can accomplish a specific task.
Functions usually "take in" data, process it , and "return" a result.
Once a function is written, it can be used over and over again.
Functions can be called from inside of another functions.
Note: When we write a function we are beginning a new block of code, and it starts with a ":" symbol
and the subsequent lines are indented with a tab-space.
In other programming languages we start a new block of code is written within { }.
Function Definition
def function_name(parameters):
body
....
....
....
def--- Keyword used to define a function
function_name---It is the name of the function
parameters--------Function may have parameters
body-------------- 1 or more statements often ending with a return statement
Example 6---Function which take no parameters and no return type
Output
Lets write a function which compute the area of a triangle.
Example 7---Function which take two parameters and return type is a number
Functions usually "take in" data, process it , and "return" a result.
Once a function is written, it can be used over and over again.
Functions can be called from inside of another functions.
Note: When we write a function we are beginning a new block of code, and it starts with a ":" symbol
and the subsequent lines are indented with a tab-space.
In other programming languages we start a new block of code is written within { }.
Function Definition
def function_name(parameters):
body
....
....
....
def--- Keyword used to define a function
function_name---It is the name of the function
parameters--------Function may have parameters
body-------------- 1 or more statements often ending with a return statement
Example 6---Function which take no parameters and no return type
Lets write a function which compute the area of a triangle.
Boolean-Logic
In Boolean-Logic values can be either True or False.
We can perform operation on Boolean-Logic using Boolean-operators.
We can perform operation on Boolean-Logic using Boolean-operators.
Boolean-operators---NOT,AND,OR
Truth-table--NOT
a | NOT a |
True | False |
False | True |
Truth-table--AND--OR
a | b | a AND b | a OR b | NOT(a AND b) | NOT(a OR b) |
True | True | True | True | False | False |
True | False | False | True | True | False |
False | True | False | True | True | False |
False | False | False | False | True | True |
Example 8--Lets write a simple Boolean program
Output
Comparison Operators
In order to generate Boolean-Logic we use comparison operators.
comparision operators
> greater
< lesser
>= greater and equal to
<= lesser than and equal to
== equal to
!= not equal to
comparision operators
> greater
< lesser
>= greater and equal to
<= lesser than and equal to
== equal to
!= not equal to
Conditional Statements
We can execute particular statements based on condition.
The condition can be comparison operator or Boolean Logic.
The condition can be comparison operator or Boolean Logic.
Syntax---if statement
Note :Condition can be formed using comparison operators
if condition:
statement 1
....
...
Syntax---if and else
if condition1:
statement 1
....
else:
statement 2
Syntax---if , elif and else
if condition 1:
statement 1
elif condition 2:
statement 2
else:
statement 3
Note:We can have 0 or more elif statements.
We can have 0 or 1 else statement and the else statement must be the last statement.
Example 9--Lets write a program using if-elif-else clause.
A train was scheduled to arrive at a particular time and it is now estimated to arrive at another time.
Write a function that returns the train's status: "on time","early" or "delayed".
Output
Function with Boolean return type
Example -10.1
Write function which tell us if a number is even or not.
Answer:
when an even number is divide by 2 the remainder is Zero using this we can solve this problem.
when an even number is divide by 2 the remainder is Zero using this we can solve this problem.
The above code can be simplified
Example 10.2
The output is same for both 10.1 and 10.2
Example 11
Write a function which take two integers, and return the sum of the two integers but if the two number are equal the function return double of the sum of the two integers.
Example 12.1
Dog_trouble--We have a dog which barks.Time is represented from 0 to 24.
If the dog is barking and the time is before 7 or after 20,we are in trouble.
Return True if you are in trouble else False
The above example can be simplified
Example 12.2
Example 13
make10--Write a function given 2 ints, a and b, return True if one if them is 10 or if their sum is 10.
No comments:
Post a Comment