Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
7 views

Python

Python is a versatile programming language used for software and web development, data analysis, and more, known for its readability and simplicity. It supports various programming paradigms and operates across multiple platforms. Key features include easy syntax, indentation for code blocks, and support for multiple data types and variable naming conventions.

Uploaded by

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

Python

Python is a versatile programming language used for software and web development, data analysis, and more, known for its readability and simplicity. It supports various programming paradigms and operates across multiple platforms. Key features include easy syntax, indentation for code blocks, and support for multiple data types and variable naming conventions.

Uploaded by

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

This is not for school use

PYTHON
FROM W3SCHOOL

WHAT IS PYTHON
Python is a programming language that is used to create software, websites, and analyze
data. It is easy to learn, efficient, and can run on many different platforms. Python is a general-
purpose language that is used in many fields, including data science, web development, and
software development.

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.

0| P a g e
3/26/2025 1:02:10 PM
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.

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.

indentation
content Correct example False example
Python uses indentation to if 5 > 2: if 5 > 2:
indicate a block of code. print("Five is print ("Five is
greater than two!") greater than two!")
Python will give you an
error if you skip the
indentation:

The number of spaces is if 5 > 2: if 5 > 2:


up to you as a print ("Five is print ("Five is
programmer, the most greater than two!") greater than two!")
common use is four, but it if 5 > 2: print ("Five
has to be at least one. print ("Five is is greater than two!")
greater than two!")
You have to use the same
number of spaces in the
same block of code,
otherwise Python will give
you an error:

1|Page
3/26/2025 1:02:10 PM

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:

Comments in Python:

content example
Comments starts with a #, and Python #This is a comment
will ignore them: print ("Hello, World!")
Comments can be placed at the end of a print ("Hello, World!") #This is a
line, and Python will ignore the rest of comment
the line:
A comment does not have to be text that #print ("Hello, World!")
explains the code, it can also be used to print ("Cheers, Mate!")
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:

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

Or, not quite as intended, you can use a multiline string. 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:

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

2|Page
3/26/2025 1:02:10 PM

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

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

type content example


str abcdef

Int 20

Float 20.5456645654

complex 1j

list apple, banana, cherry

tuple apple, banana, cherry

range range

dict

set

frozensat

bool

bytes

Byte array

3|Page
3/26/2025 1:02:10 PM
Memory view

none

GET THE TYPE

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

input output
X = 5 <CLASS 'INT'>
PRINT(TYPE(X))
Y = "JOHN" <CLASS 'STR'>
PRINT(TYPE(Y))

SINGLE OR DOUBLE QUOTES?

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

x = "John"
# is the same as
x = 'John'

CASE-SENSITIVE

Variable names are case-sensitive.

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

4|Page
3/26/2025 1:02:10 PM
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.

CORRECT EXAMPLE FLASE EXAMPLE


myvar = "John" 2myvar

MY_VAR = "JOHN" My-var

_MY_VAR = "JOHN" My var

myVar

MYVAR

Myvar2

MULTI WORDS VARIABLE NAMES


Variable names with more than one word can be difficult to read.
There are several techniques you can use to make them more readable:

CAMEL CASE

Each word, except the first, starts with a capital letter:

myVariableName = "John"

PASCAL CASE

Each word starts with a capital letter:

MyVariableName = "John"

5|Page
3/26/2025 1:02:10 PM

SNAKE CASE

Each word is separated by an underscore character:

my_variable_name = "John"

MANY VALUES TO MULTIPLE VARIABLES

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

x, y, z = "Orange", "Banana", "Cherry"


print(x)
print(y)
print(z)

Note: Make sure the number of variables matches the number of values, or
else you will get an error.

ONE VALUE TO MULTIPLE VARIABLES

And you can assign the same value to multiple variables in one line:

x = y = z = "Orange"
print(x)
6|Page
3/26/2025 1:02:10 PM
print(y)
print(z)

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.

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, you output multiple variables, separated by a comma:

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

You can also 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:

7|Page
3/26/2025 1:02:10 PM
x = 5
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:

x = 5
y = "John"
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:

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

PYTHON NUMBERS

There are three numeric types in Python:

 int
 float
 complex

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

8|Page
3/26/2025 1:02:10 PM

9|Page
3/26/2025 1:02:10 PM

10 | P a g e
3/26/2025 1:02:10 PM

11 | P a g e
3/26/2025 1:02:10 PM

12 | P a g e
3/26/2025 1:02:10 PM

13 | P a g e

You might also like