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

Python 1

The document provides an overview of computer languages, focusing on Python as a high-level, interpreter-based language that supports object-oriented programming. It explains key concepts such as variables, data types, operators, and type conversion, along with examples of syntax and usage. Additionally, it covers flowcharts and algorithms as methods for problem-solving in programming.

Uploaded by

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

Python 1

The document provides an overview of computer languages, focusing on Python as a high-level, interpreter-based language that supports object-oriented programming. It explains key concepts such as variables, data types, operators, and type conversion, along with examples of syntax and usage. Additionally, it covers flowcharts and algorithms as methods for problem-solving in programming.

Uploaded by

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

Computer Languages

Machine language Assembly Level High Level Language


Language

Mnemonics Compiler Interpreter


Assembler
Algorithm

Stepwise instructions to solve a problem

Flowchart Graphical /Pictorial


representation of steps to solve a
problem
Flowchart Symbols
PYTHON is English like language used to create web based software,
Internet applications, games and mobile apps.
* Supports Object –oriented programing.
•Interpreter based Language.
• Portable and cross platform language.
•Free and open source language.
• Case-sensitive language. In Python , ‘sum’ and ‘SUM’ are not the same.
They will be interpreted differently.
Variables

A variable is a quantity that can store varying data values during the
execution of the program. A variable is represented by a symbolic name,
which may be short(like x,y) or descriptive (like name, age,grade , d_o_b )

Python has no command for declaring a variable.


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

x=5 # An integer assignment


y = "John“ # A string assignment
Temperature = 35.7 # A floating point number
print(x)
print(y)

x = "John"
# is the same as
x = 'John'
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)

Examples

myvar = "John"
my_var = "John"
_my_var = "John"
myVar = "John"
MYVAR = "John"
myvar2 = "John"
Many Values to Multiple Variables

Python allows you to assign values to multiple variables


in one line:

Example
x, y, z = "Orange", "Banana", "Cherry“ a, b,c = 5, 6, 10
print(x) a, b, c = 30,15, “ Abhay”
print(y)
print(z)

One Value to Multiple Variables

And you can assign the same value to multiple variables


in one line:
Example
x = y = z = "Orange“ a = b = c = 25
Data Types in Python

Integer
Numeric
Floating point
String
x = 1 # int
y = 2.8 # float

Strings in python are surrounded by either single quotation marks, or double quotation
marks.
'hello' is the same as "hello".
You can display a string literal with the print() function:

a = "Hello"
print(a)
Operators are symbols that perform some calculations or manipulations on
data.
The quantities (values and variables ) that operators manipulate are known as
the operands
c=a =+ b a and b are operands and + is an operator.

Arithmetic operators
Assignment operators
Comparison operators /Relational
Logical operators

5 + 10
Python Arithmetic Operators
Arithmetic operators are used with numeric values to perform common mathematical operations:

Operator Name Example


+ Addition x+y “Hello“+
”World”
- Subtraction x-y

* Multiplication x*y

/ Division x/y

% Modulus x%y

** Exponentiation x ** y

// Floor division x // y
Python Comparison Operators /Relational
Operators
Comparison operators are used to compare two values:

Operator Name Example (x=5 ,y=3) Result

== Equal x == y False

!= Not equal x != y

> Greater than x>y

< Less than x<y

>= Greater than or equal to x >= y

<= Less than or equal to x <= y


ython Assignment Operators
nment operators are used to assign values to variables :

Operator Example Same As


= x=5 x=5 Assignment

+= x += 3 x=x+3
-= x -= 3 x=x-3
*= x *= 3 x=x*3
/= x /= 3 x=x/3
%= x %= 3 x=x%3
//= x //= 3 x = x // 3
**= x **= 3 x = x ** 3
Python Logical Operators
Logical operators are used to combine
conditional statements:

Operator Description Example


and Returns True if both x < 5 and x < 10
statements are true
or Returns True if one of x < 5 or x < 4
the statements is true
not Reverse the result, not(x < 5 and x < 10)
returns False if the
result is true
Python Nested if Statement
In Python a Nested if statement, we can
have an if…elif…else statement inside
another if…elif…else statement. This is
called nesting in computer
programming. Any number of these
statements can be nested inside one
another. Indentation is the only way to
figure out the level of nesting.
# Python program to demonstrate
# nested if with multiple if statements

i = -15;
# condition 1
if i != 0:
# condition 2
if i > 0:
print("Positive")
# condition 3
if i < 0:
print("Negative")
i = 0;

# if condition 1
if i != 0:
# condition 1
if i > 0:
print("Positive")

# condition 2
if i < 0:
print("Negative")
else:
print("Zero")
Type Conversion in Python
The act of changing an object’s data
type is known as type conversion.
The Python interpreter automatically
performs Implicit Type Conversion.
Python prevents Implicit Type
Conversion from losing data.
The user converts the data types of
objects using specified functions in
explicit type conversion, sometimes
referred to as type casting.
Implicit Type Conversion in Python
In Implicit type conversion of data types in
Python, the Python interpreter automatically
converts one data type to another without any
user involvement.

x = 10

print("x is of type:",type(x))

y = 10.6
print("y is of type:",type(y))

z=x+y

print(z)
print("z is of type:",type(z))
Output

x is of type: <class 'int'>


y is of type: <class 'float'>
20.6
z is of type: <class 'float'>
Explicit Type Conversion in Python

# initializing string
s = "10010"

# printing string converting to int base 2


c = int(s,2)
print ("After converting to integer base 2 : ", end="")
print (c)

# printing string converting to float


e = float(s)
print ("After converting to float : ", end="")
print (e)

You might also like