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

Copy of L-1 Pyhton- Basic Fundamentals

The document provides an overview of basic Python programming fundamentals, including concepts such as variables, data types, operators, and comments. It explains the importance of indentation, tokens, keywords, and the different types of literals in Python. Additionally, it covers the various operators and their functions, as well as how to take user input and display data types.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Copy of L-1 Pyhton- Basic Fundamentals

The document provides an overview of basic Python programming fundamentals, including concepts such as variables, data types, operators, and comments. It explains the importance of indentation, tokens, keywords, and the different types of literals in Python. Additionally, it covers the various operators and their functions, as well as how to take user input and display data types.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

L-1 Python - Basic Fundamentals

Outline

Recall Memory
Python
Comments
Indentation
Token
Identifiers
Keywords
Constants
Variables
Literals
Operators and its
types
precedence of
operators
data types
PYHTON
i

Python is a very popular general-purpose interpreted, interactive, object-oriented, and


high-level programming language. Python is dynamically-typed and garbage-collected
programming language. It was created by Guido van Rossum during 1985- 1990. Like
Perl, Python source code is also available under the GNU General Public License (GPL).

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

COMMENT
S
They are added with the purpose of making the source code easier for humans to
understand, and are ignored by Python interpreter.
Python supports single-line (or end-of-line) and multi-line (block) comments.
1) Single Line Comments - It should be start with #
# This is a comment.
2) Multi-Line Comments - Python does not provide a direct way to
comment multiple line. You can comment multiple lines as follows −
# This is a comment.
# This is a comment, too.
Following triple-quoted string is also ignored by Python interpreter and can be used as a
multiline comments: '''

TOKEN
The smallest part of the statement is a token. It may be characters, digits, special symbols
and punctuations.

a=18
Tokens are a, = and

KEYWOR
Keywords are special words , which convey some special meaning to the compiler.
Keywords are as follows
False await else import pass

None break except in raise

True class finally is return

and continue for lambda try

as def from nonlocal while

assert del global not with

input elif if or yield

IDENTIFIE
RS
It is the name given to the such as variable, function, list, dictionary etc

VARIABL

Variables are containers for storing data values.

Creating Variables
● A variable name must start with a letter or the underscore character
● A variable name cannot start with a number or any special character like $, (, * %
etc.
● A variable name can only contain alpha-numeric characters and underscores (A-z,
0-9, and _ )
● Python variable names are case-sensitive which means Name and NAME are two
different variables in Python.
● Python reserved keywords cannot be used naming the variable.

Legal variable names are: Rollno, Roll_no, _roll2


Illegal variable names: #ab, anc%123, roll no

Single value to Single Variable:

x=89;
y=”aman”

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)

One Value to Multiple Variables: And you can assign the same value to multiple
variables in one line:

x=y=z
=
"Orange"
print(x)
print(y)

LITERAL
S
Literals is a value assigned to variables while programming.We mainly have five types of
literals which includes
1. string literals - The group of characters in single, double or triple quotes.

s= 'Scaler Academy' #
single quotes
d="Hello World" #
double quotes
e="""Welcome #
Triple quotes
to
Scaler
2. numeric literals - Numerical literals in Python are those literals that contain digits
only
● Integer: The numerical literals that are zero, positive or negative natural numbers
and contain no decimal points are integers.
● Float: Unlike integers, these contain decimal points.
● Complex: Complex literals are represented by A+Bj. Over here, A is the real part.
And the entire B part, along with j, is the imaginary or complex part

A = 67 #integer
literal
B = 3.5 #float

3. boolean literals - Boolean literals have only two values-


True- True represents the value 1.,
False-False represents the value 0.

OPERA
x = True TORS
Y = False

Operators are used to perform operations on variables and values.Different operators are as
follows

● Arithmetic operators-

OPerator Description Syntax

+ Addition: adds two operands x+y

– Subtraction: subtracts two operands x–y

* Multiplication: multiplies two operands x*y

Division (float): divides the first operand by the


/ x/y
second

Division (floor): divides the first operand by


// x // y
the second

Modulus: returns the remainder when the first


% x%y
operand is divided by the second

** Power: Returns first raised to power second x ** y

● Assignment operators
Operator Description Syntax

Assign the value of the right side


= of the expression to the left side x=y+z
operand

Add AND: Add right-side


+= operand with left-side operand a+=b a=a+b
and then assign to left operand

Subtract AND: Subtract right


-= operand from left operand and a-=b a=a-b
then assign to left operand

Multiply AND: Multiply right


*= operand with left operand and a*=b a=a*b
then assign to left operand

Divide AND: Divide left operand


/= with right operand and then a/=b a=a/b
assign to left operand

Modulus AND: Takes modulus


a%=b a=a
%= using left and right operands and
%b
assign the result to left operand

Divide(floor) AND: Divide left


operand with right operand and
//= a//=b a=a//b
then assign the value(floor) to
left operand

Exponent AND: Calculate


exponent(raise power) value a**=b
**=
using operands and assign value a=a**b
to left operand

Performs Bitwise AND on


a&=b
&= operands and assign value to left
a=a&b
operand

Performs Bitwise OR on gn value to


|=
operands and assi left operand
Performs Bitwise xOR on
^= operands and assign value to left a^=b a=a^b
operand

● Comparison operators- Comparison operators are used to compare two


values:

Operator Description Syntax

Greater than: True if the left operand is greater than


> x>y
the right

< Less than: True if the left operand is less than the right x < y

== Equal to: True if both operands are equal x == y

!= Not equal to – True if operands are not equal x != y

Greater than or equal to True if the left operand is


>= x >= y
greater than or equal to the right

Less than or equal to True if the left operand is less


<= x <= y
than or equal to the right

Note :- = is an assignment operator and == comparison operator.

● Logical operators- Logical operators are used to combine conditional


statements:

Operator Description Example

and Returns True if both statements are true x < 5 and x < 10

or Returns True if one of the statements is x < 5 or x < 4


true
not Reverse the result, returns False if the not(x < 5 and x < 10)
result is true

DATA
TYPES
Python Data Types are used to define the type of a variable. It defines what type of data
we are going to store in a variable.
python has various data types
1. Numeric
2. String
3. List
4. Tuple
5. Dictionary
1. Numeric Data Type- The variable stores numeric value.

a=23 # integer
data
b=2.34 # float
data

2. String Data Type - The variable stores sequence of characters which are enclosed by ‘ ‘, “ “
or ‘’’ ‘’’
x = “hello”

3.Lists: List is also a sequence of values of any type. Values in the list are called
elements / items. These are mutable and indexed/ordered. List is enclosed in square
brackets.
x = [1, ‘a’,

4. Tuples: Tuple is also a sequence of values of any type. Values in the tuple are called
elements / items. These are immutable and indexed/ordered. Tuple is enclosed in round
brackets.

T = ('spam',
5.Dictionaries: These can store any number of python objects. What they store is a key
– value pairs, which are accessed using key. Dictionary is enclosed in curly brackets.

Immutable Types: The immutable types are those that can never change their
value in place. Integers, Floating Point Numbers, Booleans, Strings and Tuples are
immutable types

Mutable Types: The mutable types are those that can change their value in
place. Lists, Dictionaries and Sets are mutable types

type( ): The data type of a constant or variable can be displayed using type( ) statement with
the required argument.

a=2 a=[2 a=(2


3 3, 3,
45] 45)
type
(a) type
id( ) : It displays address of variable.

x=10
id(x)
1722770

input (): This function first takes the input from the user and converts it into a string.

Syntax:
Variable name = input('STATEMENT')

Example:

name = input('What is your name?\n')

There are various function that are used to take as desired input few of them are : –
● int(input())
● float(input())

num = int(input("Enter a number: "))


print(num, " ", type(num))
floatNum = float(input("Enter a decimal number: "))
print(floatNum, " ", type(floatNum))
Split( ): This function helps in getting multiple inputs from users. It breaks the given input
by the specified separator. If a separator is not provided then any white space is a
separator.
x, y = input("Enter two values: ").split()
print("Number of boys: ", x)
print("Number of girls: ", y)

Enter two values: 5 6

Number of boys: 5

Number of girls: 6

You might also like