Python
Python
Python is a high-level interpreted and general-purpose coding language that focuses on code
readability. Coding in python can be done is=n fewer steps in comparison to other coding languages
such as C++ or Java. It was created by Guido van Rossum; it was named python due to the creators
love for the comedy show “Monty Python”. The name python itself has its advantages as it is short,
unique and mysterious. Python has versatile features and fewer lines of code.
Features of Python:
-Interpreted
-Interactive
-Dynamic (allows you to solve a bigger problem by breaking it down into smaller sub-problems,
solving each of those problems and storing the solution)
-Object oriented (Coding focused around an object or entity rather than on logic and functions.
Focuses on the object that needs to be manipulated rather than the logic required to manipulate it.)
-Portable
-Extensible in C++ or C
Python is a high-level language. Meaning coding is done in human readable form. Using words from
the English language. This is efficient for the coders however the computer does not understand
high level coding (source code). It only understands binary code (0’s and 1’s) this is known as
machine code. Interpreters and compilers covert the high-level code into the machine code to the
program can be executed. Python uses interpretors and hence it tends to slightly slowers, but in
some cases can be considered quite fast.
Variables, Expressions and Types:
• A value is one of the basic things a program works with, like a letter or a number.
Type Is a command which tells you the type of object a value is ,the syntax is as follows:
x = 5
s = "geeksforgeeks"
y = [1,2,3]
print(type(x))
print(type(s))
print(type(y))
NOTE: Every variable holds a value for example x is the variable and 5 is
the value. So when we say that a object type is “int” this means that this
variable can old hold integer values. This int is a sata type. Every value
has a data type, the data type tells us the type of value that is being
held in a cell.
Statement:
• A statement is a unit of code that the Python interpreter can
execute.
For example, the script
print 1
x = 2
print x
Expression:
Comparison operators:
• x != y # x is not equal to y
• x is y # x is the same as y
n%2 == 0 or n%3 == 0
Arithmetic Operators
Conditional Execution:
NOTE: for all condition function there is not “=” symbol “==” must always be used instead.
NOTE: indentation can be described as the spacing between different codes in the program. Which
executing different codes indentation must always be kept in mind. Otherwise a syntax error will
occur.
Functions = bool()
Syntax:
# Python program to illustrate
# built-in method bool()
# Returns False as x is 0
x = 0.0
print(bool(x))
If Statements:
#simple if statement
x=int(input("enter x:"))
if x>0:
if x%2 == 0:
else:
#elif conditions
y=int(input("enter y:"))
if x>y:
else:
#nestedif conditions
if x==y:
else:
if x>y:
else:
Summary:
If Statement is used for decision making. It will run the body of code only
when IF statement is true. Elif is the combination of else and if, it basically
means if the first statement is not true then see if see if this second condition
is true.
Note: Remember the colons after if elif and else.
Note: remember to put brackets around print and input functions
Note: the # is basically the comments meaning it will not be present in the
output but its for the users to understand why a particular piece of code is
there.
Follow the indentations!