0% found this document useful (0 votes)
14 views

PYTHONOTES

Uploaded by

Saksham Jha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

PYTHONOTES

Uploaded by

Saksham Jha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

PYTHON

Python is one of the world's easiest and most popular programming languages. It can
be used for almost anything, including:

 Web development
 Game development
 Artificial intelligence
 Operating systems
 Desktop applications
 And more.

Python is considered a very powerful language due to its large community and many
built-in libraries. Read more

This course will teach you the basics of Python, without requiring any prior
knowledge.

Hello World!
The "Hello World!" is a simple program that outputs Hello World! to the screen.

It is often used as a first introduction to a new programming language.

Let's take a look at the "Hello World!" program in Python:


print("Hello World!")

Numbers
Variables are containers that hold data values. They are used to store, manipulate, and
display information within a program.

In short a variable is like a memory unit that we can access by typing the name of the
variable.
Each variable has a unique name and a value that can be of different types. Python is
capable of automatically detecting the variable type, which makes coding more
efficient.

To initialize a variable, we use the following format:


variable_name = value

Let's take a look at the different types of numbers:

int - whole number, such as 1 or -2 .

float - real number, such as 1.32 or 0.98 .

In the following example:

 We initialize a variable of type int with the name a and the value 3 .
 We initialize a variable of type float with the name b and the value 13.2 .
a = 3
b = 13.2

String
A char is a single character (For example: 1, 6, %, b, p, ., T, etc.)

The str (string) type is a special type that consists of multiple chars.

To initialize a string value in a variable, enclose it within single or double quotation


marks:
s1 = 'This is a string'

s2 = "This is also a string"

In the above example, two string variables initialized, named s1 and s2 .

Boolean
A bool (Boolean) type has only 2 possible values: True or False .

To assign a bool value to a variable,


variable_true = True

variable_false = False

In the above example, two variables named variable_true and variable_false are
initialized, with the values True and False respectively.

Booleans are the building blocks for creating logic in the programs we write. We
have a whole chapter about logic and conditions.

Arithmetic operators
Operators used to perform operations on values.

First we will discuss the most basic arithmetic operators, they may be familiar from
math classes.

Operator Operation Example

+ Addition 3+2=5

- Subtraction 3-2=1

* Multiplication 3*2=6

/ Division 4/2=2

% Modulus (Remainder after division) 3%2=1

Let's see usage example,


a = 3
b = 5
c = a + b # c holds 8

Modulus operation provides the remainder that results from dividing the first value
by the second value.

For example:

14 % 4 returns 2 because 4 can be inserted into 14 three times (4*3 = 12) and now 14
minus 12 equals 2 (The remainder).
Arithmetic shortcuts
Python created a cool shortcut for self-arithmetic operations.

For example instead of writing:


a = 5
a = a + 3 # a holds 8

We can simplify it by writing += :

a = 5
a += 3 # a hold 8

The += is adding to a itself the value 3

This operation is valid for all arithmetic operations:

Operato
Shortcut
r

+ +=

- -=

* *=

/ /=

% %=

Comparison Operators
Comparison operators is used to compare between two operands.

Sometimes we need to check whether an operand is bigger/smaller/... than another


operand. The following table shows possible operators for comparison:

Operator Meaning Example

== Equal 1 == 2 return false

!= Not Equal 1 != 2 return true


Operator Meaning Example

> Greater Than 1 > 2 return false

< Lower Than 1 < 2 return true

Greater or
>= 1 >= 2 return false
Equal

<= Lower or Equal 1 <= 2 return true

The comparison operator returns True if the comparison is correct or False otherwise.

For example:
var1 = 13
var2 = 12
var3 = var1 != var2

var3 will hold True because var1 and var2 are not equal

Remember the boolean type, var3 is a boolean.

You might also like