You can implement Python syntax by directly writing in the Command Line. Here is a simple python program to do that.
>>> print("Good Morning!")
Output:
Good Morning!
Alternativel, you can create a python file on the server, through the .py file extension, and execute it in the Command Line:
C:\Users\Your Name>python newfile.py
Lists
Lists in Python are used to store multiple items within a single variable. They are created with the help of square brackets:
Example of python list:
newlist = ["rose", "sunflower", "daisy"]
print(newlist)
Output:
"rose", "sunflower", "daisy"
Unique characteristics of Python List:
- List items are changeable, ordered, and allow duplicate values.
- In Python, List items are indexed; the first item denotes index [0], the second item denotes index [1], etc.
List Length:
To find out a number of items a list has, you can use the len() function:
The following example prints the number of items in the list:
newlist = ["rose", "sunflower", "daisy"]
print(len(newlist))
Output:
3
Tuples
Tuples in Python store multiple items in a single variable. Essentially, a tuple is an ordered and unchangeable collection.
They are written with round brackets.
Example to create a Tuple in Python:
newtuple = ("rose", "sunflower", "daisy")
print(newtuple)
Unique characteristics of Tuples in Python:
- Tuple items are unchangeable, ordered, and allow duplicate values.
- Tuple items are indexed; the first item denotes index [0], the second item denotes index [1], and so on.
Dictionary:
Dictionaries store data values in key: value pairs. In other words, a dictionary is a collection that is changeable, ordered, and doesn’t permit duplicates. They are written in curly brackets.
Example to create and print a dictionary in Python:
mydict = {
"flower": "Rose",
"color": "Red",
}
print(mydict)
Output:
{'flower': 'Rose', 'color': 'Red'}
Conditional Statements in Python:
List of Conditional Statements in Python:
Python If statement
Python If…else statements
Python Nested if statements
Python If-elif ladder
Python Short hand if statements
Python Short hand if-else statements
Now let’s understand each of them:
Python If statement:
It denotes the simple decision-making statement. In it, when you run some lines of code, a block of statement will execute if the condition is true else it will not be executed.
Syntax:
if (condition):
# if block
Python If…else statements:
In the If..else statement, there are two blocks. If the condition is false, the statements in the else condition are executed.
Syntax:
if(condition):
# if block
else:
# else block
Python Nested if statements:
In Nested if statements, an if statement includes another if statement. This conditional statement is useful to check another condition within a condition. You can use it to check multiple conditions in a specific program.
Syntax:
If (condition1):
#statement to be executed if condition is true
If (condition2):
# statement to be executed if condition is true
#end of nested if(condition2)
#end of if
#end of if (condition1)
Python If-elif ladder:
This condition statement is stepwise executed from top to bottom. When the if condition is true, then the if block will be executed else not. Subsequently, it checks if the elif condition is true then it would be executed else not. Finally, if none of the conditions are true, the statements in the else block will be executed.
Syntax:
if(condition):
#statement to execute if condition is true
elif(condition):
#statement to be executed if condition is false and elif is true
elif(condition):
#statement to be executed if both conditions are false and this elif condition is true
. else:
#statement to execute when all the conditions are false
Python Short hand if statements:
The alternate name is ‘one line statement’. In python, you can write an if statement, if else statement, and elif statement in a single line without indentation. You can use a short hand if statement if a single line statement needs to be executed.
Syntax:
If condition: statement
Python Short hand if else statements:
This statement is helpful to write if else in a single line.
Syntax:
if (condition): # if statement
else: #else statement
Loops
A loop statement executes a statement or collection of statements multiple times. Three types of loops in Python are for loop, while loop, and nested loop.
for loop:
It executes a series of statements multiple times and condenses the code that deals with the loop variable.
while loop:
It iterates a statement or collection of statements while a given condition is ‘True’. Before executing the loop body, it tests the condition.
nested loops:
You can use a single or multiple loops into any other for, while, or do..while loop.