Welcome to course on python.Please ensure to follow every steps in the course correctly
Python is a general-purpose programming language that is becoming ever more popular for data science. Companies worldwide are using Python to harvest insights from their data and gain a competitive edge. Start space academy’s online Python course now.
Just to give you a little excitement about Python, We are going to give you a small conventional Python Hello World program, You can try it by clicking the Run Button.
Code:
print (“Hello World”);
Now type “your name” instead of “Hello World” and run the code
1) Clear the existing codes in the programming console (ie main.py)
2) Enter the below code and click run.(Please enter your name in the code instead of your name in the below code )
Code:
print (“your name”);
A hash sign (#) that is not inside a string literal begins a comment. All characters after the # and up to the end of the physical line are part of the comment and the Python interpreter ignores them.
1) Please clear the current code in main.py and enter the below code.
print (“Hello, Python!”); #my comment
3)Click the Run button to see the output
Now we understood that the comments wont appear in the output and it can be used by the programmer to identify certain parts of code while coding
A variable is a named location used to store data in the memory. It is helpful to think of variables as a container that holds data that can be changed later in the program.
you can use the assignment operator “="
to assign a value to a variable.
1) Clear the line existing codes in the programming console(ie main.py)
2) Enter the below code and run it, you will find that the assigned value appears in the output ie “spacefoundation.com”
website = "spacefoundations.com"
print(website)
1) Clear the line existing codes in the programming console(ie main.py)
2) Enter the below code and run it
website = "spacefoundations.com"
print(website)
# assigning a new variable to website
website = "spaceplatforms.in"
print(website)
In the above program, we have assigned spacefoundations.com to the website variable initially. Then, the value is changed to spaceplatforms.in
1) Clear the line existing codes in the programming console(ie main.py)
2) Now enter the below code and run it
a, b, c = 5, 3.2, "Hello"
print (a)
print (b)
print (c)
3) Now let’s see what happens if we only print one variable…For that enter the code below and run it
a, b, c = 5, 3.2, "Hello"
print (a)
List is an ordered sequence of items. It is one of the most used datatype in Python and is very flexible. All the items in a list do not need to be of the same type.
Declaring a list is pretty straight forward. Items separated by commas are enclosed within brackets [ ]
1) Clear the line existing codes in the programming console(ie main.py)
2) Now enter the below code and run it
a = [1, 2, 3]
print(a)
We can use the slicing operator [ ]
to extract an item or a range of items from a list.
1) Clear the line existing codes in the programming console(ie main.py)
2) Now enter the below code and run it
a = [1,2,3,4,5,6,7,8]
print("a[2] = ", a[2])
print("a[0:3] = ", a[0:3])
print("a[5:] = ", a[5:])
String is sequence of Unicode characters. We can use single quotes or double quotes to represent strings. Multi-line strings can be denoted using triple quotes, '''
or """
.
1) Clear the line existing codes in the programming console(ie main.py)
2) Now enter the below code and run it
s = "Hello World,This is a string"
print(s)
s = '''This is a multiline
string'''
print(s)
Set is an unordered collection of unique items. Set is defined by values separated by comma inside braces { }
. Items in a set are not ordered.
1) Clear the line existing codes in the programming console(ie main.py)
2) Enter the below code and run it
a = {4,1,5,2,3}
# printing set variable
print("a = ", a)
# data type of variable a
print(type(a))
We can perform set operations like union, intersection on two sets. Sets have unique values. They eliminate duplicates.
a = {1,2,2,3,3,3}
print(a)
Operators are special symbols in Python that carry out arithmetic or logical computation. The value that the operator operates on is called the operand.
1) Clear the line existing codes in the programming console(ie main.py)
2) Now enter the below code and run it
x = 25
y = 15
# Output: x + y = 40
print('x + y =',x+y)
# Output: x - y = 10
print('x - y =',x-y)
# Output: x * y = 375
print('x * y =',x*y)
# Output: x / y = 1.66
print('x / y =',x/y)
1) Clear the line existing codes in the programming console(ie main.py)
2) Now enter the below code and run it
x = 25
y = 15
print('x + y =',x+y)
1) Clear the line existing codes in the programming console(ie main.py)
2) Now enter the below code and run it
x = 25
y = 15
print('x - y =',x-y)
1) Clear the line existing codes in the programming console(ie main.py)
2) Now enter the below code and run it
x = 25
y = 15
print('x * y =',x*y)
1) Clear the line existing codes in the programming console(ie main.py)
2) Now enter the below code and run it
x = 25
y = 15
print('x / y =',x/y)
You will get the output answer as 1.66
1) Clear the line existing codes in the programming console(ie main.py)
2) Enter the below code and run it
x = 5
y = 10
# Output: x > y is False
print('x > y is',x>y)
# Output: x < y is True
print('x < y is',x<y)
# Output: x == y is False
print('x == y is',x==y)
# Output: x != y is True
print('x != y is',x!=y)
# Output: x >= y is False
print('x >= y is',x>=y)
# Output: x <= y is True
print('x <= y is',x<=y)
Output:
x > y is False x < y is True x == y is False x != y is True x >= y is False x <= y is True
If statements are control flow statements which helps us to run a particular code only when a certain condition is satisfied. For example, you want to print a message on the screen only when a condition is true then you can use if statement to accomplish this in programming.
Example:
1) Clear the line existing codes in the programming console(ie main.py)
2) Now enter the below code and run it
flag = True if flag==True: print("Welcome") print("To") print("Space")
Output:
Welcome To Space
In the above example we are checking the value of flag variable and if the value is True then we are executing few print statements. The important point to note here is that even if we do not compare the value of flag with the ‘True’ and simply put ‘flag’ in place of condition, the code would run just fine so the better way to write the above code would be:
Example:
1) Clear the line existing codes in the programming console(ie main.py)
2) Now enter the below code and run it
flag = True if flag: print("Welcome") print("To") print("Space")
By seeing this we can understand how if statement works. The output of the condition would either be true or false. If the outcome of condition is true then the statements inside body of ‘if’ executes, however if the outcome of condition is false then the statements inside ‘if’ are skipped. Lets take another example to understand this:
Example:
1) Clear the line existing codes in the programming console(ie main.py)
2) Now enter the below code and run it
flag = False if flag: print("You") print("are") print("Awesome")
The output of this code is none, it does not print anything because the outcome of condition is ‘false’.
Example:
1) Clear the line existing codes in the programming console(ie main.py)
2) Now enter the below code and run it
num = 100 if num < 200: print("num is less than 200")
Output:
num is less than 200
We use if statements when we need to execute a certain block of Python code when a particular condition is true. If..else statements are like extension of ‘if’ statements, with the help of if..else we can execute certain statements if condition is true and a different set of statements if condition is false. For example, you want to print ‘even number’ if the number is even and ‘odd number’ if the number is not even, we can accomplish this with the help of if..else statement.
Example:
1) Clear the line existing codes in the programming console(ie main.py)
2) Now enter the below code and run it
num = 22 if num % 2 == 0: print("Even Number") else: print("Odd Number")
Output:
Even Number
1) Write a code to print Welcome to Earth in python.
2) Add 24+35 in python
3) Write a code to find whether 856 is even, If yes print Bingo in the output
Try these yourselves. All the best
You have completed the course, Now take the test and get certified.