0% found this document useful (0 votes)
78 views13 pages

Pthon For BSC CYS 4th Sem Class Note 3 - 1 - 2023

The document provides an introduction to Python programming concepts such as variables, data types, casting, case sensitivity, naming conventions, outputting variables, and global vs local scope. It explains that variables store data values and are created using the assignment operator, and strings can be declared with either single or double quotes. It also covers built-in functions for casting between types, how to get the type of a variable, rules for naming variables, assigning multiple values to variables, and unpacking values from collections. The document discusses using print() to output variables and how to combine strings and numbers, as well as global and local scope and using the global keyword.

Uploaded by

nexusplayxus
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)
78 views13 pages

Pthon For BSC CYS 4th Sem Class Note 3 - 1 - 2023

The document provides an introduction to Python programming concepts such as variables, data types, casting, case sensitivity, naming conventions, outputting variables, and global vs local scope. It explains that variables store data values and are created using the assignment operator, and strings can be declared with either single or double quotes. It also covers built-in functions for casting between types, how to get the type of a variable, rules for naming variables, assigning multiple values to variables, and unpacking values from collections. The document discusses using print() to output variables and how to combine strings and numbers, as well as global and local scope and using the global keyword.

Uploaded by

nexusplayxus
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/ 13

BSC CYS 4th SEM

Python Programming

as
D
m
da
rin
A
What is a program?

A computer program is a set of instructions and as a term it can be


used as a verb as well as a noun. In terms of a verb, it is used as a
process of creating a software program by using programming
language. In terms of a noun, an application, program, or application
software is used to perform a specific task on the computer. For

as
example, Microsoft PowerPoint is an application, which provides a way
to create documents related to the presentation. Furthermore, a
browser is also an application, which allows us to browse any website.

D
Introduction to Python:
m
What are Variables?

In Python, variables are used to store data values. You can think of a
da
variable as a container that holds a specific value. Variables are
created by assigning a value to them using the assignment operator =.
Variable names should follow certain rules, such as starting with a letter
rin

or underscore, followed by letters, digits, or underscores.

Example:

x = 10
A

_y = "Pritisha"
print(x)
print(y)

What is Casting?

1
In Python, casting refers to the process of converting a variable from
one data type to another. This is also known as type conversion or
typecasting. Python provides several built-in functions for casting
between different data types.

Example:

float_num = 10.5

as
int_num = int(float_num)

print(int_num) # Output: 10

string_num = "20"

D
m
int_from_str = int(string_num)
da
print(int_from_str) # Output: 20

How to know the Type of variable?


rin

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

x = 10

y = "Subham"
A

print(type(x))

print(type(y))

What should we use Single or Double Quotes in Python?

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

What is Case-Sensitivity?

Variable names are case-sensitive.

a = 10.5

as
A = "Saheli"

#In this case A will not overwrite a

D
m
Rules for Python variables:

A variable can have a short name (like x and y) or a more descriptive


da
name (age, carname, total_volume).

A variable name must start with a letter or the underscore character


rin

A variable name cannot start with a number

A variable name can only contain alpha-numeric characters and


underscores (A-z, 0-9, and _ )
A

Variable names are case-sensitive (age, Age and AGE are three
different variables)

A variable name cannot be any of the Python keywords.

Example:
Legal variable names:

3
myvar = "John"

my_var = "John"

_my_var = "John"

myVar = "John"

MYVAR = "John"

as
myvar2 = "John"
Illegal variable names:

D
2myvar = "John"

my-var = "John"
m
my var = "John"

Multi Words Variable Names:


da
Variable names with more than one word can be difficult to read.

There are several techniques you can use to make them more
rin

readable:

Camel Case
A

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

myVariableName = "Sneha"

Pascal Case

Each word starts with a capital letter:

MyVariableName = "Ram"

4
Snake Case

Each word is separated by an underscore character:

my_variable_name = "Shyam"

as
D
m
Many Values to Multiple Variables
da
Python allows you to assign values to multiple variables in one line:

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

print(x)
rin

print(y)

print(z)
A

One Value to Multiple Variables

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

Example

x = y = z = "Orange"

5
print(x)

print(y)

print(z)

What is Collection Unpack in Python?

If you have a collection of values in a list, tuple etc. Python allows you

as
to extract the values into variables. This is called unpacking.

Example

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

x, y, z = fruits
m
print(x)

print(y)
da
print(z)

Output Variables in Python


rin

The Python print() function is often used to output variables.

Example:
A

x = "Python is awesome"

print(x)

Or

x = "Python"

y = "is"

6
z = "awesome"

print(x, y, z)

Or

x = "Python "

y = "is "

as
z = "awesome"

print(x + y + z)

D
For numbers, the + character works as a mathematical operator:
m
da
Example

x=5

y = 10
rin

print(x + y)

In the print() function, when you try to combine a string and a number
A

with the + operator, Python will give you an error:

Example

x=5

y = "Cat"

print(x + y)

7
The best way to output multiple variables in the print() function is to
separate them with commas, which even support different data types:

Example

x=5

y = "John"

as
print(x, y)

What are Global & Local Variables in Python?

D
Variables that are created outside of a function (as in all of the
examples above) are known as global variables.
m
Global variables can be used by everyone, both inside of functions and
da
outside.

Example:
rin

Create a variable outside of a function, and use it inside the function

x = "awesome"

def myfunc():
A

print("Python is " + x)

myfunc()

8
If you create a variable with the same name inside a function, this
variable will be local, and can only be used inside the function. The
global variable with the same name will remain as it was, global and
with the original value.

Example:

Create a variable inside a function, with the same name as the global

as
variable

x = "awesome"

D
def myfunc():

x = "fantastic"
m
print("Python is " + x)

myfunc()
da
print("Python is " + x)

What is the global Keyword in Python?


rin

Normally, when you create a variable inside a function, that variable is


local, and can only be used inside that function.

To create a global variable inside a function, you can use the global
A

keyword.

Example:

If you use the global keyword, the variable belongs to the global scope:

def myfunc():

9
global x

x = "fantastic"

myfunc()

print("Python is " + x)

Also, use the global keyword if you want to change a global variable

as
inside a function.

Example

D
To change the value of a global variable inside a function, refer to the
variable by using the global keyword:
m
x = "awesome"
da
def myfunc():

global x
rin

x = "fantastic"

myfunc()

print("Python is " + x)
A

10
A
rin
da
m
D
as

11
Conclusion

as
D
m
da
rin
A

12

You might also like