Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

Python -Coding

Python as programming language:

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.)

-High level(A high-level language is one which is understandable by us humans)

-Data modelling-how different objects relate to each other**Find out

-Portable

-Extensible in C++ or C

Interpreter Vs. Complier:

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.

The values we have seen so far are 1, 2, and 'Hello, World!'

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:

• An expression is a combination of values, variables, and operators.


A value all by itself is considered an expression, and so is a variable
Data types:
1) Int -Any integer
2) Float- Anything value that contains decimals or “e” or “E”
3) Complex- any value containing real+ imaginary numbers.
4) String- Anything in between””
If you have trouble entering characters within a sting: (Characters such as
“” or ‘’ or /)

5) Boolean -Data is processed to show if condition is true or false.

Below are some additional built in functions for reference:


Operators: Perform certain mathematical or logical manipulation.

Comparison operators:

• x != y # x is not equal to y

• x > y # x is greater than y

• x < y # x is less than y

• x >= y # x is greater than or equal to y

• x <= y # x is less than or equal to y

• x is y # x is the same as y

• x is not y # x is not the same as y


Logical operators:

• And, Or and Not.

x > 0 and x < 1

is true only if x is greater than 0 and less than 10

n%2 == 0 or n%3 == 0

if the number is divisible by 2 or 3 then its true

Arithmetic Operators

The operators +, -, *, /, and ** perform addition, subtraction, multiplication, division, and


exponentiation.

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.

Boolean- Converts a value to true or false through standard protocol

Functions = bool()

Condition through which a statement will be false:

 If a False value is passed.


 If None is passed.
 If an empty sequence is passed, such as (), [], ”, etc
 If Zero is passed in any numeric type, such as 0, 0.0 etc
 If an empty mapping is passed, such as {}.
 If Objects of Classes having __bool__() or __len()__ method, returning 0 or
False

Syntax:
# Python program to illustrate
# built-in method bool()

# Returns False as x is False


x = False
print(bool(x))

# Returns True as x is True


x = True
print(bool(x))

# Returns False as x is not equal to y


x = 5
y = 10
print(bool(x==y))

# Returns False as x is None


x = None
print(bool(x))

# Returns False as x is an empty sequence


x = ()
print(bool(x))

# Returns False as x is an emty mapping


x = {}
print(bool(x))

# Returns False as x is 0
x = 0.0
print(bool(x))

# Returns True as x is a nonempty string


x = 'GeeksforGeeks'
print(bool(x))

If Statements:

#simple if statement

x=int(input("enter x:"))

if x>0:

print ("x is greater than 0")

#alternative execution meaing else

if x%2 == 0:

print ("x is even")

else:

print ("x is odd")

#elif conditions

y=int(input("enter y:"))

if x>y:

print("x is greater y")


elif x<y:

print ("x is less than y")

else:

print ("x is equal to y")

#nestedif conditions

if x==y:

print ("x is equal to y")

else:

if x>y:

print ("x is greater than y")

else:

print ("x is less than y")

CHECK IDLE CODE FOR INDENTATION FOR FURTHER UNDERSTANDING.

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!

You might also like