0% found this document useful (0 votes)
10 views10 pages

Python

Python is a versatile programming language created by Guido van Rossum in 1991, used for web development, software development, mathematics, and system scripting. It features a simple syntax, supports multiple programming paradigms, and is platform-independent, making it suitable for rapid prototyping and production-ready software. Key concepts include variables, data types, indentation for defining scope, and control structures like if statements.

Uploaded by

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

Python

Python is a versatile programming language created by Guido van Rossum in 1991, used for web development, software development, mathematics, and system scripting. It features a simple syntax, supports multiple programming paradigms, and is platform-independent, making it suitable for rapid prototyping and production-ready software. Key concepts include variables, data types, indentation for defining scope, and control structures like if statements.

Uploaded by

noorisout
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

What is Python?

Python is a popular programming language. It was created by Guido van Rossum, and released in 1991.

It is used for:

●​ web development (server-side),

●​ software development,

●​ mathematics,

●​ system scripting.

What can Python do?

●​ Python can be used on a server to create web applications.

●​ Python can be used alongside software to create workflows.

●​ Python can connect to database systems. It can also read and modify files.

●​ Python can be used to handle big data and perform complex mathematics.

●​ Python can be used for rapid prototyping, or for production-ready software development.

Why Python?

●​ Python works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc).

●​ Python has a simple syntax similar to the English language.

●​ Python has syntax that allows developers to write programs with fewer lines than some other
programming languages.

●​ Python runs on an interpreter system, meaning that code can be executed as soon as it is
written. This means that prototyping can be very quick.

●​ Python can be treated in a procedural way, an object-oriented way or a functional way.

Good to know

●​ The most recent major version of Python is Python 3, which we shall be using in this tutorial.

●​ In this tutorial Python will be written in a text editor. It is possible to write Python in an
Integrated Development Environment, such as Thonny, Pycharm, Netbeans or Eclipse which are
particularly useful when managing larger collections of Python files.

Python Syntax compared to other programming languages

●​ Python was designed for readability, and has some similarities to the English language with
influence from mathematics.
●​ Python uses new lines to complete a command, as opposed to other programming languages
which often use semicolons or parentheses.

●​ Python relies on indentation, using whitespace, to define scope; such as the scope of loops,
functions and classes. Other programming languages often use curly-brackets for this purpose.

Example:
print("Hello, World!")

Python Indentation
Indentation refers to the spaces at the beginning of a code line.

Where in other programming languages the indentation in code is for readability only, the
indentation in Python is very important.

Python uses indentation to indicate a block of code.

ExampleGet your own Python Server


if 5 > 2:​
print("Five is greater than two!")

The number of spaces is up to you as a programmer, the most common use is four, but it
has to be at least one.

Example
if 5 > 2:​
print("Five is greater than two!") ​
if 5 > 2:​
print("Five is greater than two!")

Python Variables
In Python, variables are created when you assign a value to it:

Example
Variables in Python:
x = 5​
y = "Hello, World!"

Python has no command for declaring a variable.

You will learn more about variables in the Python Variables chapter.

Comments
Python has commenting capability for the purpose of in-code documentation.

Comments start with a #, and Python will render the rest of the line as a comment:

Example
Comments in Python:

#This is a comment.​
print("Hello, World!")

Statements
A computer program is a list of "instructions" to be "executed" by a computer.

In a programming language, these programming instructions are called statements.

The following statement prints the text "Python is fun!" to the screen:

Example
print("Python is fun!")

Many Statements
Most Python programs contain many statements.

The statements are executed one by one, in the same order as they are written:
Example
print("Hello World!")​
print("Have a good day.")​
print("Learning Python is fun!")

Semicolons (Optional, Rarely Used)


Semicolons are optional in Python. You can write multiple statements on one line by
separating them with ; but this is rarely used because it makes it hard to read:

Example
print("Hello"); print("How are you?"); print("Bye bye!")

However, if you put two statements on the same line without a separator (newline or ;),
Python will give an error:

Example
print("Python is fun!") print("Really!")

Result:

SyntaxError: invalid syntax

Print Text
You have already learned that you can use the print() function to display text or output
values:

ExampleGet your own Python Server


print("Hello World!")

Double Quotes
Text in Python must be inside quotes. You can use either " double quotes or ' single
quotes:
Example
print("This will work!")​
print('This will also work!')

If you forget to put the text inside quotes, Python will give an error:

Example
print(This will cause an error)

Result:
SyntaxError: invalid syntax.

Print Numbers
You can also use the print() function to display numbers:

However, unlike text, we don't put numbers inside double quotes:

ExampleGet your own Python Server


print(3)​
print(358)​
print(50000)

Variables
Variables are containers for storing data values.

Creating Variables
Python has no command for declaring a variable.

A variable is created the moment you first assign a value to it.

ExampleGet your own Python Server


x = 5​
y = "John"​
print(x)​
print(y)

Casting
If you want to specify the data type of a variable, this can be done with casting.

Example
x = str(3) # x will be '3'​
y = int(3) # y will be 3​
z = float(3) # z will be 3.0

Case-Sensitive
Variable names are case-sensitive.

Example
This will create two variables:

a = 4​
A = "Sally"​
#A will not overwrite a

Variable Names
A variable can have a short name (like x and y) or a more descriptive name (age, carname,
total_volume).

Rules for Python variables:

●​ A variable name must start with a letter or the underscore character


●​ A variable name cannot start with a number
●​ A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9,
and _ )
●​ Variable names are case-sensitive (age, Age and AGE are three different variables)
●​ A variable name cannot be any of the Python keywords.
Example
Legal variable names:

myvar = "John"​
my_var = "John"​
_my_var = "John"​
myVar = "John"​
MYVAR = "John"​
myvar2 = "John"

Output Variables
The print() function is often used to output variables.

ExampleGet your own Python Server


x = "Python is awesome"​
print(x)

You can also use the + operator to output multiple variables:

Example
x = "Python "​
y = "is "​
z = "awesome"​
print(x + y + z)

Built-in Data Types


In programming, data type is an important concept.

Variables can store data of different types, and different types can do different things.

Python has the following data types built-in by default, in these categories:

Text Type: str


Numeric Types: int, float, complex

Sequence Types: list, tuple, range

Mapping Type: dict

Set Types: set, frozenset

Boolean Type: bool

Binary Types: bytes, bytearray, memoryview

None Type: NoneType

String Format
As we learned in the Python Variables chapter, we cannot combine strings and numbers
like this:

ExampleGet your own Python Server


age = 36​
#This will produce an error:​
txt = "My name is John, I am " + age​
print(txt)

Python Conditions and If statements


Python supports the usual logical conditions from mathematics:

●​ Equals: a == b
●​ Not Equals: a != b
●​ Less than: a < b
●​ Less than or equal to: a <= b
●​ Greater than: a > b
●​ Greater than or equal to: a >= b

These conditions can be used in several ways, most commonly in "if statements" and
loops.

An "if statement" is written by using the if keyword.


ExampleGet your own Python Server
If statement:

a = 33​
b = 200​
if b > a:​
print("b is greater than a")

Indentation
Python relies on indentation (whitespace at the beginning of a line) to define scope in the
code. Other programming languages often use curly-brackets for this purpose.

Example
If statement, without indentation (will raise an error):

a = 33​
b = 200​
if b > a:​
print("b is greater than a") # you will get an error

The Elif Keyword


The elif keyword is Python's way of saying "if the previous conditions were not true, then
try this condition".

The elif keyword allows you to check multiple expressions for True and execute a block
of code as soon as one of the conditions evaluates to True.

ExampleGet your own Python Server


a = 33​
b = 33​
if b > a:​
print("b is greater than a")​
elif a == b:​
print("a and b are equal")

The Else Keyword


The else keyword catches anything which isn't caught by the preceding conditions.
The else statement is executed when the if condition (and any elif conditions) evaluate
to False.

ExampleGet your own Python Server


a = 200​
b = 33​
if b > a:​
print("b is greater than a")​
elif a == b:​
print("a and b are equal")​
else:​
print("a is greater than b")

INPUT/ READ x
INPUT/ READ y

sum= x + y
PRINT 'sum'

You might also like