PPT 4. Variables: (assignment)  A variable: holds a value.  A space in RAM we give a name to  A lot like parameters, only we create them within the.

Slides:



Advertisements
Similar presentations
Variables in C Amir Haider Lecturer.
Advertisements

1 CSC 221: Computer Programming I Fall 2006 interacting objects modular design: dot races constants, static fields cascading if-else, logical operators.
2/7/2008. >>> Overview * boolean * while * random * tuples.
ECS 15 if and random. Topic  Testing user input using if statements  Truth and falsehood in Python  Getting random numbers.
Python Programming Language
Mrs. Chapman. Tabs (Block Categories) Commands Available to use Script Area where you type your code Sprite Stage All sprites in this project.
Python November 14, Unit 7. Python Hello world, in class.
Python November 18, Unit 7. So Far We can get user input We can create variables We can convert values from one type to another using functions We can.
Chapter 2 Writing Simple Programs
Mr. Wortzman. Tabs (Block Categories) Available Blocks Script Area Sprite Stage All sprites in this project.
Python. What is Python? A programming language we can use to communicate with the computer and solve problems We give the computer instructions that it.
Shorter of two objects and changing color Functions, events and setting the color Susan Rodger, Duke University June 2008.
Week 2 - Friday.  What did we talk about last time?  Data representation  Binary numbers  Types  int  boolean  double  char  String.
Do Now Noticing skills What does input do?. Annotate your code to explain what happens name = input( “What is your name?\n” ) print(“Hello ”, name) Extension-Python.
Hands on Projects Dr. Bernard Chen Ph.D. University of Central Arkansas July 9 th 2012
Arrays for Random Storage Another way to use an array is to create an array with dozens, hundreds or thousands of items and then randomly select from them.
Instructor: Chris Trenkov Hands-on Course Python for Absolute Beginners (Spring 2015) Class #002 (January 17, 2015)
PYTHON CONDITIONALS AND RECURSION : CHAPTER 5 FROM THINK PYTHON HOW TO THINK LIKE A COMPUTER SCIENTIST.
PPT 2. Other things: functions  Can you have a function with no inputs?  Yes: def f( ): return (3 + 4 )  Can you have a function with no outputs? 
An Overview of Programming in Python CSC 161: The Art of Programming Prof. Henry Kautz 9/9/2009 Slides stolen shamelessly from Dr. Mark Goadrich, Centenary.
3 - Variables Lingma Acheson Department of Computer and Information Science, IUPUI CSCI N331 VB.NET Programming.
PPT3. Quick function:  Write a function that checks to see if a number is even or not.  What TYPE does this return?
How to start Visual Studio 2008 or 2010 (command-line program)
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
Functions. Built-in functions You’ve used several functions already >>> len("ATGGTCA")‏ 7 >>> abs(-6)‏ 6 >>> float("3.1415")‏ >>>
Homework Assignment You are going to research any artist of your choosing from any time period or genre. You are going to complete a one page double- spaced.
C OMPUTER P ROGRAMMING 1 Assignment. A SSIGNMENT We have used gets to input a value into variable The second way to give a variable a value is known as.
TO CALCULATE MEAN AND MEDIAN MARIA FATIMA BS-CS-1.
Algorithms Writing instructions in the order they should execute.
CSE 1341 Honors Note Set 05 Professor Mark Fontenot Southern Methodist University.
Strings:  Strings?  concatenate (join) +  make multiple copies using *  compare: > =
Math, Data Types. Python Math Operations OperationOperator Addition + Subtraction – Multiplication * Division (floating point) / Division (integer) //
Sect Probability. Def: Probability is the measure of the chances of an event happening A desired outcome is called a success Any other outcome is.
Logical Operators, Boolean Variables, Random Numbers This template was just too good to let go in one day!
Python Let’s get started!.
Modularity using Functions Chapter 4. Modularity In programming blocks of code often can be "called up" and reused whenever necessary, for example code.
PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!
Creating a mystic meg program A simple program asking the user questions and using their answers to build up a fortune telling in a list.
Variables and Strings. Variables  When we are writing programs, we will frequently have to remember a value for later use  We will want to give this.
Mixing Complementary Colors to create neutrals. Complementary colors are directly across from one another on the color wheel. When mixed they get various.
CompSci 4 Chap 6 Sec 2 Sep 30, 2010 Prof. Susan Rodger “All your troubles are due to those ‘ifs’,” declared the Wizard. If you were not a Flutterbudget.
Midterm: Question 1 (35%) (30 minutes) Write an assembly program to draw a rectangle. – (5%) Show a message to ask for information of the rectangle – (5%)
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
Python Arithmetic Operators OperatorOperationDescription +AdditionAdd values on either side of the operator -SubtractionSubtract right hand operand from.
Introduction to Python Developed by Dutch programmer Guido van Rossum Named after Monty Python Open source development project Simple, readable language.
Instructor: Chris Trenkov Hands-on Course Python for Absolute Beginners (Spring 2015) Class #003 (February 14, 2015)
Part 1 Learning Objectives To understand that variables are a temporary named location to store data and that programmers work with different data types.
Chapter 2 Writing Simple Programs
COLORS.
Python Let’s get started!.
Introduction to Python
Introduction to Computer Science / Procedural – 67130
ECS10 10/10
Formatting Output.
JavaScript Functions.
Review.
Validations and Error Handling
For -G7 programing language Teacher / Shamsa Hassan Alhassouni.
Can I color yellow?. Can I color yellow?
What Color is it?.
What is printed out? def f(x): print(x+3) return(x + 7) def g(k): return(f(k) + 2) print(g(1)) 4 10 def f2(x): return(x + 7) print(x+3) def g2(k): return(f2(k)
Python Basics with Jupyter Notebook
Shell Jeopardy! Add your name here.
Variables in C Topics Naming Variables Declaring Variables
Problem to solve Given four numbers, days, hours, minutes, and seconds, compute the total number of seconds that is equivalent to them. For example, 0.
CMPT 120 Lecture 4 – Unit 1 – Chatbots
Data Types and Expressions
Getting Started in Python
Presentation transcript:

PPT 4

Variables: (assignment)  A variable: holds a value.  A space in RAM we give a name to  A lot like parameters, only we create them within the function  Then we can use them inside the function def f(x): #simple and silly example of using a variable y = 3 # y only exists within this function return(x + y)

Variables: def f(x): y=3 y=y+x # Do the right side first, then put # that value into the left side. return(y**2) f(5)

More examples: def calcvol(length,width,depth): area = length * width #area only exists inside this function vol = area * depth return(vol) def bankaccount(x,add): dollars = 2.57 print( " Currently, you have " + str(dollars) + " in your bank account " ) if add == 1: dollars = dollars + x # evaluate right, then assign to left else: dollars = dollars - x return( " you now have " + str(dollars) + " in your bank account " ) #again, function ends when the return statement is executed. print(bankaccount(0.10,1) print(bankaccount(1.0, 0)

Shortcuts >>> x = 4 >>> x +=2 >>> x 6 >>> x -=7 >>> x >>> x *= 32 >>> x -32 >>> x /=8 >>> x -4.0 >>>

Example: def f(p1,p2): if p1>p2: x = p1-p2 else: x = p2-p1 if (x%2) == 1: # x is now what value? x+=1 # Now what is x? x/=2 # and now what is x? return(x) print(f(7,2)) print(f(24,82))

Input from user:  What if we want to ask the user to input something?  We can use input!  answer = input(“Do you love this class?”)  Takes what you typed in and places it in the variable, answer.  It’s always a string unless we convert it to another type if (answer == “yes”): return (“You get an A!”) else: return(“You fail.”)

Input def getage_1(): x = input(“How old are you?”) if x< 16: return(“You can’t drive”) else: return(“Get your license”) print(getage_1()) def getage_2(): x = input(“How old are you?”) x = int(x) if x < 16: return(“You can’t drive”) else: return(“Get your license”) print(getage_2()) The input function always returns a string type. what you type in in response to the question is always considered a string. If you want it to be an int, you must convert it.

Why should I use a variable here?: def f(): k = input("What is your favorite color?") if (k == 'blue'): return("You love harmony, are reliable, sensitive and always make an effort to think of others. You like to keep things clean and tidy and feel that stability is the most important aspect in life.") elif (k == 'red'): return(“you live life to the fullest and are tenacious and determined in their endeavors.") elif (k == 'green'): return("you are often affectionate, loyal and frank. Green lovers are also aware of what others think of them and consider their reputation very important. ") elif (k == 'yellow'): return("you enjoy learning and sharing your knowledge with others. Finding happiness comes easy to you and others would compare you to sunshine. ") elif (k == 'purple'): return("you are artistic and unique. You have a great respect for people but at times can be arrogant.") elif (k == 'brown'): return("you are a good friend and try your hardest to be reliable and dependable. Flashy objects are not something you desire; you just want a stable life.") print(f())

Variables and Random numbers:  You’ve used variables with Random Numbers  To generate random numbers between x and y: randrange(x,y)  To store the random number: randvar = randrange(0,100)  generates a random number between 0 and 99 (inclusive) and stores it in randvar  Now you can use randvar again and again throughout the function  To see what number is generated: print (randvar)