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

Learn Python11

Python is a versatile programming language used for web development, software development, and more. It emphasizes the importance of indentation for code blocks and allows for dynamic variable creation without explicit type declaration. Python supports various data types, including integers, floats, and complex numbers, and provides methods for type conversion.

Uploaded by

jolieabolebdeh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Learn Python11

Python is a versatile programming language used for web development, software development, and more. It emphasizes the importance of indentation for code blocks and allows for dynamic variable creation without explicit type declaration. Python supports various data types, including integers, floats, and complex numbers, and provides methods for type conversion.

Uploaded by

jolieabolebdeh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Learn Python

Python is a popular programming language.

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

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.

Example
code output
Hello, World!
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.

code output
if 5 > 2: error
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.

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

You have to use the same number of spaces in the same block of code,
otherwise Python will give you an error:

code output
if 5 > 2: error
print("Five is greater than two!")
print("Five is greater than two!")

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

code output
5
x = 5
y = "Hello, World!" Hello, World!
print(x)
print(y)
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:

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

Comments can be placed at the end of a line, and Python will ignore the
rest of the line:

code output
print("Hello, World!") #This is a comment Hello, World!

A comment does not have to be text that explains the code, it can also be
used to prevent Python from executing code:

Multiline Comments
Python does not really have a syntax for multiline comments.

To add a multiline comment you could insert a # for each line:

code output
#This is a comment
Hello, World!
#written in
#more than just one line
print("Hello, World!")
since Python will ignore string literals that are not assigned to a variable,
you can add a multiline string (triple quotes) in your code, and place your
comment inside it:

code output
"""
Hello, World!
This is a comment
written in
more than just one line
"""
print("Hello, World!")

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

code output
x = 5
5
y = "John"
print(x) John
print(y)

Variables do not need to be declared with any particular type, and can
even change type after they have been set.
code output
x = 5
John
x = "John"
print(x)

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

code output
x = str(3) 3
y = int(3)
z = float(3) 3
print(x) 3.0
print(y)
print(z)

Get the Type


You can get the data type of a variable with the type() function.

code output
x = 5 <class 'int'>
y = "John"
print(type(x)) <class 'str'>
print(type(y))
Single or Double Quotes?
String variables can be declared either by using single or double quotes:

code output
x = "John" John
print(x)
#double quotes are the same as single John
quotes:
x = 'John'
print(x)

Case-Sensitive
Variable names are case-sensitive.

code output
a = 4 4
A = "Sally"
Sally
print(a)
print(A)

Python - 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:
myvar = "John"
my_var = "John"
_my_var = "John"
myVar = "John"
MYVAR = "John"
myvar2 = "John"

Illegal variable names:

2myvar = "John"

my-var = "John"

my var = "John"

Python Data Types


Text Type: str

Numeric Types: int, float, complex

 Python Variables - Assign Multiple Values


 Python allows you to assign values to multiple variables in one line:

code output
x, y, z = "Orange", "Banana", "Cherry" Orange
print(x)
print(y) Banana
print(z) Cherry
One Value to Multiple Variables
And you can assign the same value to multiple variables in one line:

code output
X= y= z = "Orange" Orange
print(x)
print(y) Orange
print(z) Orange

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

code output
fruits = ["apple", "banana", "cherry"]
apple
x, y, z = fruits
print(x) banana
print(y) cherry
print(z)
print(fruits) ['apple', 'banana', 'cherry']

Python - Output Variables


code output
x = "Python is awesome"
Python is awesome
print(x)

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


comma:
code output
x = "Python"
Python is awesome
y = "is"
z = "awesome"
print(x, y, z)

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

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

Notice the space character after "Python " and "is ", without them the
result would be "Pythonisawesome".

For numbers, the + character works as a mathematical operator:

code output
x = 5
15
y = 10
print(x + y)

In the print() function, when you try to combine a string and a number
with the + operator, Python will give you an error:

code output
x = 5
error
y = “10”
print(x + y)

The best way to output multiple variables in the print() function is to


separate them with commas, which even support different data types:
code output
x =”5”
5 10
y = “10”
print(x , y)

There are three numeric types in Python:

 int
 float
 complex

 Variables of numeric types are created when you assign a value to


them:

code output
x = 1
<class 'int'>
y = 2.8
z = 1j <class 'float'>
<class 'complex'>
print(type(x))
print(type(y))
print(type(z))

Int

or integer, is a whole number, positive or negative, without decimals, of


unlimited length.

code output
x = 1
<class 'int'>
y = 356562225
z = -3255522 <class 'int'>
print(type(x)) <class 'int'>
print(type(y))
print(type(z))
Float
Float, or "floating point number" is a number, positive or negative,
containing one or more decimals.

code output
x = 1.10
<class 'float'>
y = 1.0
z = -35.59 <class 'float'>
print(type(x)) <class 'float'>
print(type(y))
print(type(z))

Complex
Complex numbers are written with a "j" as the imaginary part:

code output
x = 3+5j
<class 'complex'>
y = 5j
z = -5j <class 'complex'>
print(type(x)) <class 'complex'>
print(type(y))
print(type(z))
Type Conversion
You can convert from one type to another with the int(), float(),
and complex() methods:

code output
#convert from int to float:
1.0
x = float(1)
#convert from float to int: 2
y = int(2.8) (1+0j)
#convert from int to complex:
<class 'float'>
z = complex(1)
print(x) <class 'int'>
print(y) <class 'complex'>
print(z)
print(type(x))
print(type(y))
print(type(z))

You might also like