0% found this document useful (0 votes)
56 views19 pages

Python From Scratch

Python is a versatile programming language created by Guido van Rossum in 1991, used for web development, software development, and data handling. It features a simple syntax, supports multiple programming paradigms, and allows for rapid prototyping. The document also covers comments, variables, and naming conventions in Python.
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)
56 views19 pages

Python From Scratch

Python is a versatile programming language created by Guido van Rossum in 1991, used for web development, software development, and data handling. It features a simple syntax, supports multiple programming paradigms, and allows for rapid prototyping. The document also covers comments, variables, and naming conventions in Python.
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
You are on page 1/ 19

PYTHON FROM

SCRATCH
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.
Python Comments
➢ Comments can be used to explain Python code.
➢ Comments can be used to make the code more readable.
➢ Comments can be used to prevent execution when testing code.
Comments starts with a #, and Python will ignore them:
Example:
#This is a comment
print("Hello, World!")
To add a multiline comment, insert a # for each line:
Example:
#This is a comment
#written in
#more than just one line
print("Hello, World!")

Add a multiline string (triple quotes) in your code, and place your comment inside it.
Example:
"""
This is a comment
written in
more than just one line
"""
print("Hello, World!")
Python Variables
Variables are containers for storing data values.
In Python, variables are created when you assign a value to it
Example:
x=5
y = "John"
print(x)
print(y)
Variables do not need to be declared with any particular type, and can even change
type after they have been set.
Example:
x=4 # x is of type int
x = "Sally" # x is now of type str
print(x)

Casting: If you want to specify the data type of a variable, this can be done with casting.
x = str(3) # x will be '3'
y = int(3) # y will be 3
z = float(3) # z will be 3.0
print(x, y, z)
Type( )
You can get the data type of a variable with the type() function.
Example:
x=5
y = "John"
print(type(x))
print(type(y))
Single or Double Quotes

String variables can be declared either by using single or double quotes

x = "John“
print(x)
# is the same as
y = 'John’
print(y)
Case-Sensitive
Variable names are case-sensitive.

a=4 a=4

print(a) a = "Sally"

A = "Sally“ #a will overwrites a

print(A) print(a)

#A will not overwrite a print(a)


One Value to Multiple Variables
Assign the same value to multiple variables in one line

x = y = z = "Orange"
print(x)
print(y)
print(z)
Unpack a Collection

If you have a collection of values in a list, tuple etc. Python allows to extract the values
into variables. This is called unpacking.

fruits = ["apple", "banana", "cherry"]


x, y, z = fruits
print(x)
print(y)
print(z)
Output Variables
The Python print() function is often used to output variables.

x = "Python is awesome"
print(x)

In the print() function, output the multiple variables, separated by a comma

x = "Python"
y = "is"
z = "awesome"
print(x, y, z)
Use the + operator to output multiple variables

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

For numbers, the + character works as a mathematical operator

x=5
y = 10
print(x + y)
Try yourself….

x=5 x=5
y = "John" y = "John"
print(x + y) print(x, y)
Python - Variable Names
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.
Legal variable names:
Illegal variable names:
myvar = "John"
2myvar = "John"
my_var = "John"
my-var = "John"
_my_var = "John"
my var = "John"
myVar = "John"
MYVAR = "John"
myvar2 = "John"
Variable names with more than one word can be difficult to read.

Multi Words Variable Names


1. Camel Case
Each word, except the first, starts with a capital letter:
myVariableName = "John"

2. Pascal Case
Each word starts with a capital letter:
MyVariableName = "John"

3. Snake Case
Each word is separated by an underscore character:
my_variable_name = "John"

You might also like